Comments (8)
Yeah good idea, I would prefer to avoid environment variable and add an option in config incognitoContext
that default to false
.
from jest-puppeteer.
Update: Here's how I managed to make it working right now, it's not very pretty IMO but it works. If its run in headful mode you will see a window pop-up and close before the incognito ones appear.
As mentionned in the documentation, I created a custom environment in a file named jest-puppeteer-custom-env.js
and I put it in my utils
folder which is in my app's root directory.
const PuppeteerEnvironment = require('jest-environment-puppeteer');
class CustomEnvironment extends PuppeteerEnvironment {
async setup() {
await super.setup();
// Close the existing page as we want to use the the incognito context
this.global.page.close();
// Create a new incognito browser context.
this.global.context = await this.global.browser.createIncognitoBrowserContext();
// Create a new page in a pristine context.
this.global.page = await this.global.context.newPage();
}
async teardown() {
// Your teardown
await super.teardown();
}
}
module.exports = CustomEnvironment;
I link to it in from the jest
config of my package.json
.
"jest": {
"preset": "jest-puppeteer",
"testEnvironment": "<rootDir>/utils/jest-puppeteer-custom-env.js",
[... rest of the config]
}.
I wish I could redefine setup()
without having to call super.setup()
. I tried it but since jest-puppeteer-environment
does not export readConfig()
and I need to be able to call it, I had to drop that option.
from jest-puppeteer.
Follow up #133
from jest-puppeteer.
@Niceplace you could have added
this.global.__CONTEXT__ = await this.global.__BROWSER__.createIncognitoBrowserContext();
after this
this.global.__BROWSER__ = await puppeteer.connect({ browserWSEndpoint: wsEndpoint, slowMo: 5, defaultViewport: null });
in puppeteer_environment.js and use page = await global.__CONTEXT__.newPage();
instead of page = await global.__BROWSER__.newPage();
for all new pages
from jest-puppeteer.
@Niceplace you could have added
this.global.__CONTEXT__ = await this.global.__BROWSER__.createIncognitoBrowserContext();
after this
this.global.__BROWSER__ = await puppeteer.connect({ browserWSEndpoint: wsEndpoint, slowMo: 5, defaultViewport: null });
in puppeteer_environment.js and use
page = await global.__CONTEXT__.newPage();
instead ofpage = await global.__BROWSER__.newPage();
for all new pages
Hi @justfathi I'm exactly trying to use those config line to open separate new browser context for each test, I've changed my config as you suggested, but still doesn't look like it's doing it,
plus there is no mention how to tweak the teardown.js for work for context
would appreciate your assistance if got it working
from jest-puppeteer.
@mohammedalnuaimi there is a better way to do this than what I mentioned above.
let page, context;
context = await global.BROWSER.createIncognitoBrowserContext();
page = await context.newPage();
If you want a new page/tab just use "await context.newPage();".
If you want a new browser then do "await global.BROWSER.createIncognitoBrowserContext();" which will give you the "context" and you can use this to get a new page/tab
from jest-puppeteer.
thanks @justfathi the thing is I'm using the custom implementation suggested by Jest-puppeteer which suggests setting up three config files: setup.js, environment and tearmdown.js
with those I'm not able to get the context to work!
the question here, where can similar thing you just mentioned in my case?
from jest-puppeteer.
@justfathi I think your previous answer triggered me thinking a bit deeper about it, so the solution for this is I only need to change the line in the test where we first create the page from
global.BROWSER.newPage();
To
const context = global.BROWSER.createIncognitoBrowserContext();
page = context.page
Thanks mate
from jest-puppeteer.
Related Issues (20)
- JSHandle@node is not supported HOT 6
- `spawnd` package should have `exit` as a dependency, not a devDependency
- Dependency Errors Using `npm i` HOT 2
- Cannot find module 'node:stream' on fresh install HOT 1
- Compiler errors importing `JestPuppeteerConfig` interface HOT 2
- Compiler errors extending `PuppeteerEnvironment`
- Compiler errors accessing `PuppeteerEnvironment` globals HOT 1
- Jest preset not working with latest jest and jest-puppeteer HOT 6
- Snapshot serializers added with `expect.addSnapshotSerializer` are not used HOT 3
- Remove Warning On New Headless Setting HOT 5
- Many browser windows open when running tests HOT 3
- [jest-dev-server] not working server.usedPortAction: 'ignore' config when running React application with react-scripts
- Invariant: WORKERS_COUNT not found HOT 4
- @types/expect-puppeteer no such type toMatchTextContent HOT 1
- Using jest-puppeteer with nx HOT 4
- How can I launch jest-puppeteer with args? HOT 1
- Regression: `injectGlobals:false` no longer supported ("Cannot convert undefined or null to object") HOT 1
- Puppeteer 21 regression: `[object Object] is not supported` HOT 4
- Is @types/jest-environment-puppeteer still required? HOT 3
- jest-dev-server - "kill" missing in "usedPortAction" type definition HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-puppeteer.