forgejo-frontend-integration/tests/user.ts
Aravinth Manivannan eb2353cdb5
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
fix: add sleep to wait for page to load
2024-01-10 21:00:54 +05:30

117 lines
4.1 KiB
TypeScript

import { Page, expect } from "@playwright/test";
import { getRandomString } from "./utils";
import * as config from "./config";
import { getActivationLink } from "./maildev";
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";
}
setAdmin() {
this.username = `plw_${this.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").waitFor();
await dropdown.getByText("Sign Out").click();
await new Promise(r => setTimeout(r, 3000));
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("**/user/sign_up");
// account activation through email
await expect(page.locator("h2.ui")).toContainText("Activate Your Account");
await expect(
page.locator("div.ui:nth-child(3) > p:nth-child(1)")
).toContainText(this.email);
let activationEmail: String | null = null;
while (true) {
let x = await getActivationLink(this.email);
if (typeof x === "string") {
activationEmail = x;
break;
}
}
await page.goto(activationEmail.toString());
// go to forgejo
await page.waitForURL("**/user/activate**");
await page.getByLabel("Password", { exact: true }).fill(this.password);
// click verify account btn
await page.getByRole("button", { name: "Confirm Password" }).click();
// registration successful
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 has been activated"
);
}
}