playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Way to skip global setup for selective tests?

Open AmareshKulkarni opened this issue 3 years ago • 11 comments

My application has account signup workflow and a number of workflows after user logs in. Following the documentation, I was able to use global setup for authentication and re-use across all tests. However, I don't need authentication for the actual account signup flow.

Is there a way to skip global setup that involves login for the account signup tests or conditionally execute global setup if the test being run is account signup related?

AmareshKulkarni avatar Feb 02 '22 12:02 AmareshKulkarni

Is there a way to skip global setup that involves login for the account signup tests or conditionally execute global setup if the test being run is account signup related?

Global setup runs once before all tests. Do you run authentication and signup flows in one and the same project?

You can try something like this to only load authenticated state in the tests that require authentication and not load it for signup ones. Will it work?

yury-s avatar Feb 02 '22 20:02 yury-s

Thank you for the quick turnaround.

The link provided looks like is for a situation when a user needs to be logged in multiple times (per role), but in my case, it is more like the user may not even exist. They will start with the sign-up, and then move onto login to perform various other functions.

I can run them (signup and sign-in) in separate playwright projects, but they are both in the same codebase. Is there a way to configure the global setup per playwright project? Or another alternative is to get the name of the project being run in the global setup, so that I can conditionally execute the login logic. Either of these options could work for me, could not find a reference for guidance.

The last option probably is to completely isolate codebases for sign up and sign-in into separate code bases, but I am worried that would just add additional effort in maintenance.

AmareshKulkarni avatar Feb 02 '22 21:02 AmareshKulkarni

It is one globalSetup per config currently. No per-project options and project name is not available to the globalSetup (note that you single playwright run may execute multiple projects). You could split your config into two with two different global setups, it is somewhat inconvenient though.

yury-s avatar Feb 02 '22 23:02 yury-s

Thank you. I will for now use a different configuration file for signup flow

-c or --config : Configuration file. If not passed, defaults to playwright.config.ts or playwright.config.js in the current directory.

AmareshKulkarni avatar Feb 03 '22 11:02 AmareshKulkarni

@dgozman global fixtures should help here.

aslushnikov avatar Mar 01 '22 18:03 aslushnikov

I did it this way:

const context = await browser.newContext({ storageState: { cookies: [], origins: [] } });
  const page = await context.newPage();
  await page.goto('/');
// auth context removed

evanfuture avatar Jun 04 '22 08:06 evanfuture

Found an even easier way, just drop this into your describe block or above your tests: test.use({ storageState: { cookies: [], origins: [] } });

evanfuture avatar Jun 04 '22 09:06 evanfuture

@evanfuture This worked for me, Thanks!

sowji-elligo avatar Sep 08 '22 20:09 sowji-elligo

Found an even easier way, just drop this into your describe block or above your tests: test.use({ storageState: { cookies: [], origins: [] } });

But this will still run the globalSetup once before your tests, no? Just that it won't use what's inside storageState.json file anymore.

DanielStoica85 avatar Nov 02 '22 13:11 DanielStoica85

But this will still run the globalSetup once before your tests, no? Just that it won't use what's inside storageState.json file anymore.

Yes, exactly. The idea is that you will want to use the globalSetup far more often than to not use it, so this works as a compromise.

evanfuture avatar Nov 02 '22 15:11 evanfuture

@yury-s Is there a reason why

test.use({ storageState: undefined });

does not act like

test.use({ storageState: { cookies: [], origins: [] } });

I thought the first would also override the storage state from global configuration but that does not work. I would prefer the first one because it is even shorter. Are there other use cases for undefined?

Camo30 avatar Dec 14 '22 09:12 Camo30

Why was this issue closed?

Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

pavelfeldman avatar Jun 30 '23 20:06 pavelfeldman

I have another case that could be studied.

Environment

  • I have 3 different accounts with different roles.
  • I use the vsCode extension

Flow

I run a specific test for the first time, which has as a dependency the auth.setup.ts file. It saves the needed storageState for each of the three roles.

Second time I run the specific test, the storageState could be reused, and all auth.setup.ts tests are in fact overwriting the existing files.

Expected

Conditionally run the auth.setup.ts if storageState files are already present. Or at least in some way to be able to skip them from the vsCode interface.

Workaround

Of course, we could setup an env file to add or remove the dependency:

// playwright.config.ts
  projects: [
    { name: 'setup', testMatch: /.*\.setup\.ts/ },
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
      dependencies: !!process.env.SKIP_SETUP? [] : ['setup'],
    },
...

But maybe there is some more elegant way to do that?

Silverium avatar Dec 29 '23 11:12 Silverium

@Silverium You can manually check that fs.existsSync(storageStateFilePath) in the auth.setup.ts and just return from the test function. If that does not work, please file a new issue so that we can take a look.

dgozman avatar Jan 10 '24 00:01 dgozman

@Silverium You can manually check that fs.existsSync(storageStateFilePath) in the auth.setup.ts and just return from the test function. If that does not work, please file a new issue so that we can take a look.

That's indeed faster and a less messy DX.

Now when the developer wants to re-write the files generated in the auth.setup.ts, it will be enough to delete the .auth folder (with the files inside) or any of the generated files.

Silverium avatar Jan 11 '24 08:01 Silverium