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

execution-context.js

Source: execution-context.js Github

copy
1import { createContext } from 'vm';
2import Module from 'module';
3import path from 'path';
4import exportableLib from '../exportable-lib';
5
6const OPTIONS_KEY      = Symbol('options');
7const NODE_MODULES_DIR = 'node_modules';
8
9function getModuleBasePaths (currentPath) {
10    const nodePaths = [];
11    let parentDir   = path.dirname(currentPath);
12
13    while (currentPath !== parentDir) {
14        currentPath = parentDir;
15        parentDir   = path.dirname(currentPath);
16
17        nodePaths.push(path.join(currentPath, NODE_MODULES_DIR));
18    }
19
20    return nodePaths;
21}
22
23function createRequire (filename) {
24    //Deprecated since: Node v12.2.0
25    if (Module.createRequireFromPath)
26        return Module.createRequireFromPath(filename);
27
28    if (Module.createRequire)
29        return Module.createRequire(filename);
30
31    const dummyModule          = new Module(filename, module);
32    const localModulesPaths    = getModuleBasePaths(filename);
33
34    dummyModule.filename = filename;
35    dummyModule.paths    = localModulesPaths.concat(module.paths);
36
37    return id => dummyModule.require(id);
38}
39
40function createSelectorDefinition (testRun) {
41    return (fn, options = {}) => {
42        const { skipVisibilityCheck, collectionMode } = testRun.controller.getExecutionContext()[OPTIONS_KEY];
43
44        if (skipVisibilityCheck)
45            options.visibilityCheck = false;
46
47        if (testRun && testRun.id)
48            options.boundTestRun = testRun;
49
50        if (collectionMode)
51            options.collectionMode = collectionMode;
52
53        return exportableLib.Selector(fn, options);
54    };
55}
56
57function createClientFunctionDefinition (testRun) {
58    return (fn, options = {}) => {
59        if (testRun && testRun.id)
60            options.boundTestRun = testRun;
61
62        return exportableLib.ClientFunction(fn, options);
63    };
64}
65
66export function setContextOptions (context, options) {
67    context[OPTIONS_KEY] = options;
68}
69
70export function createExecutionContext (testRun) {
71    const filename = testRun.test.testFile.filename;
72
73    const replacers = {
74        require:        createRequire(filename),
75        __filename:     filename,
76        __dirname:      path.dirname(filename),
77        t:              testRun.controller,
78        Selector:       createSelectorDefinition(testRun),
79        ClientFunction: createClientFunctionDefinition(testRun),
80        Role:           exportableLib.Role,
81        RequestLogger:  exportableLib.RequestLogger,
82        RequestMock:    exportableLib.RequestMock,
83        RequestHook:    exportableLib.RequestHook
84    };
85
86    return createContext(new Proxy(replacers, {
87        get: (target, property) => {
88            if (replacers.hasOwnProperty(property))
89                return replacers[property];
90
91            if (global.hasOwnProperty(property))
92                return global[property];
93
94            throw new Error(`${property} is not defined`);
95        }
96    }));
97}
98
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)