Playwright Control Test Order

Profile picture for user arilio666

In this article, we will see how we can control test order in playwright. Unless we parallelize tests in a single file, the playwright runs tests from a single file in order of declaration.

Playwright runs tests in parallel by default, and there is no guarantee about the order of execution. If we disable parallel execution, we can control the order of execution by test file naming in alphabetical order or using a test file list.

Alphabetically:

  • After disabling parallel execution, playwright tests now run in alphabetical order.
  • Certain naming conventions can be followed, for example: 
    001-signin.spec.ts, 002-create-User.spec.ts

Test List File:

// feature-a.spec.js
const { test, expect } = require('@playwright/test');
module.exports = function createTests() {
 test('feature-a test', async ({ page }) => {
   // ... text goes here
 });
};

// feature-b.spec.js
const { test, expect } = require('@playwright/test');
module.exports = function createTests() {
 test.use({ viewport: { width: 500, height: 500 } });
 test('feature-b test', async ({ page }) => {
   // ... text goes here
 });
};
  • We can use a test list file that will easily control the test's order.
  • Test.use() affects only tests from a single file.
  • This will run feature-b tests and then feature-a tests.

After creating the tests, do this.

const { test } = require('@playwright/test');
test.describe(require('./feature-b.spec.js'));
test.describe(require('./feature-a.spec.js'));

Now disable parallel execution, set workers to one, and specify the test list file.

const config = {
 workers: 1,
 testMatch: 'test.list.js',
};
module.exports = config;