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

question regarding globalSetup and globalTeardown #686

Open
jdsmith2816 opened this issue Apr 30, 2021 · 8 comments
Open

question regarding globalSetup and globalTeardown #686

jdsmith2816 opened this issue Apr 30, 2021 · 8 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jdsmith2816
Copy link

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!

@cutterbl
Copy link

cutterbl commented May 6, 2021

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

@mmarkelov
Copy link
Member

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

@cutterbl
Copy link

cutterbl commented May 6, 2021

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.

@eddiemonge
Copy link

@mmarkelov does that provide some clarification?

@wenshu520
Copy link

I have the same question.

@mmarkelov
Copy link
Member

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: #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 mmarkelov added enhancement New feature or request help wanted Extra attention is needed labels May 27, 2021
@tommmyy
Copy link

tommmyy commented Jul 23, 2021

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

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

@cutterbl
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants