73 行
1.9 KiB
TypeScript
73 行
1.9 KiB
TypeScript
import { expect, type Page } from "@playwright/test";
|
|
|
|
import { logout } from "./auth";
|
|
|
|
export type LiveSmokeRole = "admin" | "employee";
|
|
|
|
export type LiveSmokeUser = {
|
|
role: LiveSmokeRole;
|
|
identifier: string;
|
|
password: string;
|
|
name: string;
|
|
};
|
|
|
|
function requireEnv(key: string) {
|
|
const value = process.env[key]?.trim();
|
|
if (!value) {
|
|
throw new Error(`缺少 live smoke 环境变量:${key}`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export function getExpectedWecomStatus() {
|
|
return process.env.LIVE_EXPECT_WECOM_STATUS?.trim() || "disabled";
|
|
}
|
|
|
|
export function getLiveSmokeUser(role: LiveSmokeRole) {
|
|
if (role === "admin") {
|
|
return {
|
|
role,
|
|
identifier: requireEnv("LIVE_SMOKE_ADMIN_IDENTIFIER"),
|
|
password: requireEnv("LIVE_SMOKE_ADMIN_PASSWORD"),
|
|
};
|
|
}
|
|
|
|
return {
|
|
role,
|
|
identifier: requireEnv("LIVE_SMOKE_EMPLOYEE_IDENTIFIER"),
|
|
password: requireEnv("LIVE_SMOKE_EMPLOYEE_PASSWORD"),
|
|
};
|
|
}
|
|
|
|
async function readCurrentUserName(page: Page): Promise<string> {
|
|
const profileTrigger = page.getByTestId("topbar-profile-trigger").first();
|
|
await expect(profileTrigger).toBeVisible();
|
|
const name = (await profileTrigger.textContent())?.trim();
|
|
if (!name) {
|
|
throw new Error("无法从顶栏读取当前登录用户名称");
|
|
}
|
|
return name;
|
|
}
|
|
|
|
export async function loginLiveUser(page: Page, role: LiveSmokeRole): Promise<LiveSmokeUser> {
|
|
const credentials = getLiveSmokeUser(role);
|
|
|
|
await page.goto("/login");
|
|
await expect(page.getByTestId("login-identifier")).toBeVisible();
|
|
await page.getByTestId("login-identifier").fill(credentials.identifier);
|
|
await page.getByTestId("login-password").fill(credentials.password);
|
|
await page.getByTestId("login-submit").click();
|
|
await expect(page).toHaveURL(/\/portal/);
|
|
|
|
const name = await readCurrentUserName(page);
|
|
|
|
return {
|
|
...credentials,
|
|
name,
|
|
};
|
|
}
|
|
|
|
export async function logoutLiveUser(page: Page, user?: Pick<LiveSmokeUser, "name">) {
|
|
await logout(page, user?.name);
|
|
}
|