Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Test Structure and Hooks

Playwright Test Structure and Hooks

This chapter explains how Playwright tests are organized, including test grouping, execution control, and the use of lifecycle hooks.


test.describe() test grouping

test.describe() Used to organize related tests together into logical groups.

Example

// File path: tests/describe-demo.spec.ts

import

{

test

,

expect

}

from

'@playwright/test'

;

// First-level group

test.

describe

(

'RUNOOB homepage tests'

,

(

)

=>

{

  test

(

'Page title is correct'

,

async

(

{

page

}

)

=>

{

    await page.

goto

(

'https://www.runoob.com/'

)

;

    await expect

(

page

)

.

toHaveTitle

(

/RUNOOB/

)

;

 

}

)

;

  test

(

'Navigation bar exists'

,

async

(

{

page

}

)

=>

{

    await page.

goto

(

'https://www.runoob.com/'

)

;

   

// Assert that navigation elements exist

    await expect

(

page.

locator

(

'nav'

)

)

.

toBeVisible

(

)

;

 

}

)

;

}

)

;

// You can define multiple describe blocks

test.

describe

(

'Search function tests'

,

(

)

=>

{

  test

(

'Search box is visible'

,

async

(

{

page

}

)

=>

{

    await page.

goto

(

'https://www.runoob.com/'

)

;

    await expect

(

      page.

getByPlaceholder

(

'Search'

)

   

)

.

toBeVisible

(

)

;

 

}

)

;

}

)

;

The test report is displayed grouped by describe for easy viewing of results.


Nested describe

test.describe() can be nested to form a tree structure:

Example

test.

describe

