42 行
1.5 KiB
TypeScript
42 行
1.5 KiB
TypeScript
import { expect, type Locator, type Page } from "@playwright/test";
|
|
|
|
import { TEST_IDS } from "./testIds";
|
|
|
|
export async function openBackupModulesIfNeeded(page: Page) {
|
|
const summary = page.locator("summary").filter({ hasText: "更多" }).first();
|
|
if (await summary.count()) {
|
|
const details = summary.locator("xpath=ancestor::details[1]");
|
|
const isOpen = await details.evaluate((node) => (node as HTMLDetailsElement).open);
|
|
if (!isOpen) {
|
|
await summary.click();
|
|
await expect(details).toHaveAttribute("open", "");
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function openNavLink(page: Page, name: string | RegExp): Promise<Locator> {
|
|
const link = page.getByRole("link", { name }).first();
|
|
if (await link.isVisible().catch(() => false)) {
|
|
return link;
|
|
}
|
|
|
|
await openBackupModulesIfNeeded(page);
|
|
await expect(link).toBeVisible();
|
|
return link;
|
|
}
|
|
|
|
|
|
export async function openProfileSettings(page: Page, userDisplayName?: string) {
|
|
const profileTrigger = page.getByTestId(TEST_IDS.topbar.profileTrigger).first();
|
|
if (await profileTrigger.isVisible().catch(() => false)) {
|
|
await profileTrigger.click();
|
|
} else if (userDisplayName) {
|
|
await page.getByRole("button", { name: userDisplayName, exact: true }).first().click();
|
|
} else {
|
|
throw new Error("无法定位个人设置入口:缺少 topbar profile trigger");
|
|
}
|
|
await expect(page.getByRole("menuitem", { name: "个人设置" })).toBeVisible();
|
|
await page.getByRole("menuitem", { name: "个人设置" }).click();
|
|
await expect(page).toHaveURL(/\/settings\/profile/);
|
|
}
|