Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit cce2727

Browse files
mmarkelovmxschmitt
andauthored
Add resetPage and resetBrowser (#184)
* Add resetPage * Add resetBrowser * Add Readme * Update README.md * Add resetContext * Add resetContext to readme Co-authored-by: Max Schmitt <max@schmitt.mx>
1 parent f33278d commit cce2727

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,38 @@ Debugging tests can be hard sometimes and it is very useful to be able to pause
148148
await jestPlaywright.debug()
149149
```
150150

151+
## Reset helper functions
152+
153+
### Reset current page
154+
155+
```js
156+
beforeEach(async () => {
157+
await jestPlaywright.resetPage()
158+
})
159+
```
160+
161+
To create a new page for each test, you can use this snippet to have a new page object for each individual test.
162+
163+
### Reset current context
164+
165+
```js
166+
beforeEach(async () => {
167+
await jestPlaywright.resetContext()
168+
})
169+
```
170+
171+
To create a new context for each test, you can use this snippet to have a new context object for each individual test.
172+
173+
### Reset current browser
174+
175+
```js
176+
beforeEach(async () => {
177+
await jestPlaywright.resetBrowser()
178+
})
179+
```
180+
181+
You can use this snippet to reset current browser for each individual test. It will reset browser, context and page.
182+
151183
## Tracking the coverage
152184

153185
It's possible to track the coverage of the end-to-end tests with the [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) Babel plugin configured. It needs to be included in the web application which you are gonna test otherwise it won't work. To use it, you have to set `collectCoverage` in the `jest-playwright.config.js` to `true`. Per default the test coverage will be automatically saved after each navigation change (`beforeunload` event). If a certain code path is not covered, you can manually call and add the corresponding `saveCoverage(page)` call to your tests like that:

src/PlaywrightEnvironment.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,48 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
153153
this.global.it = it
154154
this.global.test = test
155155
},
156+
resetPage: async (): Promise<void> => {
157+
const { context, page } = this.global
158+
if (page) {
159+
page.removeListener('pageerror', handleError)
160+
await page.close()
161+
}
162+
163+
this.global.page = await context.newPage()
164+
if (exitOnPageError) {
165+
this.global.page.addListener('pageerror', handleError)
166+
}
167+
},
168+
resetContext: async (): Promise<void> => {
169+
const { browser, context } = this.global
170+
171+
if (context) {
172+
await context.close()
173+
}
174+
175+
this.global.context = await browser.newContext(contextOptions)
176+
177+
await this.global.jestPlaywright.resetPage()
178+
},
179+
resetBrowser: async (): Promise<void> => {
180+
const { browser } = this.global
181+
182+
if (browser) {
183+
await browser.close()
184+
}
185+
186+
this.global.browser = await getBrowserPerProcess(
187+
playwrightInstance,
188+
browserType,
189+
this._jestPlaywrightConfig,
190+
)
191+
192+
this.global.context = await this.global.browser.newContext(
193+
contextOptions,
194+
)
195+
196+
await this.global.jestPlaywright.resetPage()
197+
},
156198
debug: async (): Promise<void> => {
157199
// Run a debugger (in case Playwright has been launched with `{ devtools: true }`)
158200
await this.global.page.evaluate(() => {

types/global.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@ import type { SkipOption } from '../src/types'
33

44
interface JestPlaywright {
55
skip: (skipOptions: SkipOption, callback: Function) => void
6+
/**
7+
* Reset global.page
8+
*
9+
* ```ts
10+
* it('should reset page', async () => {
11+
* await jestPlaywright.resetPage()
12+
* })
13+
* ```
14+
*/
15+
resetPage: () => Promise<void>
16+
/**
17+
* Reset global.context
18+
*
19+
* ```ts
20+
* it('should reset context', async () => {
21+
* await jestPlaywright.resetContext()
22+
* })
23+
* ```
24+
*/
25+
resetContext: () => Promise<void>
26+
/**
27+
* Reset global.browser, global.context, and global.page
28+
*
29+
* ```ts
30+
* it('should reset page', async () => {
31+
* await jestPlaywright.resetBrowser()
32+
* })
33+
* ```
34+
*/
35+
resetBrowser: () => Promise<void>
636
/**
737
* Suspends test execution and gives you opportunity to see what's going on in the browser
838
* - Jest is suspended (no timeout)

0 commit comments

Comments
 (0)