(

'User module'

,

(

)

=>

{

  test.

describe

(

'Login feature'

,

(

)

=>

{

    test

(

'Login with correct account and password'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

    test

(

'Login failed with wrong password'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

 

}

)

;

  test.

describe

(

'Registration function'

,

(

)

=>

{

    test

(

'Register with correct information'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

    test

(

'Registration fails with an existing email'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

 

}

)

;

}

)

;


test.skip() skip test

test.skip() Used to skip a test; skipped tests will not execute.

Example

// Skip directly (often used to temporarily disable failing feature tests)

test.

skip

(

'Incomplete feature test'

,

async

(

{

page

}

)

=>

{

 

// This test will not execute

}

)

;

// Conditional skip (determined at runtime)

test

(

'Skip under certain conditions'

,

async

(

{

page

,

browserName

}

)

=>

{

 

// Skip in Firefox

  test.

skip

(

browserName

===

'firefox'

,

'Firefox does not yet support this feature'

)

;

 

// Test logic executes only in non-Firefox browsers

  await page.

goto

(

'https://www.runoob.com/'

)

;

}

)

;


test.fail() mark as expected failure

test.fail() Used to mark a test that is known to fail.

Example

// Mark as expected failure (will not be reported as a test failure)

test.

fail

(

'Known bug: search function not yet implemented'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// This assertion is expected to fail

  await expect

(

page.

locator

(

'.search-results'

)

)

.

toBeVisible

(

)

;

}

)

;

// If the test unexpectedly passes, it will be reported as a failure

// This reminds you "the bug is fixed; you can remove the test.fail marker"

test.fail() serves as a reverse marker: test failure = as expected, test pass = abnormal (the marker may need to be removed).


test.only() run only the current test

test.only() Used to run only specific tests during development and debugging, ignoring other tests in the file.

Example

// During development and debugging, run only the current test

test.

only

(

'I am debugging this test'

,

async

(

{

page

}

)

=>

{

 

// Only this test will run

  await page.

goto

(

'https://www.runoob.com/'

)

;

}

)

;

test

(

'This test will be ignored'

,

async

(

{

page

}

)

=>

{

 

// Since test.only() is present above, this will not run

}

)

;

test.only() is a debugging tool and should not be committed to the code repository. If you set playwright.config.ts in the forbidOnly: true, CI will detect only and fail the build.


test.fixme() mark as to be fixed

test.fixme() The difference between test.skip() Similar, but semantically means "this test needs to be fixed".

Example

// Mark as needing fixes (will not run)

test.

fixme

(

'Login flow needs updating due to API changes'

,

async

(

{

page

}

)

=>

{

 

// This test will not execute for now

}

)

;


test.slow() mark as slow test

test.slow() Increases the test's timeout by 3 times.

Example

// Mark as slow test, tripling the timeout

test.

slow

(

'Large data volume test'

,

async

(

{

page

}

)

=>

{

 

// The default timeout for this test will be 90 seconds (30s x 3)

 

// Suitable for scenarios requiring large data loads or complex computations

}

)

;


Test hooks (Hooks)

Hook functions let you perform setup and cleanup at different stages of a test.

test.beforeEach() and test.afterEach()

beforeEach executes before each test,afterEach executes after each test.

Example

// File path: tests/hooks-demo.spec.ts

import

{

test

,

expect

}

from

'@playwright/test'

;

test.

beforeEach

(

async

(

{

page

}

)

=>

{

 

// Navigate to the same page before every test

  await page.

goto

(

'https://www.runoob.com/'

)

;

}

)

;

test.

afterEach

(

async

(

{

page

}

)

=>

{

 

// Perform cleanup after every test (e.g., clear created data)

 

// Playwright automatically cleans up the browser context,

 

// the cleanup here mainly refers to server-side state

  console.

log

(

'Test complete, starting cleanup...'

)

;

}

)

;

test

(

'First test'

,

async

(

{

page

}

)

=>

{

 

// page is already on the RUNOOB homepage

  await expect

(

page

)

.

toHaveTitle

(

/RUNOOB/

)

;

}

)

;

test

(

'Second test'

,

async

(

{

page

}

)

=>

{

 

// page is also already on the RUNOOB homepage

 

// This is a completely new context; the previous test's Cookie/Storage will not persist

  await expect

(

page.

locator

(

'nav'

)

)

.

toBeVisible

(

)

;

}

)

;

test.beforeAll() and test.afterAll()

beforeAll executes once before all tests in the current scope,afterAll executes once after all tests.

Example

test.

describe

(

'User management'

,

(

)

=>

{

  test.

beforeAll

(

async

(

{

browser

}

)

=>

{

   

// Create a test user before all tests (executed only once)

   

// Note: beforeAll cannot use the page fixture

 

}

)

;

  test.

afterAll

(

async

(

{

browser

}

)

=>

{

   

// Delete the test user after all tests (executed only once)

 

}

)

;

  test

(

'View user list'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

  test

(

'Edit user information'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

}

)

;

beforeAll and afterAll cannot access page fixture, because page is independent for each test. If you need to work with the page, use beforeEach。


Hook scope and execution order

A hook's scope depends on where it is located:

Example

// File-level hook -- applies to the entire file

test.

beforeEach

(

async

(

)

=>

{

  console.

log

(

'File-level beforeEach'

)

;

}

)

;

test.

describe

(

'Group A'

,

(

)

=>

{

 

// Group-level hook -- applies only to Group A

  test.

beforeEach

(

async

(

)

=>

{

    console.

log

(

'Group A's beforeEach'

)

;

 

}

)

;

  test

(

'Test A1'

,

async

(

)

=>

{

/* ... */

}

)

;

  test

(

'Test A2'

,

async

(

)

=>

{

/* ... */

}

)

;

}

)

;

test.

describe

(

'Group B'

,

(

)

=>

{

 

// Group B has no hooks of its own; only the file-level hook applies

  test

(

'Test B1'

,

async

(

)

=>

{

/* ... */

}

)

;

}

)

;

Execution order: outer hooks execute first, inner hooks execute later.

For A1 and A2: first output "文件级 beforeEach", then output "分组 A 的 beforeEach"。

For B1: only output "文件级 beforeEach"。