In this example we will explore how to run playwright spec files in specific order. Playwright does not guarantee test execution order by default, as tests are designed to be independent and parallelizable. However, there are a few approaches you can take to run spec files in a specific order, depending on your use case.
Approaches to run spec files in specific order
In this example we are going to use two approaches to order the execution of spec files in playwright.
1. Use a "test list" file
You can put your tests in helper functions in multiple files. Consider the following example where tests are not defined directly in the file, but rather in a wrapper function.
feature-a.spec.ts
import { test, expect } from '@playwright/test'; export default function createTests() { test('feature-a example test', async ({ page }) => { // ... test goes here }); }
feature-b.spec.ts
import { test, expect } from '@playwright/test'; export default function createTests() { test.use({ viewport: { width: 500, height: 500 } }); test('feature-b example test', async ({ page }) => { // ... test goes here }); }
test.list.ts
import { test } from '@playwright/test'; import featureBTests from './feature-b.spec.ts'; import featureATests from './feature-a.spec.ts'; test.describe(featureBTests); test.describe(featureATests);
Now disable parallel execution by setting workers to one, and specify your test list file in playwright config setting file.
playwright.config.ts
import { defineConfig } from '@playwright/test'; export default defineConfig({ workers: 1, testMatch: 'test.list.ts', });
2. Use a single test file with ordered test steps
If you're testing a flow that depends on sequence (e.g., login → perform action → logout), it’s best to write those steps in a single spec file:
test('Step 1: Login', async ({ page }) => { // Login logic }); test('Step 2: Do something', async ({ page }) => { // Action after login }); test('Step 3: Logout', async ({ page }) => { // Logout logic });
Use the test.describe.serial() block to enforce serial execution:
test.describe.serial('Ordered flow', () => { test('Login', async ({ page }) => { /* ... */ }); test('Action', async ({ page }) => { /* ... */ }); test('Logout', async ({ page }) => { /* ... */ }); });
No comments:
Post a Comment