Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resetBrowser #237

Merged
merged 4 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})