Skip to content

Commit

Permalink
feat: implement LoginFixture for streamlined login/logout in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Nov 20, 2024
1 parent eadffba commit cb7970f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
32 changes: 32 additions & 0 deletions apps/web/e2e/fixture/login.fixture.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
17 changes: 9 additions & 8 deletions apps/web/e2e/tests/get-course.spec.ts
Original file line number Diff line number Diff line change
@@ -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("[email protected]", "password");
});

test("should find, open and enroll the paid course", async ({ page }) => {
await page.getByLabel("email").fill("[email protected]");
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();

Expand Down

0 comments on commit cb7970f

Please sign in to comment.