|
| 1 | +import { type Page, expect, test } from "@playwright/test" |
| 2 | + |
| 3 | +test.use({ storageState: { cookies: [], origins: [] } }) |
| 4 | + |
| 5 | +type OptionsType = { |
| 6 | + exact?: boolean |
| 7 | +} |
| 8 | + |
| 9 | +const fillForm = async (page: Page, email: string, password: string) => { |
| 10 | + await page.getByPlaceholder("Email").fill(email) |
| 11 | + await page.getByPlaceholder("Password", { exact: true }).fill(password) |
| 12 | +} |
| 13 | + |
| 14 | +const verifyInput = async ( |
| 15 | + page: Page, |
| 16 | + placeholder: string, |
| 17 | + options?: OptionsType, |
| 18 | +) => { |
| 19 | + const input = page.getByPlaceholder(placeholder, options) |
| 20 | + await expect(input).toBeVisible() |
| 21 | + await expect(input).toHaveText("") |
| 22 | + await expect(input).toBeEditable() |
| 23 | +} |
| 24 | + |
| 25 | +test("Inputs are visible, empty and editable", async ({ page }) => { |
| 26 | + await page.goto("/login") |
| 27 | + |
| 28 | + await verifyInput(page, "Email") |
| 29 | + await verifyInput(page, "Password", { exact: true }) |
| 30 | +}) |
| 31 | + |
| 32 | +test("Log In button is visible", async ({ page }) => { |
| 33 | + await page.goto("/login") |
| 34 | + |
| 35 | + await expect(page.getByRole("button", { name: "Log In" })).toBeVisible() |
| 36 | +}) |
| 37 | + |
| 38 | +test("Forgot Password link is visible", async ({ page }) => { |
| 39 | + await page.goto("/login") |
| 40 | + |
| 41 | + await expect( |
| 42 | + page.getByRole("link", { name: "Forgot password?" }), |
| 43 | + ).toBeVisible() |
| 44 | +}) |
| 45 | + |
| 46 | +test("Log in with valid email and password ", async ({ page }) => { |
| 47 | + await page.goto("/login") |
| 48 | + |
| 49 | + await fillForm(page, "admin@example.com", "changethis") |
| 50 | + await page.getByRole("button", { name: "Log In" }).click() |
| 51 | + |
| 52 | + await page.waitForURL("/") |
| 53 | + |
| 54 | + await expect( |
| 55 | + page.getByText("Welcome back, nice to see you again!"), |
| 56 | + ).toBeVisible() |
| 57 | +}) |
| 58 | + |
| 59 | +test("Log in with invalid email", async ({ page }) => { |
| 60 | + await page.goto("/login") |
| 61 | + |
| 62 | + await fillForm(page, "invalidemail", "changethis") |
| 63 | + await page.getByRole("button", { name: "Log In" }).click() |
| 64 | + |
| 65 | + await expect(page.getByText("Invalid email address")).toBeVisible() |
| 66 | +}) |
| 67 | + |
| 68 | +test("Log in with invalid password", async ({ page }) => { |
| 69 | + await page.goto("/login") |
| 70 | + |
| 71 | + await fillForm(page, "admin@example.com", "changethat") |
| 72 | + await page.getByRole("button", { name: "Log In" }).click() |
| 73 | + |
| 74 | + await expect(page.getByText("Incorrect email or password")).toBeVisible() |
| 75 | +}) |
| 76 | + |
| 77 | +// Log out |
| 78 | + |
| 79 | +test("Successful log out", async ({ page }) => { |
| 80 | + await page.goto("/login") |
| 81 | + |
| 82 | + await fillForm(page, "admin@example.com", "changethis") |
| 83 | + await page.getByRole("button", { name: "Log In" }).click() |
| 84 | + |
| 85 | + await page.waitForURL("/") |
| 86 | + |
| 87 | + await expect( |
| 88 | + page.getByText("Welcome back, nice to see you again!"), |
| 89 | + ).toBeVisible() |
| 90 | + |
| 91 | + await page.getByTestId("user-menu").click() |
| 92 | + await page.getByRole("menuitem", { name: "Log out" }).click() |
| 93 | + await page.waitForURL("/login") |
| 94 | +}) |
| 95 | + |
| 96 | +test("Logged-out user cannot access protected routes", async ({ page }) => { |
| 97 | + await page.goto("/login") |
| 98 | + |
| 99 | + await fillForm(page, "admin@example.com", "changethis") |
| 100 | + await page.getByRole("button", { name: "Log In" }).click() |
| 101 | + |
| 102 | + await page.waitForURL("/") |
| 103 | + |
| 104 | + await expect( |
| 105 | + page.getByText("Welcome back, nice to see you again!"), |
| 106 | + ).toBeVisible() |
| 107 | + |
| 108 | + await page.getByTestId("user-menu").click() |
| 109 | + await page.getByRole("menuitem", { name: "Log out" }).click() |
| 110 | + await page.waitForURL("/login") |
| 111 | + |
| 112 | + await page.goto("/settings") |
| 113 | + await page.waitForURL("/login") |
| 114 | +}) |
0 commit comments