Skip to content

Commit

Permalink
Add support for browserContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Beaulieu authored and kevlened committed Sep 24, 2018
1 parent 2324f71 commit 1c1d3dd
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
globals: {
page: true,
browser: true,
context:true,
jestPuppeteer: true,
},
rules: {
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ Other options are documented in [jest-dev-server](https://github.com/smooth-code

### Configure Puppeteer

Jest Puppeteer automatically detect the best config to start Puppeteer but sometimes you may need to specify custom options. All Puppeteer [launch](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) or [connect](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) options can be specified in `jest-puppeteer.config.js` at the root of the project. Since it is JavaScript, you can use all stuff you need, including environment.
Jest Puppeteer automatically detects the best config to start Puppeteer but sometimes you may need to specify custom options. All Puppeteer [launch](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) or [connect](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) options can be specified in `jest-puppeteer.config.js` at the root of the project. Since it is JavaScript, you can use all the stuff that you need, including environment.

The browser context can be also specified - `default` or `incognito` - if you want more isolation between running instances. More information available in [jest-puppeteer-environment readme](https://github.com/smooth-code/jest-puppeteer/blob/master/packages/jest-environment-puppeteer/README.md)

```js
// jest-puppeteer.config.js
Expand All @@ -108,12 +110,13 @@ module.exports = {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
},
browserContext: 'default'
}
```

### Configure ESLint

Jest Puppeteer exposes two new globals: `browser`, `page`. If you want to avoid errors, you can add them to your `.eslintrc.js`:
Jest Puppeteer exposes three new globals: `browser`, `page`, `context`. If you want to avoid errors, you can add them to your `.eslintrc.js`:

```js
// .eslintrc.js
Expand All @@ -124,6 +127,7 @@ module.exports = {
globals: {
page: true,
browser: true,
context: true,
jestPuppeteer: true,
},
}
Expand Down
1 change: 1 addition & 0 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
launch: {
headless: process.env.CI === 'true',
},
browserContext: 'default',
server: {
command: `PORT=${port} node server`,
port,
Expand Down
1 change: 1 addition & 0 deletions packages/expect-puppeteer/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const getDefaultOptions = () => {
(global.puppeteerConfig.launch && global.puppeteerConfig.launch.slowMo) ||
(global.puppeteerConfig.connect && global.puppeteerConfig.connect.slowMo)
) &&
global.puppeteerConfig.browserContext &&
defaultOptionsValue &&
defaultOptionsValue.timeout
) {
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ it('should fill an input', async () => {
})
```

### `global.context`

Give access to a [Browser context](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browsercontext) that is instanciated when the browser is launched.

It is possible to set the browser context inside the config `jest-puppeteer-config.js` in the root of this project. The context will always be exposed via the global `context` object the same way `page` and `browser` are exposed.

### `global.jestPuppeteer.debug()`

Put test in debug mode.
Expand All @@ -79,6 +85,9 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin

- `launch` <[object]> [All Puppeteer launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
- `connect` <[object]> [All Puppeteer connect options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) can be specified in config. This is an alternative to `launch` config, allowing you to connect to an already running instance of Chrome.
- `browserContext` <[string]>. Defaults to `default` but can have the following values (any other will throw an error on test suite setup):
- `default` Default valDefault behavior, the browser will have one instance where all tabs share the same context.
- `incognito` Each instance to have a separate, isolated context. Useful when running tests that could interfere with one another. (*Example: testing multiple users on the same app at once with login, transactions, etc.*)
- `exitOnPageError` <[boolean]> Exits page on any global error message thrown. Defaults to `true`.
- `server` <[Object]> Server options allowed by [jest-dev-server](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server)

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"prepublishOnly": "yarn build"
},
"peerDependencies": {
"puppeteer": "^1.0.0"
"puppeteer": "^1.5.0"
},
"dependencies": {
"chalk": "^2.4.1",
Expand Down
24 changes: 22 additions & 2 deletions packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ class PuppeteerEnvironment extends NodeEnvironment {
...config.launch,
browserWSEndpoint: wsEndpoint
})
this.global.page = await this.global.browser.newPage()

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') {
/**
* 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.page = await this.global.context.newPage()
if (config && config.exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
}
Expand Down Expand Up @@ -88,8 +102,14 @@ class PuppeteerEnvironment extends NodeEnvironment {
}

async teardown() {
const config = await readConfig()
this.global.page.removeListener('pageerror', handleError)
await this.global.page.close()

if(config.browserContext === 'incognito') {
await this.global.context.close()
} else {
await this.global.page.close()
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-environment-puppeteer/src/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const exists = promisify(fs.exists)

const DEFAULT_CONFIG = {
launch: {},
browserContext: 'default',
exitOnPageError: true,
}
const DEFAULT_CONFIG_CI = {
launch: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
},
browserContext: 'default',
exitOnPageError: true,
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment-puppeteer/tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
globals: {
page: true,
browser: true,
context:true,
},
}
2 changes: 1 addition & 1 deletion packages/jest-puppeteer-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"chrome-headless"
],
"peerDependencies": {
"puppeteer": "^1.0.0"
"puppeteer": "^1.5.0"
},
"dependencies": {
"expect-puppeteer": "^3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"chrome-headless"
],
"peerDependencies": {
"puppeteer": "^1.0.0"
"puppeteer": "^1.5.0"
},
"dependencies": {
"expect-puppeteer": "^3.4.0",
Expand Down

0 comments on commit 1c1d3dd

Please sign in to comment.