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

Playwright Test Isolation and Browser Context

Playwright Test Isolation and Browser Context

This chapter introduces Playwright's test isolation mechanism and the concept of Browser Context, which are key designs for ensuring stable and reliable tests.


What is test isolation

Test isolation means each test case runs in a completely independent environment without affecting one another.

In Playwright, each test automatically gets an independent Browser Context.


What is a Browser Context

A Browser Context is equivalent to a brand-new browser profile.

Creating a Context is like opening a brand-new browser window, with its own cookies, localStorage, and sessionStorage, but with extremely low overhead (at the millisecond level).

Concept Analogy Description
Browser (browser instance) Opened a browser program High overhead; generally created only once for the entire test run
Browser Context An independent profile within the browser Low overhead; one per test
Page (tab) A tab in the browser Open a page in a Context

In each test, the page and context are all brand-new, so if one test modifies localStorage or cookies, it does not affect other tests.


What Context isolates

The following are completely isolated between Browser Contexts:

Isolated item Description
Cookies Each Context has an independent cookie store
localStorage localStorage is completely isolated
sessionStorage sessionStorage is completely isolated
IndexedDB Browser databases are isolated
Cache HTTP cache is isolated

Example

// Test A sets a cookie

test

(

'Test A'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

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

)

;

 

// Set a cookie (only effective in the current Context)

  await page.

context

(

)

.

addCookies

(

[

{

    name

:

'runoob_session'

,

    value

:

'abc123'

,

    domain

:

'www.runoob.com'

,

    path

:

'/'

,

 

}

]

)

;

 

// Subsequent operations use this cookie

}

)

;

// Test B receives a brand-new Context, without the cookie set by Test A

test

(

'Test B'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

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

)

;

 

// context().cookies() returns an empty array

 

const

cookies

=

await page.

context

(

)

.

cookies

(

)

;

  console.

log

(

cookies

)

;

// []

}

)

;


Using the context fixture

Besides page, the test function can also access the context fixture to perform Context-level operations.

Example

test

(

'Use the context fixture'

,

async

(

{

context

,

page

}

)

=>

{

 

// Set permissions at the Context level

  await context.

grantPermissions

(

[

'geolocation'

]

)

;

  await context.

setGeolocation

(

{

latitude

:

39.9

,

longitude

:

116.4

}

)

;

 

// Intercept network at the Context level

  await context.

route

(

'**/*.{png,jpg,jpeg}'

,

route

=>

route.

abort

(

)

)

;

 

// page is in the same context and inherits the above settings

  await page.

goto

(

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

)

;

}

)

;


Multi-Context scenarios

When you need to simulate multiple independent users within the same test, you can manually create Contexts.

Example

test

(

'Two users operating independently'

,

async

(

{

browser

}

)

=>

{

 

// Create independent Contexts for two users

 

const

userAContext

=

await browser.

newContext

(

)

;

 

const

userBContext

=

await browser.

newContext

(

)

;

 

// The two users' pages are completely isolated

 

const

pageA

=

await userAContext.

newPage

(

)

;

 

const

pageB

=

await userBContext.

newPage

(

)

;

  await pageA.

goto

(

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

)

;

  await pageB.

goto

(

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

)

;

 

// User A logs in

  await pageA.

getByLabel

(

'Username'

)

.

fill

(

'user_a'

)

;

  await pageA.

getByLabel

(

'Password'

)

.

fill

(

'password_a'

)

;

  await pageA.

getByRole

(

'button'

,

{

name

:

'Login'

}

)

.

click

(

)

;

 

// User B logs in (each has independent cookies/session, without affecting one another)

  await pageB.

getByLabel

(

'Username'

)

.

fill

(

'user_b'

)

;

  await pageB.

getByLabel

(

'Password'

)

.

fill

(

'password_b'

)

;

  await pageB.

getByRole

(

'button'

,

{

name

:

'Login'

}

)

.

click

(

)

;

 

// Cleanup

  await userAContext.

close

(

)

;

  await userBContext.

close

(

)

;

}

)

;


Benefits of test isolation

1. Reproducibility

Each test starts from the same initial state and produces consistent results in any order.

2. Preventing cascading failures

A failure in Test A does not cause Test B to fail; each test is independently debuggable.

3. Supporting parallel execution

Because tests are completely independent, Playwright can safely run them in parallel across multiple workers.

Test isolation is Playwright's default behavior; each test() automatically gets an independent page and context, without any extra configuration.