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

[Bug]: Auth Setup File Not Being Found #31347

Closed
joshmgrant opened this issue Jun 17, 2024 · 9 comments
Closed

[Bug]: Auth Setup File Not Being Found #31347

joshmgrant opened this issue Jun 17, 2024 · 9 comments

Comments

@joshmgrant
Copy link

joshmgrant commented Jun 17, 2024

Version

1.44.1

Steps to reproduce

  1. Write/update a given spec.ts file
  2. Use test.use({ storageState: 'playwright/.auth/gmail.json' }); in the spec file
  3. Run playwright test

Expected behavior

I expect tests to use the test.use storage state based on the .json file and login appropriately as needed.

Actual behavior

Tests fail immediately with an error

ERROR: Error reading storage state from playwright/.auth/gmail.json
ENOENT: No such file or directory, open "playwright/.auth/gmail.json"

Additional context

I have validated that the .auth file in question exists. This error occurs on developer machines, and so authentication does work but fails intermittently. As well, there are multiple auth setup functions to use multiple roles in testing based on this documentation
.

Environment

System:
- MacOS Sonoma 14.5
- Apple M3 Max proccessor
Binaries:
- Node v18.19.0
- pnpm 8.12.1
Playwright:
- @playwright/test: 1.44.1
@dgozman
Copy link
Contributor

dgozman commented Jun 17, 2024

@joshmgrant Could you please share a full repro with us? I suspect this is related to cwd when you run your tests. The robust solution is to provide an absolute path, for example:

import path from 'path';

test.use({ storageState: path.resolve(__dirname, '../../playwright/.auth/gmail.json') })

@joshmgrant
Copy link
Author

Here is my auth.setup.ts file

import * as fs from 'node:fs';
import { test as setup } from '@playwright/test';
import { LoginPage } from './pages/login-page';

// TODO: replace with env vars
const gmailUser = 'some_user"
const outlookUser = 'some_other_user'
const password = process.env.E2E_USER_PASSWORD || ''

// following this approach for multiple user authenications
// https://playwright.dev/docs/auth#multiple-signed-in-roles
const outlookAuthFile = 'playwright/.auth/outlook.json';

setup.skip(fs.existsSync(outlookAuthFile)); // added this based on another ticket to fix the issue
setup('authenticate as Outlook user', async ({ page }) => {
  // Perform authentication steps. Replace these actions with your own.
  const loginPage = new LoginPage(page);
  loginPage.loginAs(outlookUser, password);

  await page.context().storageState({ path: outlookAuthFile });
});

const gmailAuthFile = 'playwright/.auth/gmail.json';

setup.skip(fs.existsSync(gmailAuthFile));
setup('authenticate as GMail user', async ({ page }) => {
  const loginPage = new LoginPage(page);
  loginPage.loginAs(gmailUser, password);

  await page.context().storageState({ path: gmailAuthFile });
});

@dgozman
Copy link
Contributor

dgozman commented Jun 18, 2024

@joshmgrant Unfortunately, we cannot repro with just a single file. We need to run the whole example, so need the whole project.

Did you have a chance to try the absolute path suggestion?

@joshmgrant
Copy link
Author

Let me try the absolute path suggestion. I can see if I can get a repo but it might take some work.

@joshmgrant
Copy link
Author

Hello again. I tried an absolute file path and it doesn't seem to work. I'm also unable to share more than the config file above.

@dgozman
Copy link
Contributor

dgozman commented Jun 18, 2024

Hello again. I tried an absolute file path and it doesn't seem to work. I'm also unable to share more than the config file above.

In this case we won't be able to help, unfortunately. I'll leave this issue open for a few days, just in case you'd come up with a reproduction.


Looking at the snippet above, it seems like you'd skip both setups, for outlook and gmail, whenever any of the storage state files is present. I'd recommend to read about test.skip in the documentation.

@joshmgrant
Copy link
Author

@dgozman Hi again, thanks for the help. I've created a repo that hopefully reproduces the issue

https://github.com/joshmgrant/playwright-auth-repo

@joshmgrant
Copy link
Author

Just to add: I ended up deleting the gmail.json and outlook.json files from playwright/.auth/ and that has caused a hard fail for all tests. I'm not sure how to recover these files or generate them again, or use the workflows to re-create these files

@joshmgrant
Copy link
Author

Ok, turns out I can simply regenerate .json files using the Playwright UI and auth.setup.ts. I missed this before in the docs. Thanks for the help! We can close this issue/

@dgozman dgozman closed this as completed Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants