Rayrun
← Back to Discord Forum

auth.setup.ts not running (following documentation)

augustinsorelposted in #help-playwright
Open in Discord
augustinsorel

Hi guys,

I am trying to add e2e tests with playwright and I am following the documentation (https://playwright.dev/docs/auth) I have copied the exact same code from the docs but I encounter an error with it. I get:

Error: Error reading storage state from e2e/.auth/user.json:
ENOENT: no such file or directory, open 'e2e/.auth/user.json'

I am guessing that my auth.setup.ts file isn't being run. As I can't see any user.json output. I have tried to create that user.json file manually but that does not solve the error.

Any help would be very much appricated 🙌

Here is my github repo if it helps: https://github.com/AugustinSorel/gym-graphs/tree/FEAT/e2e-oauth And here is the code:

This thread is trying to answer question "Why isn't 'auth.setup.ts' being executed, and why is 'user.json' not being generated when following the Playwright documentation for authentication in e2e tests?"

3 replies
augustinsorel

auth.setup.ts

import { test as setup, expect } from "@playwright/test";
import dotenv from "dotenv";

dotenv.config();

const authFile = "e2e/.auth/user.json";

const user = {
  email: process.env.E2E_GOOGLE_USER_EMAIL,
  password: process.env.E2E_GOOGLE_USER_PASSWORD,
};

setup("authentication", async ({ page }) => {
  if (!user.email) {
    throw new Error("user google user email is missing form env");
  }

  if (!user.password) {
    throw new Error("user google user password is missing form env");
  }

  // Perform authentication steps. Replace these actions with your own.
  await page.goto("https://github.com/login");
  await page.getByLabel("Username or email address").fill(user.email);
  await page.getByLabel("Password").fill(user.password);
  await page.getByRole("button", { name: "Sign in" }).click();

  await page.waitForURL("https://github.com/");
  await expect(
    page.getByRole("button", { name: "View profile and more" }),
  ).toBeVisible();

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

config file:

projects: [
    { name: "setup", testMatch: /.*\.setup\.ts/ },

    {
      name: "chromium",
      use: {
        ...devices["Desktop Chrome"],
        // Use prepared auth state.
        storageState: "e2e/.auth/user.json",
      },
      dependencies: ["setup"],
    },

    {
      name: "firefox",
      use: {
        ...devices["Desktop Firefox"],
        // Use prepared auth state.
        storageState: "e2e/.auth/user.json",
      },
      dependencies: ["setup"],
    },
]
augustinsorel

ok never mind It turns out running npx playwright test --ui does not trigger the auth.setup.ts

augustinsorel

but npx playwright test will

Related Discord Threads

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.