Search code examples
node.jsplaywrightparallel.foreachplaywright-testparametrized-testing

Executing Parameterized Playwright Tests in Parallel in Node.JS


I am trying to run Parametrized Playwright tests created with Node.js in Parallel.
SRC: https://playwright.dev/docs/test-parameterize
SRC: https://playwright.dev/docs/test-parallel

However, when I combine the two, the test cases run sequentially. Can the parametrised tests be executed in parallel in Playwright (Node.js)?

import fs from 'fs';
import path from 'path';
import { test } from '@playwright/test';
import { parse } from 'csv-parse/sync';

 const records = parse(fs.readFileSync(path.join(__dirname, 
 'input.csv')), {
 columns: true,
 skip_empty_lines: true
});

for (const record of records) {
test(`foo: ${record.test_case}`, async ({ page }) => {
console.log(record.test_case, record.some_value, 
record.some_other_value);
// Test Case Goes Here.
});
}

Solution

  • As default, tests in a single file are run sequentially (source). Add this line to your playwright.config to achieve parallell run of same file tests:

    fullyParallel: true
    

    Demonstrated by doing the following:

    const records = ['a', 'b', 'c']
    for(const record of records){
        test('record ' + record, async({page})=>{
            // Printing timestamp
            console.log(`Record ${record} ${new Date().getTime()}`)
        })
    }
    

    When fullyParallell is true, all 3 tests are ran in parallell (identical timestamp). Remember to set number of workers > 1 in config file. My current config file:

    import {defineConfig, devices} from '@playwright/test'
    import * as dotenv from 'dotenv'
    export default defineConfig({
        testDir: './tests',
        timeout: 5 * 1000,
        expect: {
            timeout: 5000
        },
        fullyParallel: true,
        workers: 10,
        forbidOnly: !!process.env.CI,
        retries: process.env.CI ? 4 : 0,
        reporter: 'list',
        use: {
            actionTimeout: 0,
            trace: 'retain-on-failure'
        },
        projects: [
            {
                name: 'chrome web',
                use: {
                    ...devices['Desktop Chrome']
                }
            }
        ]
    })