From 28cfa742f268bb467e613201b945ba2962db5153 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 10 Jan 2024 03:01:10 +0530 Subject: [PATCH] feat: test sign out and sign in --- tests/auth.spec.ts | 45 +++++++++++++++++++++++++++++++++++++++++++-- tests/user.ts | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/tests/auth.spec.ts b/tests/auth.spec.ts index 956f604..1ff6b55 100644 --- a/tests/auth.spec.ts +++ b/tests/auth.spec.ts @@ -19,8 +19,8 @@ test("Go to explore page", async ({ page }) => { await expect(page).toHaveTitle("Explore - Forgejo: Beyond coding. We forge."); }); -test("Go to register page", async ({ page }) => { - const user = new User("gotoregisterpage"); +test("Test registration error: password mismatch", async ({ page }) => { + const user = new User("pwmismatch"); console.log(`running with user ${user.username}`); @@ -42,6 +42,14 @@ test("Go to register page", async ({ page }) => { expect(page.locator(".negative > p:nth-child(1)")).toContainText( "passwords do not match" ); +}); + +test("Test successful registration", async ({ page }) => { + const user = new User("register"); + + console.log(`running with user ${user.username}`); + + await page.goto(config.INSTANCE_URL.toString()); // successful registration @@ -56,3 +64,36 @@ test("Go to register page", async ({ page }) => { "Account was successfully created. Welcome!" ); }); + +test("Test signout", async ({ page }) => { + const user = new User("signout"); + + // successful registration + await user.register(page); + await user.logout(page); + await expect(page).toHaveTitle("Forgejo: Beyond coding. We forge."); +}); + +test("Test login", async ({ page }) => { + const user = new User("login"); + + // successful registration + await user.register(page); + await user.logout(page); + await user.login(page); + await user.logout(page); + + // sign in with email + await page.goto(config.INSTANCE_URL.toString()); + await page.getByRole("link", { name: "Sign In" }).click(); + await page.waitForURL("**/user/login**"); + await page.getByLabel("Username").clear(); + await page.getByLabel("Password", { exact: true }).clear(); + await page.getByLabel("Username").fill(user.email); + await page.getByLabel("Password", { exact: true }).fill(user.password); + await page.getByRole("button", { name: "Sign In" }).click(); + + await expect(page).toHaveTitle( + `${user.username} - Dashboard - Forgejo: Beyond coding. We forge.` + ); +}); diff --git a/tests/user.ts b/tests/user.ts index 682a180..27b8a2b 100644 --- a/tests/user.ts +++ b/tests/user.ts @@ -10,11 +10,46 @@ export default class User { constructor(name: string) { this.name = name; - this.username = `playwright_${getRandomString(6)}_${name}`; + this.username = `plw_${getRandomString(6)}_${name}`; + if (this.username.length > 40) { + throw Error(`username must be less than 40 char. Reduse length of ${name}`) + } this.email = `${this.username}@playwright.local`; this.password = "628acdbd8b7b3f"; } + async logout(page: Page) { + await page.goto(config.INSTANCE_URL.toString()); + // going to explore page to wait for sign out to be successful. Change in + // URL must indicate successful sign out. + await page.getByRole("link", { name: "Explore" }).click(); + await page.waitForURL("**/explore/repos"); + + let dropdown = page.locator("div.dropdown:nth-child(4)"); + await dropdown.click(); +// await dropdown.getByRole("link", { name: "Sign Out" }).click(); + await dropdown.getByText("Sign Out").click(); + await page.waitForURL(config.INSTANCE_URL.toString()); + + await expect(page).toHaveTitle("Forgejo: Beyond coding. We forge."); + } + + async login(page: Page) { + await page.goto(config.INSTANCE_URL.toString()); + await page.getByRole("link", { name: "Sign In" }).click(); + await page.waitForURL("**/user/login**"); + await page.getByLabel("Username").clear(); + await page.getByLabel("Password", { exact: true }).clear(); + await page.getByLabel("Username").fill(this.username); + await page.getByLabel("Password", { exact: true }).fill(this.password); + await page.getByRole("button", { name: "Sign In" }).click(); + + await expect(page).toHaveTitle( + `${this.username} - Dashboard - Forgejo: Beyond coding. We forge.` + ); + } + + async register(page: Page) { await page.goto(config.INSTANCE_URL.toString()); await page.getByRole("link", { name: "Register" }).click();