How to Find Tests That Depend on Previous Tests in Playwright



Have you ever executed a Playwright test individually and noticed that it fails?

But when you execute the complete test suite, the same test passes.

This situation creates confusion for automation engineers.

You might start checking:

  • Locators

  • Assertions

  • Timeouts

  • Browser settings

  • Application behaviour

But sometimes, the actual problem is much simpler.

The test depends on another test that executed before it.

For example:

Test 1 → Creates a new customer.

Test 2 → Updates the customer.

Test 3 → Verifies the customer.

If Test 2 is executed without Test 1, the customer might not exist.

This is called test dependency or order dependency.

Finding these hidden dependencies is important when building a reliable Playwright automation framework.

In this article, you will learn how to identify dependent tests, understand why they occur, debug them and fix them using practical Playwright examples.

What Does It Mean When a Test Depends on Another Test?

A test depends on another test when it needs something created, modified or prepared by a previous test to execute successfully.

For example:

test('Create customer', async ({ page }) => {


  // Creates a customer


});


test('Update customer', async ({ page }) => {


  // Assumes the customer already exists


});

The second test assumes that the first test has already created the customer.

If the first test does not execute, the second test may fail.

This creates an unnecessary relationship between two test cases.

A good test should normally prepare its own required conditions.

What Are the Warning Signs of Test Dependencies?

Before fixing a problem, you need to recognize it.

Here are common warning signs.

1. A Test Passes in the Full Suite but Fails Individually

This is one of the most common signs.

Example:

Full execution:

Test A → Passed

Test B → Passed

Test C → Passed

Individual execution:

Test C → Failed

Possible reason:

Test C might depend on data created by Test A or Test B.

However, this is not the only possible explanation. Environment differences, timing problems and shared backend state can produce similar symptoms.

2. Changing the Test Order Changes the Result

Suppose your tests execute in this order:

Login → Add Product → Checkout

Everything passes.

But when Checkout executes first, it fails.

This indicates that Checkout may be relying on something created by an earlier test.

3. Tests Fail When Running in Parallel

Your tests pass when executed with one worker.

But they fail when executed with four workers.

Possible reason:

Two tests might be modifying the same customer account, product or database record.

4. A Test Passes Only After Another Test Runs

For example:

The profile update test fails initially.

You execute the registration test.

Then the profile update test passes.

This suggests that registration might be preparing data required by the profile test.

5. Test Results Change Between Different Execution Environments

Your local machine shows successful results.

But your CI pipeline reports failures.

Shared data, authentication state, execution timing and environment configuration may be different.

You should compare the conditions rather than immediately changing the timeout.

How to Find Tests That Depend on Previous Tests in Playwright

Let's understand a practical debugging process.

Step 1: Run the Suspicious Test Individually

First, identify the test that behaves differently.

Execute:

npx playwright test tests/profile.spec.ts

If the test fails individually but passes in the full suite, investigate its setup and dependencies.

Do not immediately assume that the test runner is executing incorrectly.

Step 2: Execute the Test With a Specific Title

Use the -g option:

npx playwright test -g "Update customer profile"

This helps you execute a specific test without running unrelated test cases.

Observe:

  • Does the required customer exist?

  • Is the user logged in?

  • Is the correct application state available?

  • Does the test create its own required data?

Step 3: Execute the Test File Without Other Files

Sometimes the dependency exists between tests in the same file.

Run:

npx playwright test tests/profile.spec.ts

Then compare that result with individual execution.

This helps narrow down the problem.

Step 4: Use List Mode to Inspect the Test Suite

Playwright provides a useful command:

npx playwright test --list

This lists the tests discovered by Playwright without executing them.

You can use it to understand:

  • Which tests are included

  • Which files contain them

  • Whether expected tests are discovered

  • How the suite is organized

Remember that listing tests does not reveal every hidden dependency. It is an inspection step, not an automatic dependency detector.

Step 5: Execute Tests With One Worker

Run:

npx playwright test --workers=1

This reduces concurrency and helps identify problems related to parallel execution.

If the tests pass with one worker but fail with multiple workers, investigate shared resources and data collisions.

However, one worker does not guarantee that tests are independent.

Tests can still depend on previous tests when executed sequentially.

Step 6: Execute Tests With Multiple Workers

Now try:

npx playwright test --workers=4

Compare the results.

