How to use playwrightConfig method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

codecept.conf.js

Source: codecept.conf.js Github

copy
1require('dotenv').config()
2const { setHeadlessWhen } = require('@codeceptjs/configure')
3const config = require('./resources/config')
4const testData = require(`./resources/test_data/${process.env.TEST_ENV}/test_data`)
5const { devices } = require('playwright')
6// https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts
7
8const isHeadless = process.env.HEADLESS === 'true'
9
10const browser = config.browser
11const isMobile = config.isMobile
12const mobileDevice = config.mobileDevice
13
14// turn on headless mode when running with HEADLESS=true environment variable
15// HEADLESS=true npx codecept run
16setHeadlessWhen(isHeadless)
17
18let playwrightConfig = {
19    url: testData.homePageUrl,
20    show: !isHeadless,
21    restart: true,
22    // basicAuth: {
23    //     username: process.env.USERNAME_HOST,
24    //     password: process.env.PASSWORD_HOST,
25    // },
26    waitForNavigation: 'networkidle0',
27    waitForTimeout: 30000,
28    getPageTimeout: 60000,
29    waitForAction: 500,
30}
31
32const mobileConfig = {
33    emulate: devices[mobileDevice],
34}
35
36const webConfig = {
37    windowSize: `${process.env.WINDOWS_WIDTH}x${process.env.WINDOWS_HEIGHT}`,
38}
39
40if (isMobile) {
41    switch (browser) {
42        case 'chromium':
43            playwrightConfig = {
44                ...mobileConfig,
45                ...playwrightConfig,
46                browser: 'chromium',
47                chromium: {
48                    headless: isHeadless,
49                    args: [
50                        '--no-sandbox',
51                        '--disable-dev-shm-usage',
52                        '--disable-setuid-sandbox',
53                    ],
54                },
55            }
56            break
57        case 'webkit':
58            playwrightConfig = {
59                ...mobileConfig,
60                ...playwrightConfig,
61                browser: 'webkit',
62            }
63            break
64        default:
65            throw new Error(
66                'Please setting up environtment BROWSER to webkit or chromium',
67            )
68    }
69} else {
70    switch (browser) {
71        case 'chromium':
72            playwrightConfig = {
73                ...webConfig,
74                ...playwrightConfig,
75                browser: 'chromium',
76                chromium: {
77                    headless: isHeadless,
78                    args: [
79                        `--window-size=${process.env.WINDOWS_WIDTH},${process.env.WINDOWS_HEIGHT}`,
80                        '--no-sandbox',
81                        '--disable-dev-shm-usage',
82                        '--disable-setuid-sandbox',
83                    ],
84                },
85            }
86            break
87        case 'webkit':
88            playwrightConfig = {
89                ...webConfig,
90                ...playwrightConfig,
91                browser: 'webkit',
92            }
93            break
94        default:
95            throw new Error(
96                'Please setting up environtment BROWSER to webkit or chromium',
97            )
98    }
99}
100
101exports.config = {
102    tests: './tests/**/*_test.js',
103    output: './codecept_output',
104    helpers: {
105        Playwright: playwrightConfig,
106        CustomCommands: {
107            require: './helpers/custom_commands_helper.js',
108        },
109        PlaywrightBrowser: {
110            require: './helpers/playwright_browser_helper.js',
111        },
112        ChaiWrapper: {
113            require: './node_modules/codeceptjs-chai',
114        },
115    },
116    include: {
117        config: './resources/config.js',
118        I: './steps_file.js',
119        pages: './pages/pages_import.js',
120        steps: './steps/steps_import.js',
121        api: './api/index.js',
122        translate: `./resources/translation/${process.env.SITE_LANGUAGE}`,
123        testData: `./resources/test_data/${process.env.TEST_ENV}/test_data.js`,
124    },
125    plugins: {
126        allure: {
127            enabled: true,
128            outputDir: './output',
129        },
130        autoDelay: {
131            enabled: true,
132            delayBefore: 1000,
133        },
134        retryFailedStep: {
135            enabled: true,
136            defaultIgnoredSteps: [],
137            ignoredSteps: [
138                'amOnPage',
139                'send*',
140                'execute*',
141                'run*',
142                'assert*',
143                'waitFor*',
144                'waitEmail*',
145            ],
146            minTimeout: 5000,
147        },
148        screenshotOnFail: {
149            enabled: true,
150        },
151        customLocator: {
152            enabled: true,
153            showActual: true,
154            // prefix: '$',
155            // strategy: 'css',
156            attribute: 'data-testid',
157        },
158    },
159    mocha: {},
160    name: 'boilerplate',
161}
162
Full Screen

hooks.js

Source: hooks.js Github

copy
1const { setDefaultTimeout, BeforeAll, Before, After, AfterAll } = require('@cucumber/cucumber');
2const playwright = require('playwright');
3const common = require('./common.js');
4
5
6// Launch options.
7const chromiumConfig = {
8  headless: true,
9  slowMo: 10,
10  chromiumSandbox:false
11};
12const defaultConfig = {
13  headless: true,
14  slowMo: 3,
15  chromiumSandbox:false
16};
17
18const debuggingMode = {
19  headless: false,
20  slowMo: 50,
21};
22
23function debugConfig() {
24  return process.env.DEBUG === 'true' ? debuggingMode : {};
25};
26function playwrightConfig(browserType) {
27  let config = Object.assign(defaultConfig, debugConfig());
28  if (browserType === "chromium")
29    config = Object.assign(chromiumConfig, debugConfig());
30  return config;
31};
32
33Before({timeout: 60 * 1000} , async () => {
34  setDefaultTimeout(60000);
35  console.log("BROWSERTYPE::" + process.env.BROWSERTYPE);
36  let browserType = process.env.BROWSERTYPE === '' || typeof process.env.BROWSERTYPE === 'undefined' ? 'chromium' : process.env.BROWSERTYPE;
37  console.log('Running on browser type: '+browserType)
38  browser = await playwright[browserType].launch(playwrightConfig(browserType));
39  await common.launchBrowser(browser, process.env.URL);
40});
41
42Before({timeout: 60 * 1000} , async () => {
43  //Write code for login
44});
45
46After(async function (scenario){
47  if(scenario.result.status=== 'FAILED'){
48      const screenShotFail= await page.screenshot();
49      this.attach(screenShotFail, "image/png");
50  }  
51  await browser.close();
52});
53
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 Playwright Internal 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)