How to use initCookiesOption 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.

actions.js

Source: actions.js Github

copy
1import TYPE from './type';
2import SelectorBuilder from '../../client-functions/selectors/selector-builder';
3import ClientFunctionBuilder from '../../client-functions/client-function-builder';
4import functionBuilderSymbol from '../../client-functions/builder-symbol';
5import { ActionCommandBase, CommandBase } from './base';
6import {
7    ActionOptions,
8    ClickOptions,
9    MouseOptions,
10    TypeOptions,
11    PressOptions,
12    DragToElementOptions,
13    OffsetOptions,
14    CookieOptions,
15} from './options';
16
17import { initSelector, initUploadSelector } from './validations/initializers';
18import { executeJsExpression } from '../execute-js-expression';
19import { isJSExpression } from './utils';
20
21import {
22    actionOptions,
23    integerArgument,
24    positiveIntegerArgument,
25    stringArgument,
26    nonEmptyStringArgument,
27    nullableStringArgument,
28    urlArgument,
29    stringOrStringArrayArgument,
30    setSpeedArgument,
31    actionRoleArgument,
32    booleanArgument,
33    functionArgument,
34    cookiesArgument,
35    setCookiesArgument,
36    urlsArgument,
37} from './validations/argument';
38
39import { SetNativeDialogHandlerCodeWrongTypeError } from '../../errors/test-run';
40import { ExecuteClientFunctionCommand } from './observation';
41import { camelCase } from 'lodash';
42
43
44// Initializers
45function initActionOptions (name, val, initOptions, validate = true) {
46    return new ActionOptions(val, validate);
47}
48
49function initClickOptions (name, val, initOptions, validate = true) {
50    return new ClickOptions(val, validate);
51}
52
53function initMouseOptions (name, val, initOptions, validate = true) {
54    return new MouseOptions(val, validate);
55}
56
57function initOffsetOptions (name, val, initOptions, validate = true) {
58    return new OffsetOptions(val, validate);
59}
60
61function initTypeOptions (name, val, initOptions, validate = true) {
62    return new TypeOptions(val, validate);
63}
64
65function initDragToElementOptions (name, val, initOptions, validate = true) {
66    return new DragToElementOptions(val, validate);
67}
68
69function initPressOptions (name, val, initOptions, validate = true) {
70    return new PressOptions(val, validate);
71}
72
73function initDialogHandler (name, val, { skipVisibilityCheck, testRun }) {
74    let fn;
75
76    if (isJSExpression(val))
77        fn = executeJsExpression(val.value, testRun, { skipVisibilityCheck });
78    else
79        fn = val.fn;
80
81    if (fn === null || fn instanceof ExecuteClientFunctionCommand)
82        return fn;
83
84    const options      = val.options;
85    const methodName   = 'setNativeDialogHandler';
86    const functionType = typeof fn;
87
88    let builder = fn && fn[functionBuilderSymbol];
89
90    const isSelector       = builder instanceof SelectorBuilder;
91    const isClientFunction = builder instanceof ClientFunctionBuilder;
92
93    if (functionType !== 'function' || isSelector)
94        throw new SetNativeDialogHandlerCodeWrongTypeError(isSelector ? 'Selector' : functionType);
95
96    if (isClientFunction)
97        builder = fn.with(options)[functionBuilderSymbol];
98    else
99        builder = new ClientFunctionBuilder(fn, options, { instantiation: methodName, execution: methodName });
100
101    return builder.getCommand([]);
102}
103
104function initCookiesOption (name, val, initOptions, validate = true) {
105    return val.map(cookie => new CookieOptions(cookie, validate));
106}
107
108// Commands
109export class DispatchEventCommand extends ActionCommandBase {
110    static methodName = camelCase(TYPE.dispatchEvent);
111
112    constructor (obj, testRun, validateProperties) {
113        super(obj, testRun, TYPE.dispatchEvent, validateProperties);
114    }
115
116    _getAssignableProperties () {
117        return [
118            { name: 'selector', init: initSelector, required: true },
119            { name: 'eventName', type: nonEmptyStringArgument, required: true },
120            { name: 'options', type: actionOptions },
121            { name: 'relatedTarget', init: initSelector, required: false },
122        ];
123    }
124}
125
126export class ClickCommand extends ActionCommandBase {
127    static methodName = camelCase(TYPE.click);
128
129    constructor (obj, testRun, validateProperties) {
130        super(obj, testRun, TYPE.click, validateProperties);
131    }
132
133    _getAssignableProperties () {
134        return [
135            { name: 'selector', init: initSelector, required: true },
136            { name: 'options', type: actionOptions, init: initClickOptions, required: true },
137        ];
138    }
139}
140
141export class RightClickCommand extends ActionCommandBase {
142    static methodName = camelCase(TYPE.rightClick);
143
144    constructor (obj, testRun, validateProperties) {
145        super(obj, testRun, TYPE.rightClick, validateProperties);
146    }
147
148    _getAssignableProperties () {
149        return [
150            { name: 'selector', init: initSelector, required: true },
151            { name: 'options', type: actionOptions, init: initClickOptions, required: true },
152        ];
153    }
154}
155
156export class ExecuteExpressionCommand extends CommandBase {
157    constructor (obj, testRun, validateProperties) {
158        super(obj, testRun, TYPE.executeExpression, validateProperties);
159    }
160
161    _getAssignableProperties () {
162        return [
163            { name: 'expression', type: nonEmptyStringArgument, required: true },
164            { name: 'resultVariableName', type: nonEmptyStringArgument, defaultValue: null },
165        ];
166    }
167}
168
169export class ExecuteAsyncExpressionCommand extends CommandBase {
170    constructor (obj, testRun, validateProperties) {
171        super(obj, testRun, TYPE.executeAsyncExpression, validateProperties);
172    }
173
174    _getAssignableProperties () {
175        return [
176            { name: 'expression', type: stringArgument, required: true },
177        ];
178    }
179}
180
181export class DoubleClickCommand extends ActionCommandBase {
182    static methodName = camelCase(TYPE.doubleClick);
183
184    constructor (obj, testRun, validateProperties) {
185        super(obj, testRun, TYPE.doubleClick, validateProperties);
186    }
187
188    _getAssignableProperties () {
189        return [
190            { name: 'selector', init: initSelector, required: true },
191            { name: 'options', type: actionOptions, init: initClickOptions, required: true },
192        ];
193    }
194}
195
196export class HoverCommand extends ActionCommandBase {
197    static methodName = camelCase(TYPE.hover);
198
199    constructor (obj, testRun, validateProperties) {
200        super(obj, testRun, TYPE.hover, validateProperties);
201    }
202
203    _getAssignableProperties () {
204        return [
205            { name: 'selector', init: initSelector, required: true },
206            { name: 'options', type: actionOptions, init: initMouseOptions, required: true },
207        ];
208    }
209}
210
211export class TypeTextCommand extends ActionCommandBase {
212    static methodName = camelCase(TYPE.typeText);
213
214    constructor (obj, testRun, validateProperties) {
215        super(obj, testRun, TYPE.typeText, validateProperties);
216    }
217
218    _getAssignableProperties () {
219        return [
220            { name: 'selector', init: initSelector, required: true },
221            { name: 'text', type: nonEmptyStringArgument, required: true },
222            { name: 'options', type: actionOptions, init: initTypeOptions, required: true },
223        ];
224    }
225}
226
227export class DragCommand extends ActionCommandBase {
228    static methodName = camelCase(TYPE.drag);
229
230    constructor (obj, testRun, validateProperties) {
231        super(obj, testRun, TYPE.drag, validateProperties);
232    }
233
234    _getAssignableProperties () {
235        return [
236            { name: 'selector', init: initSelector, required: true },
237            { name: 'dragOffsetX', type: integerArgument, required: true },
238            { name: 'dragOffsetY', type: integerArgument, required: true },
239            { name: 'options', type: actionOptions, init: initMouseOptions, required: true },
240        ];
241    }
242}
243
244export class DragToElementCommand extends ActionCommandBase {
245    static methodName = camelCase(TYPE.dragToElement);
246
247    constructor (obj, testRun, validateProperties) {
248        super(obj, testRun, TYPE.dragToElement, validateProperties);
249    }
250
251    _getAssignableProperties () {
252        return [
253            { name: 'selector', init: initSelector, required: true },
254            { name: 'destinationSelector', init: initSelector, required: true },
255            { name: 'options', type: actionOptions, init: initDragToElementOptions, required: true },
256        ];
257    }
258}
259
260export class ScrollCommand extends ActionCommandBase {
261    static methodName = camelCase(TYPE.scroll);
262
263    constructor (obj, testRun, validateProperties) {
264        super(obj, testRun, TYPE.scroll, validateProperties);
265    }
266
267    _getAssignableProperties () {
268        return [
269            { name: 'selector', init: initSelector, required: false },
270            { name: 'position', type: nullableStringArgument, required: false },
271            { name: 'x', type: positiveIntegerArgument, defaultValue: null },
272            { name: 'y', type: positiveIntegerArgument, defaultValue: null },
273            { name: 'options', type: actionOptions, init: initOffsetOptions, required: true },
274        ];
275    }
276}
277
278export class ScrollByCommand extends ActionCommandBase {
279    static methodName = camelCase(TYPE.scrollBy);
280
281    constructor (obj, testRun, validateProperties) {
282        super(obj, testRun, TYPE.scrollBy, validateProperties);
283    }
284
285    _getAssignableProperties () {
286        return [
287            { name: 'selector', init: initSelector, required: false },
288            { name: 'byX', type: integerArgument, defaultValue: 0 },
289            { name: 'byY', type: integerArgument, defaultValue: 0 },
290            { name: 'options', type: actionOptions, init: initOffsetOptions, required: true },
291        ];
292    }
293}
294
295export class ScrollIntoViewCommand extends ActionCommandBase {
296    static methodName = camelCase(TYPE.scrollIntoView);
297
298    constructor (obj, testRun, validateProperties) {
299        super(obj, testRun, TYPE.scrollIntoView, validateProperties);
300    }
301
302    _getAssignableProperties () {
303        return [
304            { name: 'selector', init: initSelector, required: true },
305            { name: 'options', type: actionOptions, init: initOffsetOptions, required: true },
306        ];
307    }
308}
309
310export class SelectTextCommand extends ActionCommandBase {
311    static methodName = camelCase(TYPE.selectText);
312
313    constructor (obj, testRun, validateProperties) {
314        super(obj, testRun, TYPE.selectText, validateProperties);
315    }
316
317    _getAssignableProperties () {
318        return [
319            { name: 'selector', init: initSelector, required: true },
320            { name: 'startPos', type: positiveIntegerArgument, defaultValue: null },
321            { name: 'endPos', type: positiveIntegerArgument, defaultValue: null },
322            { name: 'options', type: actionOptions, init: initActionOptions, required: true },
323        ];
324    }
325}
326
327export class SelectEditableContentCommand extends ActionCommandBase {
328    static methodName = camelCase(TYPE.selectEditableContent);
329
330    constructor (obj, testRun, validateProperties) {
331        super(obj, testRun, TYPE.selectEditableContent, validateProperties);
332    }
333
334    _getAssignableProperties () {
335        return [
336            { name: 'startSelector', init: initSelector, required: true },
337            { name: 'endSelector', init: initSelector, defaultValue: null },
338            { name: 'options', type: actionOptions, init: initActionOptions, required: true },
339        ];
340    }
341}
342
343export class SelectTextAreaContentCommand extends ActionCommandBase {
344    static methodName = camelCase(TYPE.selectTextAreaContent);
345
346    constructor (obj, testRun, validateProperties) {
347        super(obj, testRun, TYPE.selectTextAreaContent, validateProperties);
348    }
349
350    _getAssignableProperties () {
351        return [
352            { name: 'selector', init: initSelector, required: true },
353            { name: 'startLine', type: positiveIntegerArgument, defaultValue: null },
354            { name: 'startPos', type: positiveIntegerArgument, defaultValue: null },
355            { name: 'endLine', type: positiveIntegerArgument, defaultValue: null },
356            { name: 'endPos', type: positiveIntegerArgument, defaultValue: null },
357            { name: 'options', type: actionOptions, init: initActionOptions, required: true },
358        ];
359    }
360}
361
362export class PressKeyCommand extends ActionCommandBase {
363    static methodName = camelCase(TYPE.pressKey);
364
365    constructor (obj, testRun, validateProperties) {
366        super(obj, testRun, TYPE.pressKey, validateProperties);
367    }
368
369    _getAssignableProperties () {
370        return [
371            { name: 'keys', type: nonEmptyStringArgument, required: true },
372            { name: 'options', type: actionOptions, init: initPressOptions, required: true },
373        ];
374    }
375}
376
377export class NavigateToCommand extends ActionCommandBase {
378    static methodName = camelCase(TYPE.navigateTo);
379
380    constructor (obj, testRun, validateProperties) {
381        super(obj, testRun, TYPE.navigateTo, validateProperties);
382    }
383
384    _getAssignableProperties () {
385        return [
386            { name: 'url', type: urlArgument, required: true },
387            { name: 'stateSnapshot', type: nullableStringArgument, defaultValue: null },
388            { name: 'forceReload', type: booleanArgument, defaultValue: false },
389        ];
390    }
391}
392
393export class SetFilesToUploadCommand extends ActionCommandBase {
394    static methodName = camelCase(TYPE.setFilesToUpload);
395
396    constructor (obj, testRun, validateProperties) {
397        super(obj, testRun, TYPE.setFilesToUpload, validateProperties);
398    }
399
400    _getAssignableProperties () {
401        return [
402            { name: 'selector', init: initUploadSelector, required: true },
403            { name: 'filePath', type: stringOrStringArrayArgument, required: true },
404        ];
405    }
406}
407
408export class ClearUploadCommand extends ActionCommandBase {
409    static methodName = camelCase(TYPE.clearUpload);
410
411    constructor (obj, testRun, validateProperties) {
412        super(obj, testRun, TYPE.clearUpload, validateProperties);
413    }
414
415    _getAssignableProperties () {
416        return [
417            { name: 'selector', init: initUploadSelector, required: true },
418        ];
419    }
420}
421
422export class SwitchToIframeCommand extends ActionCommandBase {
423    static methodName = camelCase(TYPE.switchToIframe);
424
425    constructor (obj, testRun, validateProperties) {
426        super(obj, testRun, TYPE.switchToIframe, validateProperties);
427    }
428
429    _getAssignableProperties () {
430        return [
431            { name: 'selector', init: initSelector, required: true },
432        ];
433    }
434}
435
436export class SwitchToMainWindowCommand extends ActionCommandBase {
437    static methodName = camelCase(TYPE.switchToMainWindow);
438
439    constructor () {
440        super();
441        this.type = TYPE.switchToMainWindow;
442    }
443}
444
445export class OpenWindowCommand extends ActionCommandBase {
446    static methodName = camelCase(TYPE.openWindow);
447
448    constructor (obj, testRun, validateProperties) {
449        super(obj, testRun, TYPE.openWindow, validateProperties);
450    }
451
452    _getAssignableProperties () {
453        return [
454            { name: 'url', type: urlArgument },
455        ];
456    }
457}
458
459export class CloseWindowCommand extends ActionCommandBase {
460    static methodName = camelCase(TYPE.closeWindow);
461
462    constructor (obj, testRun, validateProperties) {
463        super(obj, testRun, TYPE.closeWindow, validateProperties);
464    }
465
466    _getAssignableProperties () {
467        return [
468            { name: 'windowId', type: nullableStringArgument, required: true },
469        ];
470    }
471}
472
473
474export class GetCurrentWindowCommand extends ActionCommandBase {
475    static methodName = camelCase(TYPE.getCurrentWindow);
476
477    constructor (obj, testRun, validateProperties) {
478        super(obj, testRun, TYPE.getCurrentWindow, validateProperties);
479    }
480
481    _getAssignableProperties () {
482        return [
483        ];
484    }
485}
486
487export class GetCurrentWindowsCommand extends ActionCommandBase {
488    static methodName = camelCase(TYPE.getCurrentWindows);
489
490    constructor (obj, testRun, validateProperties) {
491        super(obj, testRun, TYPE.getCurrentWindows, validateProperties);
492    }
493
494    _getAssignableProperties () {
495        return [
496        ];
497    }
498}
499
500
501export class SwitchToWindowCommand extends ActionCommandBase {
502    static methodName = camelCase(TYPE.switchToWindow);
503
504    constructor (obj, testRun, validateProperties) {
505        super(obj, testRun, TYPE.switchToWindow, validateProperties);
506    }
507
508    _getAssignableProperties () {
509        return [
510            { name: 'windowId', type: nonEmptyStringArgument, required: true },
511        ];
512    }
513}
514
515export class SwitchToWindowByPredicateCommand extends ActionCommandBase {
516    static methodName = camelCase(TYPE.switchToWindow);
517
518    constructor (obj, testRun, validateProperties) {
519        super(obj, testRun, TYPE.switchToWindowByPredicate, validateProperties);
520    }
521
522    _getAssignableProperties () {
523        return [
524            { name: 'id', type: nonEmptyStringArgument, required: false },
525            { name: 'checkWindow', type: functionArgument, required: true },
526        ];
527    }
528}
529
530export class SwitchToParentWindowCommand extends ActionCommandBase {
531    static methodName = camelCase(TYPE.switchToParentWindow);
532
533    constructor (obj, testRun, validateProperties) {
534        super(obj, testRun, TYPE.switchToParentWindow, validateProperties);
535    }
536
537    _getAssignableProperties () {
538        return [
539        ];
540    }
541}
542
543export class SwitchToPreviousWindowCommand extends ActionCommandBase {
544    static methodName = camelCase(TYPE.switchToPreviousWindow);
545
546    constructor (obj, testRun, validateProperties) {
547        super(obj, testRun, TYPE.switchToPreviousWindow, validateProperties);
548    }
549
550    _getAssignableProperties () {
551        return [];
552    }
553}
554
555export class SetNativeDialogHandlerCommand extends ActionCommandBase {
556    static methodName = camelCase(TYPE.setNativeDialogHandler);
557
558    constructor (obj, testRun, validateProperties) {
559        super(obj, testRun, TYPE.setNativeDialogHandler, validateProperties);
560    }
561
562    _getAssignableProperties () {
563        return [
564            { name: 'dialogHandler', init: initDialogHandler, required: true },
565        ];
566    }
567
568    static from (val) {
569        const dialogHandlerStub = {
570            dialogHandler: { fn: null },
571        };
572
573        const command = new SetNativeDialogHandlerCommand(dialogHandlerStub);
574
575        command.dialogHandler = val.dialogHandler;
576
577        return command;
578    }
579}
580
581export class GetNativeDialogHistoryCommand extends ActionCommandBase {
582    static methodName = camelCase(TYPE.getNativeDialogHistory);
583
584    constructor () {
585        super();
586        this.type = TYPE.getNativeDialogHistory;
587    }
588}
589
590export class GetBrowserConsoleMessagesCommand extends ActionCommandBase {
591    static methodName = camelCase(TYPE.getBrowserConsoleMessages);
592
593    constructor () {
594        super();
595        this.type = TYPE.getBrowserConsoleMessages;
596    }
597}
598
599export class SetTestSpeedCommand extends ActionCommandBase {
600    static methodName = camelCase(TYPE.setTestSpeed);
601
602    constructor (obj, testRun, validateProperties) {
603        super(obj, testRun, TYPE.setTestSpeed, validateProperties);
604    }
605
606    _getAssignableProperties () {
607        return [
608            { name: 'speed', type: setSpeedArgument, required: true },
609        ];
610    }
611}
612
613export class SetPageLoadTimeoutCommand extends ActionCommandBase {
614    static methodName = camelCase(TYPE.setPageLoadTimeout);
615
616    constructor (obj, testRun, validateProperties) {
617        super(obj, testRun, TYPE.setPageLoadTimeout, validateProperties);
618    }
619
620    _getAssignableProperties () {
621        return [
622            { name: 'duration', type: positiveIntegerArgument, required: true },
623        ];
624    }
625}
626
627export class UseRoleCommand extends ActionCommandBase {
628    static methodName = camelCase(TYPE.useRole);
629
630    constructor (obj, testRun, validateProperties) {
631        super(obj, testRun, TYPE.useRole, validateProperties);
632    }
633
634    _getAssignableProperties () {
635        return [
636            { name: 'role', type: actionRoleArgument, required: true },
637        ];
638    }
639}
640
641export class CloseChildWindowOnFileDownloading extends ActionCommandBase {
642    static methodName = camelCase(TYPE.closeChildWindowOnFileDownloading);
643
644    constructor (obj, testRun, validateProperties) {
645        super(obj, testRun, TYPE.closeChildWindowOnFileDownloading, validateProperties);
646    }
647}
648
649export class RecorderCommand extends ActionCommandBase {
650    static methodName = camelCase(TYPE.recorder);
651
652    constructor (obj, testRun) {
653        super(obj, testRun, TYPE.recorder);
654    }
655
656    _getAssignableProperties () {
657        return [
658            { name: 'subtype', type: nonEmptyStringArgument, required: true },
659            { name: 'forceExecutionInTopWindowOnly', type: booleanArgument, defaultValue: false },
660        ];
661    }
662}
663
664export class GetCookiesCommand extends ActionCommandBase {
665    static methodName = camelCase(TYPE.getCookies);
666
667    constructor (obj, testRun, validateProperties) {
668        super(obj, testRun, TYPE.getCookies, validateProperties);
669    }
670
671    _getAssignableProperties () {
672        return [
673            { name: 'urls', type: urlsArgument, required: false },
674            { name: 'cookies', type: cookiesArgument, init: initCookiesOption, required: false },
675        ];
676    }
677}
678
679export class SetCookiesCommand extends ActionCommandBase {
680    static methodName = camelCase(TYPE.setCookies);
681
682    constructor (obj, testRun, validateProperties) {
683        super(obj, testRun, TYPE.setCookies, validateProperties);
684    }
685
686    _getAssignableProperties () {
687        return [
688            { name: 'url', type: urlsArgument, required: false },
689            { name: 'cookies', type: setCookiesArgument, init: initCookiesOption, required: true },
690        ];
691    }
692}
693
694export class DeleteCookiesCommand extends ActionCommandBase {
695    static methodName = camelCase(TYPE.deleteCookies);
696
697    constructor (obj, testRun, validateProperties) {
698        super(obj, testRun, TYPE.deleteCookies, validateProperties);
699    }
700
701    _getAssignableProperties () {
702        return [
703            { name: 'urls', type: urlsArgument, required: false },
704            { name: 'cookies', type: cookiesArgument, init: initCookiesOption, required: false },
705        ];
706    }
707}
708
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)