How to use wrapIFrameArgument method in Testcafe

Best JavaScript code snippet using testcafe

Run Testcafe automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

runner-base-test.js

Source: runner-base-test.js Github

copy
1const hammerhead    = window.getTestCafeModule('hammerhead');
2const hhsettings    = hammerhead.get('./settings').get();
3const iframeSandbox = hammerhead.sandbox.iframe;
4
5const testCafeLegacyRunner = window.getTestCafeModule('testCafeLegacyRunner');
6const ERROR_TYPE           = testCafeLegacyRunner.get('../test-run-error/type');
7const Runner               = testCafeLegacyRunner.get('./runner');
8const RunnerBase           = testCafeLegacyRunner.get('./runner-base');
9const transport            = testCafeLegacyRunner.get('./transport');
10
11
12QUnit.begin(function () {
13    hhsettings.serviceMsgUrl = '/ping/10';
14
15    iframeSandbox.on(iframeSandbox.RUN_TASK_SCRIPT_EVENT, window.initIFrameTestHandler);
16    iframeSandbox.off(iframeSandbox.RUN_TASK_SCRIPT_EVENT, iframeSandbox.iframeReadyToInitHandler);
17
18    $('<iframe id="test-iframe"></iframe>').appendTo('body');
19});
20
21QUnit.done(function () {
22    iframeSandbox.off(iframeSandbox.RUN_TASK_SCRIPT_EVENT, window.initIFrameTestHandler);
23});
24
25transport.batchUpdate = function (callback) {
26    callback();
27};
28
29$.fn.load = function (callback) {
30    callback();
31};
32
33let lastError = null;
34
35RunnerBase.prototype._onFatalError = function (err) {
36    lastError = err;
37};
38
39Runner.prototype._onFatalError = function (err) {
40    lastError = err;
41};
42
43Runner.checkStatus = function () {
44};
45
46
47QUnit.testStart(function () {
48    lastError = null;
49});
50
51
52asyncTest('init API', function () {
53    const testRunner = new RunnerBase();
54
55    let testStarted = false;
56
57    ok(testRunner.act);
58    ok(testRunner.act._start);
59    ok(testRunner.act._onJSError);
60    ok(testRunner.ok);
61    ok(testRunner.notOk);
62    ok(testRunner.eq);
63    ok(testRunner.notEq);
64    ok(testRunner.handleAlert);
65    ok(testRunner.handleConfirm);
66    ok(testRunner.handlePrompt);
67    ok(testRunner.handleBeforeUnload);
68
69    testRunner.nextStep = 0;
70    testRunner.act._start([], []);
71
72    testRunner.on(testRunner.TEST_STARTED_EVENT, function () {
73        testStarted = true;
74        ok(!testRunner.act._start);
75        ok(!testRunner.act._onJSError);
76    });
77
78    testRunner.on(testRunner.TEST_COMPLETED_EVENT, function () {
79        ok(testStarted);
80        start();
81    });
82});
83
84asyncTest('Uncaught error in test script', function () {
85    const errorText = 'Test error';
86    const stepNames = ['1.Step name'];
87    const testSteps = [function () {
88        throw errorText;
89    }];
90
91    const testRunner = new RunnerBase();
92
93    testRunner.on(testRunner.TEST_STARTED_EVENT, function () {
94        window.setTimeout(function () {
95            ok(lastError);
96            ok(lastError.type === ERROR_TYPE.uncaughtJSErrorInTestCodeStep);
97            equal(lastError.scriptErr, errorText);
98
99            start();
100        }, 0);
101    });
102
103    testRunner.nextStep = 0;
104    testRunner.act._start(stepNames, testSteps);
105});
106
107module('inIFrame arguments');
108function wrapIFrameArgument (arg) {
109    return function () {
110        return arg;
111    };
112}
113
114test('DOM element', function () {
115    const testRunner = new RunnerBase();
116
117    let arg = null;
118
119    testRunner._initApi();
120    testRunner._runInIFrame = function (iFrame) {
121        arg = iFrame;
122    };
123
124    const iFrame = $('#test-iframe')[0];
125
126    testRunner.inIFrame(wrapIFrameArgument(iFrame), 0)();
127
128    equal(arg, iFrame);
129});
130
131test('jQuery object', function () {
132    const testRunner = new RunnerBase();
133
134    let arg = null;
135
136    testRunner._initApi();
137    testRunner._runInIFrame = function (iFrame) {
138        arg = iFrame;
139    };
140
141    const $iFrame = $('#test-iframe');
142
143    testRunner.inIFrame(wrapIFrameArgument($iFrame), 0)();
144
145    equal(arg, $iFrame[0]);
146});
147
148test('string selector', function () {
149    const testRunner = new RunnerBase();
150
151    let arg = null;
152
153    testRunner._initApi();
154    testRunner._runInIFrame = function (iFrame) {
155        arg = iFrame;
156    };
157
158    const $iFrame = $('#test-iframe');
159
160    testRunner.inIFrame(wrapIFrameArgument('#test-iframe'), 0)();
161
162    equal(arg, $iFrame[0]);
163});
164
165test('function', function () {
166    const testRunner = new RunnerBase();
167
168    let arg        = null;
169
170    testRunner._initApi();
171    testRunner._runInIFrame = function (iFrame) {
172        arg = iFrame;
173    };
174
175    const iFrameGetter = function () {
176        return $('#test-iframe')[0];
177    };
178
179    testRunner.inIFrame(wrapIFrameArgument(iFrameGetter), 0)();
180
181    equal(arg, iFrameGetter());
182});
183
184test('empty argument error', function () {
185    const testRunner = new RunnerBase();
186
187    testRunner._initApi();
188    testRunner.inIFrame(wrapIFrameArgument(null), 0)();
189
190    equal(lastError.type, ERROR_TYPE.emptyIFrameArgument);
191    lastError = null;
192
193    testRunner.inIFrame(wrapIFrameArgument('#notExistingIFrame'), 0)();
194    equal(lastError.type, ERROR_TYPE.emptyIFrameArgument);
195});
196
197test('not iFrame error', function () {
198    const testRunner = new RunnerBase();
199    const $div       = $('<div></div>').appendTo('body');
200
201    testRunner._initApi();
202    testRunner.inIFrame(wrapIFrameArgument($div), 0)();
203
204    equal(lastError.type, ERROR_TYPE.iframeArgumentIsNotIFrame);
205    $div.remove();
206});
207
208test('multiple argument error', function () {
209    const testRunner = new RunnerBase();
210    const $iFrame    = $('<iframe id="test-iframe-2"></iframe>').appendTo('body');
211
212    testRunner._initApi();
213    testRunner.inIFrame(wrapIFrameArgument('iframe'), 0)();
214
215    equal(lastError.type, ERROR_TYPE.multipleIFrameArgument);
216    $iFrame.remove();
217});
218
219test('incorrect argument error', function () {
220    const testRunner = new RunnerBase();
221
222    testRunner._initApi();
223
224    testRunner.inIFrame(wrapIFrameArgument(['#iframe']), 0)();
225    equal(lastError.type, ERROR_TYPE.incorrectIFrameArgument);
226    lastError = null;
227
228    testRunner.inIFrame(wrapIFrameArgument({ iFrame: $('#iframe') }), 0)();
229    equal(lastError.type, ERROR_TYPE.incorrectIFrameArgument);
230    lastError = null;
231});
232
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Testcafe on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)