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
65.99k stars 3.6k forks source link

sharing of storage state is broken in v1.39.0. Works fine in 1.38.0 #27977

Closed bondar-artem closed 10 months ago

bondar-artem commented 11 months ago

System info

Source code

Link to the GitHub repository with the repro

https://github.com/bondar-artem/playwright-bug

Steps

Expected

when application opens, user should be already looged in

Actual

user is not logged in

When downgrade to Playwright 1.38, the test works as expected and user session is authorized when application opens

pavelfeldman commented 11 months ago

Works fine for me. Note that you are using fs.writeFileSync with relative path (resolved relative to CWD), so your tests aren't working in the general case. You should always path.resolve(__dirname, '../auth/user.json') or require.resolve('../auth/user.json') to get consistent path that does not depend on the folder you run your tests from.

bondar-artem commented 10 months ago

@pavelfeldman I don't think that fs.writeFileSync it's the case. You can remove the auth.setup.ts from the project altogether, only leaving the user.json file with a valid token. Then read this file in playwright.config.ts. Like this:

export default defineConfig({
  testDir: './tests',
  reporter: 'html',
  use: {
    trace: 'on-first-retry',
  },

  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], storageState: '.auth/user.json' }
    }
  ]
});

And it's reproducible on my end. With Playwright 1.39 - the storage state is not set image

With Playwright 1.38 and earlier versions - it is working fine. image

dgozman commented 10 months ago

@bondar-artem I can reproduce the issue. Your site behaves differently between Chrome 117.0.5938.62 that comes with Playwright 1.38.1 and Chrome 119.0.6045.9 that comes with Playwright 1.39.0. Setting localStorage.jwtToken results in a 401 request in the newer Chrome.

I'd say Playwright has found a bug on your site that breaks with the browser update. Sounds like a success story 🎉 Happy testing!

bondar-artem commented 10 months ago

I'd say Playwright has found a bug on your site that breaks with the browser update. Sounds like a success story 🎉 Happy testing!

@dgozman Could you please let me know how did you get 401? I have just updated my Chrome to 119.0.6045.9, then manually navigated to the application, clicked Sign In, entered my username and password, and was successfully authorized.

dgozman commented 10 months ago

@bondar-artem I evaluated localStorage.jwtToken = "...." in DevTools Console with the value from user.json and reloaded the page.

bondar-artem commented 10 months ago

@dgozman I tried it, worked for me just fine in Chrome 119. No errors. Probably you have 401 because you took the expired token from the repo. Generate a new token with username and password. image

Local storage: image

But if you do the same in the Playwright, you will see that Local storage in the browser remains empty after the execution

dgozman commented 10 months ago

@bondar-artem I just tried the following in Chrome 120.0.6099.5

I'd say Playwright works the same way as I did manually.

bondar-artem commented 10 months ago

@dgozman Thank you! You are right, the issue is identified on my side