import { Page, expect } from "@playwright/test"; import { getRandomString } from "./utils"; import * as config from "./config"; export default class User { username: string; name: string; email: string; password: string; constructor(name: string) { this.name = 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(); 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!" ); } }