Giter Site home page Giter Site logo

Comments (8)

gregberge avatar gregberge commented on May 12, 2024 5

Yeah good idea, I would prefer to avoid environment variable and add an option in config incognitoContext that default to false.

from jest-puppeteer.

Niceplace avatar Niceplace commented on May 12, 2024

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.

gregberge avatar gregberge commented on May 12, 2024

Follow up #133

from jest-puppeteer.

justfathi avatar justfathi commented on May 12, 2024

@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.

mohammedalnuaimi avatar mohammedalnuaimi commented on May 12, 2024

@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

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.

justfathi avatar justfathi commented on May 12, 2024

@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.

mohammedalnuaimi avatar mohammedalnuaimi commented on May 12, 2024

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.

mohammedalnuaimi avatar mohammedalnuaimi commented on May 12, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.