jest-playwright
jest-playwright copied to clipboard
question regarding globalSetup and globalTeardown
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!
I have the same question. Comment to follow this thread.
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
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.
@mmarkelov does that provide some clarification?
I have the same question.
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
I think that in your jest-playwright.config.js
file you just add:
module.exports = () => ({
contextOptions: {
storageState: JSON.parse(process.env.STORAGE)
}
})
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.