microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.81k stars 3.66k forks source link

StorageState in the global-setup doesn't work #18699

Closed gauravgandhi1315 closed 1 year ago

gauravgandhi1315 commented 1 year ago

global-setup works as expected. When test open up the URL, it goes back to the login page. I am not sure what I am doing wrong in my code. Can someone please help.

Playwright config file:

  globalSetup: require.resolve('./config/global-setup'),
  use: {
    storageState: 'storageState.json'
}

global setup:

const page = await browser.newPage();
  console.log('global setup test');
  await page.goto("URL", { waitForSelector: '#okta-signin-username' });
  await page.fill('#okta-signin-username', 'test');
  await page.click('#okta-signin-submit');
  await page.fill('#input', 'Password');
  await page.click('[value=Verify]');
  await page.context().storageState({ path: storageState });
  await browser.close();

test file:

 await page.goto("URL");
await verifyActions.verifyElementEnabled('#page-header');
gauravgandhi1315 commented 1 year ago

Is anything wrong I am doing in this case? Can someone please help?

v-romant commented 1 year ago

Two moment are possible: 1) Just simple mistake, try to name your path in global setup as "storageState.json" instead of storageState and also check that location of saved file and file in config are the same

2) Authorisation in your app can work not rellaing on cookies

gauravgandhi1315 commented 1 year ago

@v-romant Its still not working as per your suggestion

yury-s commented 1 year ago

@gauravgandhi1315 can you log storage state content to console in global setup and in the test and check if it contains same data?

in global setup:

 await page.context().storageState({ path: storageState });
 console.log(await page.context().storageState());
 await browser.close();

in the test:

 console.log(await page.context().storageState());
await page.goto("URL");
await verifyActions.verifyElementEnabled('#page-header');
gauravgandhi1315 commented 1 year ago

@yury-s I just checked and it does contain the same data

gauravgandhi1315 commented 1 year ago

I am not sure what this array is origins: [ { origin: 'URL', localStorage: [Array] } ]

yury-s commented 1 year ago

If the content is the same it means that the state in the global setup is successfully picked up in the tests. In that case it's likely case 2 that @v-romant mentioned above and your app relies on something else besides cookies for authentication. Note that Playwright persists only cookies and localStorage state into storageState file.

It might be that the app stores some parts of the authenticated state in session storage, in that case you may do what is described in this doc. I'd recommend you consult with the app developers to learn what else besides cookies and local storage is necessary to persist the authentication state of the app between browser runs.

pavelfeldman commented 1 year ago

We need more information to act on this report. As long as we can't repro it, it is unlikely with can make progress with it. Allow me to close it, but please file a new one and link to this issue when you get back to it!

allwillburnn commented 1 year ago

@gauravgandhi1315 I am more than sure that the problem is that your site doesn't allow you to authorize using cookies. (For example, Spotify will not allow you to enter in this way). Global storageState works fine.

bondar-artem commented 1 year ago

Having the same issue as yours. While debugging, I found an interesting thing that looks like a bug with a "storageState" fixture.

When my config looks like this, and I have a dependent "setup" project step everything works: projects: [ {name: 'setup', testMatch: 'auth.setup.ts' }, { name: 'chromium', use: { ...devices['Desktop Chrome'], storageState: '.auth/user.json' }, dependencies: ['setup'] } ]

But it I remove "dependencies" section, which triggers 'auth.setup.ts'. It does not work! But it should, because "user.json" file is still valid and has a valid token needed for authorization of the browser.

storageState fixture for some reason not set the authorization based on the file content, despite file is 100% valid

This application has only token-based authorization without cookies at all.

intel352 commented 1 year ago

Having the same issue as yours. While debugging, I found an interesting thing that looks like a bug with a "storageState" fixture.

When my config looks like this, and I have a dependent "setup" project step everything works: projects: [ {name: 'setup', testMatch: 'auth.setup.ts' }, { name: 'chromium', use: { ...devices['Desktop Chrome'], storageState: '.auth/user.json' }, dependencies: ['setup'] } ]

But it I remove "dependencies" section, which triggers 'auth.setup.ts'. It does not work! But it should, because "user.json" file is still valid and has a valid token needed for authorization of the browser.

storageState fixture for some reason not set the authorization based on the file content, despite file is 100% valid

This application has only token-based authorization without cookies at all.

Try adding storage state to your setup project as well, see if it helps:

"projects":[
   {
      "name":"setup",
      "testMatch":"auth.setup.ts",
      "use":{
         "storageState":".auth/user.json"
      },
   },
   {
      "name":"chromium",
      "use":{
         "...devices"[
            "Desktop Chrome"
         ],
         "storageState":".auth/user.json"
      },
      "dependencies":[
         "setup"
      ]
   }
]