Execution

Result

Possible Investigation

Individual test

Failed

Missing setup or environment issue

Full suite

Passed

Possible order dependency

One worker

Passed

Sequential shared state

Four workers

Failed

Shared resource or race condition

These are diagnostic clues, not absolute proof.

Step 7: Change the Test Execution Order

A useful way to expose hidden dependencies is to run tests under different conditions.

For example:

npx playwright test --repeat-each=5

This repeats each test multiple times.

You can also use:

npx playwright test --fully-parallel

This enables more parallel execution.

Important: --fully-parallel changes execution behaviour. Use it as a diagnostic only when the test environment can safely handle concurrent execution.

If the results change significantly, inspect the shared state and data used by the tests.

Practical Example: Finding a Hidden Test Dependency

Imagine an e-commerce application.

You have two tests.

Test 1 creates an order.

Test 2 checks the order details.

Here is the problematic implementation:

import { test, expect } from '@playwright/test';


test('Create order', async ({ page }) => {


  await page.goto('/checkout');


  await page.getByRole('button', { name: 'Place Order' }).click();


  await expect(

    page.getByText('Order successful')

  ).toBeVisible();


});


test('Verify order details', async ({ page }) => {


  await page.goto('/orders');


  await expect(

    page.getByText('Order successful')

  ).toBeVisible();


});

What is the problem?

The second test assumes that an order already exists.

If the order creation test has not executed, the verification test may fail.

How to Identify the Dependency

Execute:

npx playwright test -g "Verify order details"

If it fails because the expected order does not exist, investigate the test setup.

The second test is relying on the first test's side effect.

How to Fix the Dependency

Prepare an order specifically for the verification test.

For example, use a fixture or API helper to create the order before the test executes.

import { test, expect } from '@playwright/test';


test('Verify order details', async ({ page }) => {


  const order = await createTestOrder();


  await page.goto(`/orders/${order.id}`);


  await expect(

    page.getByText(order.reference)

  ).toBeVisible();


  await deleteTestOrder(order.id);


});

Here, createTestOrder() and deleteTestOrder() are application-specific helpers.

The test now creates its own starting conditions.

It does not need the order creation test to execute first.

For production-quality implementation, use a fixture with guaranteed cleanup, including when assertions fail.

How to Find Dependencies in Test Code During Code Review

You can identify many hidden dependencies simply by reading the test files.

Look for these patterns.

Pattern 1: Global Variables

Example:

let userId: string;


test('Create user', async () => {


  userId = 'USER123';


});


test('Update user', async () => {


  console.log(userId);


});

The second test depends on a value assigned by the first.

Pattern 2: Shared User Accounts

Example:

const email = 'commonuser@example.com';

If multiple tests change the same account, their results may interfere with each other.

Pattern 3: Shared Files

Two tests write to the same file.

One test overwrites the information created by another.

Use test-specific output paths where possible.

Pattern 4: Reusing Application State

One test logs in, another assumes the user is already authenticated, and another assumes the session is still active.

Playwright's default BrowserContext isolation helps prevent accidental browser-session sharing, but custom shared contexts or reused authentication state can introduce dependencies.

Pattern 5: Uncontrolled Database Changes

One test creates a record.

Another test deletes it.

A third test assumes it still exists.

This creates unpredictable results.

How to Use Playwright Fixtures to Prevent Dependencies

Fixtures are useful when multiple tests require similar setup.

For example, imagine several tests need an authenticated user.

Instead of depending on a login test to run first, create an authentication fixture.

import { test as base, expect } from '@playwright/test';


export const test = base.extend({


  authenticatedPage: async ({ page }, use) => {


    await page.goto('/login');


    await page.getByLabel('Email').fill(

      process.env.TEST_EMAIL!

    );


    await page.getByLabel('Password').fill(

      process.env.TEST_PASSWORD!

    );


    await page.getByRole('button', { name: 'Login' }).click();


    await use(page);


  }


});


export { expect };

A test can now use this fixture:

import { test, expect } from './fixtures';


test('Open customer dashboard', async ({ authenticatedPage }) => {


  await expect(

    authenticatedPage.getByRole('heading', {

      name: 'Dashboard'

    })

  ).toBeVisible();


});

Each test using the fixture receives its own setup.

The credentials must be stored securely in environment variables. The example also assumes the application has the specified login page and controls.

