diff --git a/apps/web/e2e/fixture/login.fixture.ts b/apps/web/e2e/fixture/login.fixture.ts new file mode 100644 index 000000000..bbb6bb5ed --- /dev/null +++ b/apps/web/e2e/fixture/login.fixture.ts @@ -0,0 +1,32 @@ +import { expect } from "@playwright/test"; + +import type { Locator, Page } from "@playwright/test"; + +export class LoginFixture { + 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 456c3aeb5..321d7e47a 100644 --- a/apps/web/e2e/tests/get-course.spec.ts +++ b/apps/web/e2e/tests/get-course.spec.ts @@ -1,18 +1,19 @@ import { expect, test } from "@playwright/test"; +import { LoginFixture } from "e2e/fixture/login.fixture"; + test.describe("course", () => { test.beforeEach(async ({ page }) => { - await page.goto("/auth/login"); + const loginFixture = new LoginFixture(page); + await loginFixture.login("user@example.com", "password"); }); - 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); + test.afterEach(async ({ page }) => { + const loginFixture = new LoginFixture(page); + await loginFixture.logout(); + }); + test("should find, open and enroll the paid course", async ({ page }) => { await page.getByPlaceholder("Search by title...").fill("For E2E Testing"); await expect(page.getByRole("button", { name: "Clear All" })).toBeVisible();