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

Add incognito context support #83

Closed
wants to merge 20 commits into from
Closed
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
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 options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) 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 detect the best config to start Puppeteer but sometimes you may need to specify custom options. [All Puppeteer launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) 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 @@ -2,6 +2,7 @@ module.exports = {
launch: {
headless: process.env.CI === 'true',
},
browserContext: 'default',
server: {
command: 'node server',
port: 4444,
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 @@ -9,6 +9,7 @@ export const getDefaultOptions = () => {
global.puppeteerConfig &&
global.puppeteerConfig.launch &&
global.puppeteerConfig.launch.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 @@ -78,6 +84,9 @@ it('should put test in debug mode', async () => {
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.

- `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.
- `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 @@ -47,7 +47,21 @@ class PuppeteerEnvironment extends NodeEnvironment {
: undefined,
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}'`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually a breaking change because there is no default value specified and all users will have an error.

Please consider it as default if not specified.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix soon ! Thanks for the feedback :) !

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed my implementation a bit, it felt easier to manage with a default value. If you think its inefficient please let me know I'll try to find a better solution.

}

this.global.page = await this.global.context.newPage()
if (config && config.exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
}
Expand Down Expand Up @@ -95,8 +109,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.2.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.2.0",
Expand Down