Rayrun

How do I manually execute an auth.setup file in @playwright/test that is usually run automatically before each test?

Answer

Manually Running an Auth Setup File in Playwright

You can manually run an auth.setup file in Playwright by following these steps:

Step 1: Update Your Configuration

Open your playwright.config.ts and find the projects array. Look for the project with the auth.setup file. Temporarily remove or comment out this project. This prevents it from running automatically before each test. Don't forget to save your changes!

// playwright.config.ts
module.exports = {
  // ...
  projects: [
    // Comment out or remove the setup project
    // {
    //   name: 'setup',
    //   testMatch: '**/auth.setup.ts',
    // },
    // ...
  ],
};

Step 2: Manually Run the Setup File

To manually run the auth.setup file, open a terminal, navigate to your project directory, and run the following command:

npx playwright test --project=setup

Replace setup with the name of your setup project if it's different. This command runs only the setup project, allowing you to perform authentication steps without running any tests.

The setup script will authenticate and save its state as specified in your code (e.g., saving cookies and local storage snapshot). The authenticated state will be stored in a designated location (e.g., 'playwright/.auth/user.json').

Step 3: Revert Your Configuration

After manually running the setup script, revert back to including or uncommenting the original setup project in your playwright.config.ts. This ensures that authentication is automatically performed before each test as intended. Don't forget to save your changes!

Remember, this guide is based on configuring global setups using Playwright's projects array. If you have additional specific requirements or constraints, you may need to adjust these steps accordingly.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.