How to Debug Hidden Dependencies Using Trace Viewer

Playwright Trace Viewer helps you inspect what happened during test execution.

It can provide:

  • Actions

  • Screenshots

  • DOM snapshots

  • Network information

  • Errors

  • Test execution details

Enable tracing in your Playwright configuration:

import { defineConfig } from '@playwright/test';


export default defineConfig({


  use: {


    trace: 'retain-on-failure'


  }


});

Execute your tests.

If a test fails, inspect the generated trace.

You can open the report:

npx playwright show-report

Select the failed test and inspect its trace when available.

Ask yourself:

  1. Did the test navigate to the expected page?

  2. Was the required record available?

  3. Was the user authenticated?

  4. Did an earlier action change the application?

  5. Was the expected element present?

  6. Did the application return the expected response?

Trace Viewer helps explain the failure, but identifying the actual dependency may also require checking database records, API responses or application logs.

How to Find Test Dependencies in CI/CD Pipelines

Hidden dependencies sometimes become visible only in CI/CD.

For example:

Local execution → Passed

CI execution → Failed

Possible reasons include:

  • Different worker counts

  • Different environment variables

  • Different test data

  • Parallel execution

  • Database cleanup

  • Network conditions

  • Browser version differences

Follow this debugging process:

Step 1: Identify the failed test.

Step 2: Inspect the CI execution report.

Step 3: Download and inspect the trace.

Step 4: Execute the failed test locally.

Step 5: Run the test with the same worker configuration.

Step 6: Compare environment and test data.

Step 7: Fix the underlying dependency.

Do not simply increase the CI timeout without identifying the cause.

A Practical Checklist for Finding Dependent Tests

Before approving a Playwright test suite, check:

Check

What to Verify

Individual execution

Does each test pass alone?

Data setup

Does each test prepare its own data?

Authentication

Does each test control its login state?

Global variables

Are mutable shared values avoided?

Database

Are records isolated?

Files

Are file paths unique?

Parallel execution

Can tests run safely together?

Test order

Does changing execution order affect results?

Cleanup

Are resources cleaned up safely?

Debugging

Are traces available for failures?

Frequently Asked Questions

1. How do I find tests that depend on previous tests in Playwright?

Run suspicious tests individually, execute the suite with different worker counts, inspect test data and check whether changing execution order changes the results.

2. Why does my Playwright test pass in the suite but fail individually?

The test may depend on data, authentication or application state created by another test. Environment and timing differences can also cause this behaviour.

3. How do I run one test in Playwright?

Use:

npx playwright test -g "Test Name"

4. How do I list all Playwright tests?

Use:

npx playwright test --list

5. How do I run Playwright tests sequentially?

Use:

npx playwright test --workers=1

This limits worker concurrency. It does not fix dependencies.

6. How do I run Playwright tests in parallel?

Use:

npx playwright test --workers=4

The number of workers can be adjusted according to your environment.

7. Can Playwright automatically identify every dependent test?

No. Playwright provides execution and debugging tools, but identifying hidden dependencies often requires examining test code, test data, application state and execution behaviour.

8. What is the best way to prevent test dependencies?

Prepare test data independently, use fixtures, avoid shared mutable state and ensure each test controls its own starting conditions.

9. Can Trace Viewer help identify test dependencies?

Yes. It provides execution evidence that helps investigate unexpected state, navigation, actions and failures. External data dependencies may require additional investigation.

10. Should dependent tests be executed in serial mode?

Only when ordered execution is genuinely required. Independent test design is generally preferable.

Final Thoughts

Finding dependent tests is an important skill for every Playwright automation engineer.

When a test passes in the complete suite but fails individually, do not immediately change locators or increase timeouts.

First investigate its starting conditions.

Check its data, authentication, shared resources and previous execution history.

Use individual execution, worker configuration, test reports and Trace Viewer to understand the problem.

Most importantly, design every test so that it can prepare what it needs.

A reliable Playwright test should verify application behaviour, not depend on the success of another test.

To understand how these practices fit into a complete automation framework, explore the Playwright Framework guide at PlaywrightMasters.


Comments

Popular posts from this blog

How to Run Only Smoke Tests in Playwright: Step-by-Step Guide

How to Test REST APIs Using Playwright

Playwright Multiple Tabs and Windows Explained: A Practical Guide