jest-playwright icon indicating copy to clipboard operation
jest-playwright copied to clipboard

question regarding globalSetup and globalTeardown

Open jdsmith2816 opened this issue 3 years ago • 8 comments

I am attempting to utilize globalSetup and globalTeardown to implement authentication once per suite rather than once per test.

In jest.config I customize the browsers array via environment variables like so

let browsers = ['chromium'];

if (process.env.E2E_BROWSERS) {
  browsers = process.env.E2E_BROWSERS.split(' ');
}

What is the proper way to inject the storage state into the currently available browser? The examples in the documentation assume the use of just one browser, chromium.

I saw an example from @mmarkelov that might be relevant but it appears to have been written a few months before PlaywrightEnvironment was introduced to jest-playwright

Any guidance is greatly appreciated!

jdsmith2816 avatar Apr 30 '21 18:04 jdsmith2816

I have the same question. Comment to follow this thread.

cutterbl avatar May 06 '21 15:05 cutterbl

I'm not sure that i understood this issue correctly. Why can't you use it like this? https://github.com/playwright-community/jest-playwright/discussions/664

mmarkelov avatar May 06 '21 18:05 mmarkelov

The globalSetup example looks like this

// global-setup.js
import { globalSetup as playwrightGlobalSetup } from 'jest-playwright-preset';

module.exports = async function globalSetup(globalConfig) {
  await playwrightGlobalSetup(globalConfig);

  const browserServer = await chromium.launchServer();
  const wsEndpoint = browserServer.wsEndpoint();
  const browser = await chromium.connect({ wsEndpoint: wsEndpoint });
  const page = await browser.newPage();

  // your login function
  await doLogin(page);

  // store authentication data
  const storage = await page.context().storageState();
  process.env.STORAGE = JSON.stringify(storage);
};

So, in the example you have to specify the browser being used, rather than the one utilized from the browsers array in the jest-playwright.config.js. So the question becomes, how would you use the current browser for your login process.

Seems to me that the globalSetup runs prior to all of the tests being run, so globalSetup probably doesn't know which browser your tests are going to run against, and probably wouldn't run again if you've defined multiple browsers, but I don't know enough about jest internals, so I'm just assuming. If that's the case, I guess the developer just has to pick one (browser) to write the login against (to get the state data). If someone could verify that, that would be great.

cutterbl avatar May 06 '21 18:05 cutterbl

@mmarkelov does that provide some clarification?

eddiemonge avatar May 24 '21 20:05 eddiemonge

I have the same question.

wenshu520 avatar May 27 '21 03:05 wenshu520

I understood the problem. It was kind of trick from jest-puppeteer to make some login stuff in globalSetup, but this trick working well with one browser. I made some investigation on how can we simplify this process for users, but without any success :(

There is discussion about this here: https://github.com/playwright-community/jest-playwright/issues/611

I can try to find out solution, but it can be complicated. So if you have any thoughts about this, please let me know

mmarkelov avatar May 27 '21 18:05 mmarkelov

I think that in your jest-playwright.config.js file you just add:

module.exports = () => ({
  contextOptions: {
    storageState: JSON.parse(process.env.STORAGE)
  } 
})

tommmyy avatar Jul 23 '21 10:07 tommmyy

I wrote a blog post about how I approached persistent login using globalSetup. Spent a lot of time going over the documentation in my work to figure this out. Maybe it'll help someone else stumbling on this thread. If there are ways to improve it, I am always open to suggestions.

Playwright and Jest-Playwright

cutterbl avatar Jul 23 '21 12:07 cutterbl