Skip to content

Commit

Permalink
feat: add jestPuppeteer.resetBrowser method (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored and gregberge committed Jul 14, 2019
1 parent f9d37ef commit ae93739
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@ it('should put test in debug mode', async () => {
})
```

### `global.jestPuppeteer.resetPage()`

Reset global.page

```js
beforeEach(async () => {
await jestPuppeteer.resetPage()
})
```

### `global.jestPuppeteer.resetBrowser()`

Reset global.browser, global.context, and global.page

```js
beforeEach(async () => {
await jestPuppeteer.resetBrowser()
})
```

### `jest-puppeteer.config.js`

You can specify a `jest-puppeteer.config.js` at the root of the project or define a custom path using `JEST_PUPPETEER_CONFIG` environment variable.
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ beforeEach(async () => {
})
```

### `global.jestPuppeteer.resetBrowser()`

Reset global.browser, global.context, and global.page

```js
beforeEach(async () => {
await jestPuppeteer.resetBrowser()
})
```

### `jest-puppeteer.config.js`

You can specify a `jest-puppeteer.config.js` at the root of the project or define a custom path using `JEST_PUPPETEER_CONFIG` environment variable. It should export a config object or a Promise for a config object.
Expand Down
68 changes: 44 additions & 24 deletions packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,6 @@ class PuppeteerEnvironment extends NodeEnvironment {
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
this.global.browser = await puppeteer.connect({
...config.connect,
...config.launch,
browserURL: undefined,
browserWSEndpoint: wsEndpoint,
})

if (config.browserContext === 'incognito') {
// Using this, pages will be created in a pristine context.
this.global.context = await this.global.browser.createIncognitoBrowserContext()
} else if (config.browserContext === 'default' || !config.browserContext) {
/**
* Since this is a new browser, browserContexts() will return only one instance
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
*/
this.global.context = await this.global.browser.browserContexts()[0]
} else {
throw new Error(
`browserContext should be either 'incognito' or 'default'. Received '${
config.browserContext
}'`,
)
}

this.global.jestPuppeteer = {
debug: async () => {
Expand Down Expand Up @@ -109,9 +86,52 @@ class PuppeteerEnvironment extends NodeEnvironment {
this.global.page.addListener('pageerror', handleError)
}
},
resetBrowser: async () => {
if (this.global.page) {
this.global.page.removeListener('pageerror', handleError)
}
if (config.browserContext === 'incognito' && this.global.context) {
await this.global.context.close()
} else if (this.global.page) {
await this.global.page.close()
}
this.global.page = null

if (this.global.browser) {
await this.global.browser.disconnect()
}

this.global.browser = await puppeteer.connect({
...config.connect,
...config.launch,
browserURL: undefined,
browserWSEndpoint: wsEndpoint,
})

if (config.browserContext === 'incognito') {
// Using this, pages will be created in a pristine context.
this.global.context = await this.global.browser.createIncognitoBrowserContext()
} else if (
config.browserContext === 'default' ||
!config.browserContext
) {
/**
* Since this is a new browser, browserContexts() will return only one instance
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
*/
this.global.context = await this.global.browser.browserContexts()[0]
} else {
throw new Error(
`browserContext should be either 'incognito' or 'default'. Received '${
config.browserContext
}'`,
)
}
await this.global.jestPuppeteer.resetPage()
},
}

await this.global.jestPuppeteer.resetPage()
await this.global.jestPuppeteer.resetBrowser()
}

async teardown() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('resetBrowser', () => {
test('should reset browser', async () => {
const oldBrowser = browser
await jestPuppeteer.resetBrowser()
expect(browser).not.toBe(oldBrowser)
expect(browser.isConnected()).toBe(true)
expect(oldBrowser.isConnected()).toBe(false)
})
})

0 comments on commit ae93739

Please sign in to comment.