Skip to content

Playwright parallelism

Published: at 01:22 PM
Author: Sebastian Bigatton

Playwright test parallelism is easier than Selenium WebDriver.

Playwright supports several ways to handle parallelization. It could be done manually using Playwright API or it offers a built-in support for parallel execution with test runners like Playwright Test.

Parallelization with Playwright Test

Playwright has its own test runner called Playwright Test. When using it, you don’t need to manually manage parallelism; it is built into the framework.

Playwright Test by default multiple tests in parallel distributing automatically the tests across available workers (CPU cores). In addition, each test runs in a separate browser context ensuring isolation. You can configure and control the number of workers or parallel test process by setting workers option in configuration file.

Example of configuration file in playwright.config.ts

module.exports = {
  workers: 4,  // Run tests in 4 parallel workers (you can adjust based on your CPU cores)
  retries: 1,  // Retry failed tests once
  use: {
    headless: true,  // Run tests in headless mode
  },
};

Simply execute following line:

npx playwright test

Selenium parallelism

Selenium does not natively have a test runner or parallel execution mechanism like Playwright Test. For that it requires external tools like TestNG (for Java) or Mocha (for JavaScript) to enable parallel test execution.

Parallelization with TestNG

An XML configuration file needs to be added specifying TestNG to run tests in parallel where:

Example of run test configuration XML file:

<suite name="Selenium Suite" parallel="tests" thread-count="4">
    <test name="Test1">
        <classes>
            <class name="testClass.Test1"/>
        </classes>
    </test>
    <test name="Test2">
        <classes>
            <class name="testClass.Test2"/>
        </classes>
    </test>
    <test name="Test3">
        <classes>
            <class name="testClass.Test3"/>
        </classes>
    </test>
</suite>

Parallelization with Mocha

Selenium can be used in conjunction with a test runner like Mocha to run tests in parallel when you decides to use Javascript scripting language.

Example:

const { Builder } = require('selenium-webdriver');
const { describe, it } = require('mocha');
const { expect } = require('chai');

describe('Parallel Selenium Tests', function() {
  it('should open SynDevs main page', async function() {
    let driver = await new Builder().forBrowser('chrome').build();
    await driver.get('https://www.syndevs.com/');
    let title = await driver.getTitle();
    expect(title).to.equal('SynDevs | Website');
    await driver.quit();
  });

  it('should open SynDevs blog page', async function() {
    let driver = await new Builder().forBrowser('chrome').build();
    await driver.get('https://blog.syndevs.com/');
    let title = await driver.getTitle();
    expect(title).to.equal('SynDevs | Blog');
    await driver.quit();
  });
});

To run in parallel:

npx mocha-parallel-tests

Conclusion

Playwright provides a built-in test runner that handles parallel execution and test isolation automatically making it a good choice for someone looking for simplicity and speed. In the other hand, Selenium offers more flexibility for parallelization but requires external test runners and additional configuration, like TestNG, Mocha or Selenium Grid. It’s better suited for legacy systems, more complex test environments, or cross-browser testing on a variety of platforms.


Previous Post
Web designer in 2024?
Next Post
Apache Airflow (an Introduction)