|
| 1 | +import { type Page, expect, test } from "@playwright/test" |
| 2 | + |
| 3 | +import { randomEmail } from "./utils/random" |
| 4 | + |
| 5 | +test.use({ storageState: { cookies: [], origins: [] } }) |
| 6 | + |
| 7 | +type OptionsType = { |
| 8 | + exact?: boolean |
| 9 | +} |
| 10 | + |
| 11 | +const fillForm = async ( |
| 12 | + page: Page, |
| 13 | + full_name: string, |
| 14 | + email: string, |
| 15 | + password: string, |
| 16 | + confirm_password: string, |
| 17 | +) => { |
| 18 | + await page.getByPlaceholder("Full Name").fill(full_name) |
| 19 | + await page.getByPlaceholder("Email").fill(email) |
| 20 | + await page.getByPlaceholder("Password", { exact: true }).fill(password) |
| 21 | + await page.getByPlaceholder("Repeat Password").fill(confirm_password) |
| 22 | +} |
| 23 | + |
| 24 | +const verifyInput = async ( |
| 25 | + page: Page, |
| 26 | + placeholder: string, |
| 27 | + options?: OptionsType, |
| 28 | +) => { |
| 29 | + const input = page.getByPlaceholder(placeholder, options) |
| 30 | + await expect(input).toBeVisible() |
| 31 | + await expect(input).toHaveText("") |
| 32 | + await expect(input).toBeEditable() |
| 33 | +} |
| 34 | + |
| 35 | +test("Inputs are visible, empty and editable", async ({ page }) => { |
| 36 | + await page.goto("/signup") |
| 37 | + |
| 38 | + await verifyInput(page, "Full Name") |
| 39 | + await verifyInput(page, "Email") |
| 40 | + await verifyInput(page, "Password", { exact: true }) |
| 41 | + await verifyInput(page, "Repeat Password") |
| 42 | +}) |
| 43 | + |
| 44 | +test("Sign Up button is visible", async ({ page }) => { |
| 45 | + await page.goto("/signup") |
| 46 | + |
| 47 | + await expect(page.getByRole("button", { name: "Sign Up" })).toBeVisible() |
| 48 | +}) |
| 49 | + |
| 50 | +test("Log In link is visible", async ({ page }) => { |
| 51 | + await page.goto("/signup") |
| 52 | + |
| 53 | + await expect(page.getByRole("link", { name: "Log In" })).toBeVisible() |
| 54 | +}) |
| 55 | + |
| 56 | +test("Sign up with valid name, email, and password", async ({ page }) => { |
| 57 | + const full_name = "Test User" |
| 58 | + const email = randomEmail() |
| 59 | + const password = "changethis" |
| 60 | + |
| 61 | + await page.goto("/signup") |
| 62 | + await fillForm(page, full_name, email, password, password) |
| 63 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 64 | +}) |
| 65 | + |
| 66 | +test("Sign up with invalid email", async ({ page }) => { |
| 67 | + await page.goto("/signup") |
| 68 | + |
| 69 | + await fillForm( |
| 70 | + page, |
| 71 | + "Playwright Test", |
| 72 | + "invalid-email", |
| 73 | + "changethis", |
| 74 | + "changethis", |
| 75 | + ) |
| 76 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 77 | + |
| 78 | + await expect(page.getByText("Invalid email address")).toBeVisible() |
| 79 | +}) |
| 80 | + |
| 81 | +test("Sign up with existing email", async ({ page }) => { |
| 82 | + const full_name = "Test User" |
| 83 | + const email = randomEmail() |
| 84 | + const password = "changethis" |
| 85 | + |
| 86 | + // Sign up with an email |
| 87 | + await page.goto("/signup") |
| 88 | + |
| 89 | + await fillForm(page, full_name, email, password, password) |
| 90 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 91 | + |
| 92 | + // Sign up again with the same email |
| 93 | + await page.goto("/signup") |
| 94 | + |
| 95 | + await fillForm(page, full_name, email, password, password) |
| 96 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 97 | + |
| 98 | + await page |
| 99 | + .getByText("The user with this email already exists in the system") |
| 100 | + .click() |
| 101 | +}) |
| 102 | + |
| 103 | +test("Sign up with weak password", async ({ page }) => { |
| 104 | + const full_name = "Test User" |
| 105 | + const email = randomEmail() |
| 106 | + const password = "weak" |
| 107 | + |
| 108 | + await page.goto("/signup") |
| 109 | + |
| 110 | + await fillForm(page, full_name, email, password, password) |
| 111 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 112 | + |
| 113 | + await expect( |
| 114 | + page.getByText("Password must be at least 8 characters"), |
| 115 | + ).toBeVisible() |
| 116 | +}) |
| 117 | + |
| 118 | +test("Sign up with mismatched passwords", async ({ page }) => { |
| 119 | + const full_name = "Test User" |
| 120 | + const email = randomEmail() |
| 121 | + const password = "changethis" |
| 122 | + const password2 = "changethat" |
| 123 | + |
| 124 | + await page.goto("/signup") |
| 125 | + |
| 126 | + await fillForm(page, full_name, email, password, password2) |
| 127 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 128 | + |
| 129 | + await expect(page.getByText("Passwords do not match")).toBeVisible() |
| 130 | +}) |
| 131 | + |
| 132 | +test("Sign up with missing full name", async ({ page }) => { |
| 133 | + const full_name = "" |
| 134 | + const email = randomEmail() |
| 135 | + const password = "changethis" |
| 136 | + |
| 137 | + await page.goto("/signup") |
| 138 | + |
| 139 | + await fillForm(page, full_name, email, password, password) |
| 140 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 141 | + |
| 142 | + await expect(page.getByText("Full Name is required")).toBeVisible() |
| 143 | +}) |
| 144 | + |
| 145 | +test("Sign up with missing email", async ({ page }) => { |
| 146 | + const full_name = "Test User" |
| 147 | + const email = "" |
| 148 | + const password = "changethis" |
| 149 | + |
| 150 | + await page.goto("/signup") |
| 151 | + |
| 152 | + await fillForm(page, full_name, email, password, password) |
| 153 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 154 | + |
| 155 | + await expect(page.getByText("Email is required")).toBeVisible() |
| 156 | +}) |
| 157 | + |
| 158 | +test("Sign up with missing password", async ({ page }) => { |
| 159 | + const full_name = "" |
| 160 | + const email = randomEmail() |
| 161 | + const password = "" |
| 162 | + |
| 163 | + await page.goto("/signup") |
| 164 | + |
| 165 | + await fillForm(page, full_name, email, password, password) |
| 166 | + await page.getByRole("button", { name: "Sign Up" }).click() |
| 167 | + |
| 168 | + await expect(page.getByText("Password is required")).toBeVisible() |
| 169 | +}) |
0 commit comments