From 80d6e801129cde773de935307ca654a01f7aef39 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 10 Jan 2024 02:32:55 +0530 Subject: [PATCH] feat: refactor registration code to be reusable --- tests/auth.spec.ts | 15 +-------------- tests/user.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/tests/auth.spec.ts b/tests/auth.spec.ts index 28632d7..956f604 100644 --- a/tests/auth.spec.ts +++ b/tests/auth.spec.ts @@ -45,22 +45,9 @@ test("Go to register page", async ({ page }) => { // successful registration - // clear past inputs, if any - await page.getByLabel("Username").clear(); - await page.getByLabel("Email Address").clear(); - await page.getByLabel("Password", { exact: true }).clear(); - await page.getByLabel("Confirm Password").clear(); - - await page.getByLabel("Username").fill(user.username); - await page.getByLabel("Email Address").fill(user.email); - await page.getByLabel("Password", { exact: true }).fill(user.password); - await page.getByLabel("Confirm Password").fill(user.password); - await page.getByRole("button", { name: "Register Account" }).click(); + await user.register(page); await page.waitForURL(config.INSTANCE_URL.toString()); - await page.waitForSelector(".positive > p:nth-child(1)"); - console.log(page.locator(".positive > p:nth-child(1)")); - await expect(page).toHaveTitle( `${user.username} - Dashboard - Forgejo: Beyond coding. We forge.` ); diff --git a/tests/user.ts b/tests/user.ts index b07195b..682a180 100644 --- a/tests/user.ts +++ b/tests/user.ts @@ -1,4 +1,6 @@ +import { Page, expect } from "@playwright/test"; import { getRandomString } from "./utils"; +import * as config from "./config"; export default class User { username: string; @@ -12,4 +14,30 @@ export default class User { this.email = `${this.username}@playwright.local`; this.password = "628acdbd8b7b3f"; } + + async register(page: Page) { + await page.goto(config.INSTANCE_URL.toString()); + await page.getByRole("link", { name: "Register" }).click(); + await page.waitForURL("**/user/sign_up"); + + await page.getByLabel("Username").clear(); + await page.getByLabel("Email Address").clear(); + await page.getByLabel("Password", { exact: true }).clear(); + await page.getByLabel("Confirm Password").clear(); + + await page.getByLabel("Username").fill(this.username); + await page.getByLabel("Email Address").fill(this.email); + await page.getByLabel("Password", { exact: true }).fill(this.password); + await page.getByLabel("Confirm Password").fill(this.password); + await page.getByRole("button", { name: "Register Account" }).click(); + await page.waitForURL(config.INSTANCE_URL.toString()); + + await expect(page).toHaveTitle( + `${this.username} - Dashboard - Forgejo: Beyond coding. We forge.` + ); + + expect(page.locator(".positive > p:nth-child(1)")).toContainText( + "Account was successfully created. Welcome!" + ); + } }