diff --git a/.prettierignore b/.prettierignore index 43c52d83..cd055a5f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -23,6 +23,7 @@ **/public packages/email-templates/**/index.ts apps/api/src/storage/migrations/** +apps/web/e2e/**/*.json # Cache diff --git a/apps/web/e2e/auth.setup.ts b/apps/web/e2e/auth.setup.ts new file mode 100644 index 00000000..0036df3e --- /dev/null +++ b/apps/web/e2e/auth.setup.ts @@ -0,0 +1,12 @@ +import { test as setup } from "@playwright/test"; + +import { AuthFixture } from "./fixture/auth.fixture"; + +setup("authenticate", async ({ page }) => { + const authFixture = new AuthFixture(page); + await authFixture.login("user@example.com", "password"); + + await page.context().storageState({ + path: "e2e/.auth/user.json", + }); +}); diff --git a/apps/web/e2e/fixture/auth.fixture.ts b/apps/web/e2e/fixture/auth.fixture.ts new file mode 100644 index 00000000..32274ccb --- /dev/null +++ b/apps/web/e2e/fixture/auth.fixture.ts @@ -0,0 +1,32 @@ +import { expect } from "@playwright/test"; + +import type { Locator, Page } from "@playwright/test"; + +export class AuthFixture { + private readonly emailInput: Locator; + private readonly passwordInput: Locator; + private readonly loginButton: Locator; + private readonly logoutButton: Locator; + + constructor(public page: Page) { + this.emailInput = page.getByLabel("email"); + this.passwordInput = page.getByLabel("password"); + this.loginButton = page.getByRole("button", { name: /login/i }); + this.logoutButton = page.getByRole("button", { name: /log out/i }); + } + + async login(email: string, password: string) { + await this.page.goto("/auth/login"); + await this.emailInput.fill(email); + await this.passwordInput.fill(password); + await this.loginButton.click(); + + await expect(this.page).toHaveURL("/"); + await expect(this.page).toHaveTitle(/dashboard/i); + } + + async logout() { + await this.logoutButton.click(); + await expect(this.page).toHaveURL("/auth/login"); + } +} diff --git a/apps/web/e2e/tests/get-course.spec.ts b/apps/web/e2e/tests/get-course.spec.ts index 456c3aeb..9acb3c4b 100644 --- a/apps/web/e2e/tests/get-course.spec.ts +++ b/apps/web/e2e/tests/get-course.spec.ts @@ -1,18 +1,8 @@ import { expect, test } from "@playwright/test"; test.describe("course", () => { - test.beforeEach(async ({ page }) => { - await page.goto("/auth/login"); - }); - test("should find, open and enroll the paid course", async ({ page }) => { - await page.getByLabel("email").fill("user@example.com"); - await page.getByLabel("password").fill("password"); - await page.getByRole("button", { name: /login/i }).click(); - - await expect(page).toHaveURL("/"); - await expect(page).toHaveTitle(/dashboard/i); - + await page.goto("/"); await page.getByPlaceholder("Search by title...").fill("For E2E Testing"); await expect(page.getByRole("button", { name: "Clear All" })).toBeVisible(); diff --git a/apps/web/e2e/tests/login.spec.ts b/apps/web/e2e/tests/login.spec.ts index 18731fa6..6f344599 100644 --- a/apps/web/e2e/tests/login.spec.ts +++ b/apps/web/e2e/tests/login.spec.ts @@ -11,13 +11,4 @@ test.describe("login page", () => { await expect(page.getByRole("heading", { name: "Sign up" })).toBeVisible(); await expect(page).toHaveURL(/register/); }); - - test("should login as test user", async ({ page }) => { - await page.getByLabel("email").fill("user@example.com"); - await page.getByLabel("password").fill("password"); - await page.getByRole("button", { name: /login/i }).click(); - - await expect(page).toHaveURL("/"); - await expect(page).toHaveTitle(/dashboard/i); - }); }); diff --git a/apps/web/e2e/tests/register.spec.ts b/apps/web/e2e/tests/register.spec.ts index 643d6584..3acf4f85 100644 --- a/apps/web/e2e/tests/register.spec.ts +++ b/apps/web/e2e/tests/register.spec.ts @@ -1,7 +1,12 @@ import { test, expect } from "@playwright/test"; +import { AuthFixture } from "e2e/fixture/auth.fixture"; + test.describe("register page", () => { test.beforeEach(async ({ page }) => { + const authFixture = new AuthFixture(page); + await page.goto("/"); + await authFixture.logout(); await page.goto("/auth/register"); }); diff --git a/apps/web/playwright.config.ts b/apps/web/playwright.config.ts index 98c5a078..97f978d7 100644 --- a/apps/web/playwright.config.ts +++ b/apps/web/playwright.config.ts @@ -20,20 +20,19 @@ export default defineConfig({ // ], // }, }, - projects: [ - { name: "setup", testMatch: /.*\.setup\.ts/ }, { - name: "chromium", - use: { - ...devices["Desktop Chrome"], - }, + name: "setup", + testMatch: /.*\.setup\.ts/, }, { - name: "firefox", + name: "chromium", + dependencies: ["setup"], use: { - ...devices["Desktop Firefox"], + ...devices["Desktop Chrome"], + storageState: "e2e/.auth/user.json", }, + testMatch: /.*\.(spec|test)\.ts$/, }, ], });