Playwright Serial Mode

Profile picture for user arilio666

In Playwright, the serial mode is a way of running a script in a sequential, synchronous manner. This means that each step of the script will be executed one at a time, and the script will wait for the completion of each step before moving on to the next one. This is in contrast to parallel mode, where multiple instances of the script can run at the same time, completing different steps simultaneously.

The serial mode can be helpful when you need to ensure that specific steps are completed before moving on to the next step or when you want to debug your script and understand the order of events.

One of the main advantages of Playwright serial mode is that it allows you to run the script in a more predictable and deterministic way, as the execution of the script is not influenced by other factors like network latency or CPU usage.

You can run the script in serial mode by passing the option mode: 'serial' while creating the browser context, like

test.describe.configure({mode: 'serial'})

Playwright serial mode allows you to run scripts sequentially and synchronously, providing a predictable and deterministic way to automate web browsers.

Here's an example of using Playwright in serial mode to automate a simple task on a website:

const { test } = require('@playwright/test');
test.describe.configure({ mode: 'serial' });
/** @type {import('@playwright/test').Page} */
let page;
test.beforeAll(async ({ browser }) => {
 page = await browser.newPage();
});
test.afterAll(async () => {
 await page.close();
});
test('runs first', async () => {
 await page.goto('https://playwright.dev/');
});
test('runs second', async () => {
 await page.getByText('Get Started').click();
});
  • In this, the mode serial is turned on, and all the tests get executed parallelly.

  • If one serial test fails, all subsequent tests are skipped.

  • All tests in a group are specifically retried together.

Example:

 

For real-time example, we are using the programsbuzz site.

const {test, expect} = require('@playwright/test');
test.describe.configure({mode: 'serial'})
let page;

test.beforeAll(async ({ browser }) => {
 page = await browser.newPage();
});
test.afterAll(async () => {
 await page.close();
});
test('runs first', async () => {
 await page.goto('https://www.programsbuzz.com/');
});
test('runs second', async () => {
  await page.locator("i[class='fas fa-search']").click()
});
test('runs third', async () => {
  await page.locator('#edit-keys').type('Playwright')
});
test('runs fourth', async () => {
  await page.locator('#edit-submit').click()
});

We can see here all the tests are run in serial mode, and if any test step should fail, the following steps will be skipped, and thats how serial mode works.<