Add AuthPage fixture

This commit is contained in:
Kloon ImKloon 2023-10-01 14:34:20 +02:00
parent 2da0978386
commit df6b784c1c
No known key found for this signature in database
GPG Key ID: CCF1C86A995C5B6A
2 changed files with 47 additions and 16 deletions

View File

@ -0,0 +1,23 @@
import { type Page, type Locator } from "@playwright/test";
export class AuthPage {
public readonly wrongCredentials: Locator;
constructor(public readonly page: Page) {
this.wrongCredentials = this.page.locator("div.wrong");
}
async goto() {
await this.page.goto("/login");
}
async loginAs(username = "admin", password = "admin") {
await this.page.getByPlaceholder("Username").fill(username);
await this.page.getByPlaceholder("Password").fill(password);
await this.page.getByRole("button", { name: "Login" }).click();
}
async logout() {
await this.page.getByRole("button", { name: "Logout" }).click();
}
}

View File

@ -1,4 +1,15 @@
import { test, expect } from "@playwright/test"; import { test as base, expect } from "@playwright/test";
import { AuthPage } from "./auth-page";
const test = base.extend<{ authPage: AuthPage }>({
authPage: async ({ page }, use) => {
const authPage = new AuthPage(page);
// await authPage.goto();
// await authPage.loginAs();
await use(authPage);
// await authPage.logout();
},
});
test("redirect to login", async ({ page }) => { test("redirect to login", async ({ page }) => {
await page.goto("/"); await page.goto("/");
@ -8,28 +19,25 @@ test("redirect to login", async ({ page }) => {
await expect(page).toHaveURL(/\/login\?redirect=\/files\//); await expect(page).toHaveURL(/\/login\?redirect=\/files\//);
}); });
test("login and logout", async ({ page, context }) => { test("login and logout", async ({ authPage, page, context }) => {
await page.goto("/login"); await authPage.goto();
await expect(page).toHaveTitle("Login - File Browser"); await expect(page).toHaveTitle(/Login - File Browser$/);
await page.getByRole("button", { name: "Login" }).click(); await authPage.loginAs("fake", "fake");
await expect( await expect(authPage.wrongCredentials).toBeVisible();
page.getByText("Wrong credentials", { exact: true })
).toBeVisible();
await page.getByPlaceholder("Username").fill("admin"); await authPage.loginAs();
await page.getByPlaceholder("Password").fill("admin"); await expect(authPage.wrongCredentials).toBeHidden();
await page.getByRole("button", { name: "Login" }).click();
await page.waitForURL("**/files/", { timeout: 5000 }); await page.waitForURL("**/files/", { timeout: 5000 });
await expect(page).toHaveTitle(/.*Files - File Browser/); await expect(page).toHaveTitle(/.*Files - File Browser$/);
let cookies = await context.cookies(); let cookies = await context.cookies();
await expect(cookies.find((c) => c.name == "auth")?.value).toBeDefined(); expect(cookies.find((c) => c.name == "auth")?.value).toBeDefined();
await page.getByRole("button", { name: "Logout" }).click(); await authPage.logout();
await page.waitForURL("**/login", { timeout: 5000 }); await page.waitForURL("**/login", { timeout: 5000 });
await expect(page).toHaveTitle("Login - File Browser"); await expect(page).toHaveTitle(/Login - File Browser$/);
cookies = await context.cookies(); cookies = await context.cookies();
await expect(cookies.find((c) => c.name == "auth")?.value).toBeUndefined(); expect(cookies.find((c) => c.name == "auth")?.value).toBeUndefined();
}); });