Udemy Playwright: Web Automation Testing From Zero to Hero: Difference between revisions

Jump to navigation Jump to search
Line 1,315: Line 1,315:


=== Sharing Authentication State ===
=== Sharing Authentication State ===
Create new folder <code>.auth</code>
Create new file <code>tests\auth.setup.ts</code>
<nowiki>
import { test as setup } from "@playwright/test"
const authfile = ".auth/user.json"
setup("authentication", async ({ page }) => {
  await page.goto("https://conduit.bondaracademy.com/")
  await page.getByText("Sign in").click()
  await page
    .getByRole("textbox", { name: "Email" })
    .fill("conduit@dirksonline.net")
  await page
    .getByRole("textbox", { name: "Password" })
    .fill("qB85R86#ZMKME$jVEVq#vJMDr*A!cJk")
  await page.getByRole("button").click()
  await page.waitForResponse('https://conduit-api.bondaracademy.com/api/tags')
                             
  await page.context().storageState({ path: authfile })
})</nowiki>
In file<code>playwright.config.ts</code> update <code>projects</code>
<nowiki>
  /* Configure projects for major browsers */
  projects: [
    { name: "setup", testMatch: "auth.setup.ts" },
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"], storageState: ".auth/user.json" },
      dependencies: ["setup"],
    },
    {
      name: "firefox",
      use: { ...devices["Desktop Firefox"], storageState: ".auth/user.json" },
      dependencies: ["setup"],
    },
    {
      name: "webkit",
      use: { ...devices["Desktop Safari"], storageState: ".auth/user.json" },
      dependencies: ["setup"],
    },</nowiki>
Delete the login steps from <code>test.beforeEach(...)</code>, but leave the <code>page.goto(...)</code>
=== API Authentication ===
=== API Authentication ===