test(e2e): extract shared playwright helpers

This commit is contained in:
2026-07-01 16:31:25 +08:00
parent c477737c0d
commit 1fbdd41da8
11 changed files with 321 additions and 334 deletions
+27 -93
View File
@@ -2,23 +2,18 @@ import { expect, test } from "@playwright/test";
import { mockCoreApis } from "../../fixtures/api-mocks";
import { paidImageMessageId } from "../../fixtures/test-data";
import {
clearBrowserState,
completeVipPayment,
dismissChatInterruptions,
enterChatFromSplash,
expectInsufficientCreditsDialog,
submitEmailLogin,
switchToEmailSignIn,
} from "../../fixtures/test-helpers";
test.beforeEach(async ({ baseURL, context, page }) => {
await context.clearCookies();
const client = await context.newCDPSession(page);
const origins = new Set([
new URL(baseURL ?? "http://127.0.0.1:3000").origin,
"http://127.0.0.1:3000",
"http://localhost:3000",
]);
for (const origin of origins) {
await client
.send("Storage.clearDataForOrigin", {
origin,
storageTypes: "all",
})
.catch(() => {});
}
await clearBrowserState(context, page, baseURL);
await mockCoreApis(page, {
paidImageFlow: true,
});
@@ -61,90 +56,29 @@ test("guest can unlock a paid image after email login and subscription payment",
await expect(page).toHaveURL(/\/auth\?redirect=/);
expect(new URL(page.url()).searchParams.get("redirect")).toBe(
"/subscription?type=vip&returnTo=chat",
`/chat/image/${paidImageMessageId}`,
);
await page.getByRole("button", { name: /Other Sign In Options/i }).click();
await expect(
page.getByRole("dialog", { name: /Other sign-in options/i }),
).toBeVisible();
await page.getByRole("button", { name: "Email Sign In" }).click();
await expect(page.getByPlaceholder("Email")).toHaveValue(
"chanwillian0@gmail.com",
await switchToEmailSignIn(page);
const unlockRequestPromise = page.waitForRequest(
"**/api/chat/unlock-private",
);
await expect(page.getByPlaceholder("Password")).toHaveValue("12345678");
const loginRequestPromise = page.waitForRequest("**/api/auth/login");
await page.getByRole("button", { name: "Login" }).click();
const loginRequest = await loginRequestPromise;
expect(loginRequest.postDataJSON()).toMatchObject({
email: "chanwillian0@gmail.com",
password: "12345678",
isTestAccount: true,
await submitEmailLogin(page, {
expectedUrl: new RegExp(`/chat/image/${paidImageMessageId}$`),
});
const unlockRequest = await unlockRequestPromise;
expect(unlockRequest.postDataJSON()).toMatchObject({
messageId: paidImageMessageId,
});
const insufficientCreditsDialog = await expectInsufficientCreditsDialog(page);
await insufficientCreditsDialog
.getByRole("button", { name: "Top up now" })
.click();
await expect(page).toHaveURL(/\/subscription\?type=vip&returnTo=chat$/);
const createOrderRequestPromise = page.waitForRequest(
"**/api/payment/create-order",
);
const orderStatusRequestPromise = page.waitForRequest(
"**/api/payment/order-status**",
);
await page.getByRole("button", { name: /Pay and Activ/i }).click();
const createOrderRequest = await createOrderRequestPromise;
expect(createOrderRequest.postDataJSON()).toMatchObject({
planId: "vip_monthly",
payChannel: "stripe",
});
await orderStatusRequestPromise;
const paymentSuccessDialog = page.getByRole("alertdialog", {
name: "Payment successful",
});
await expect(paymentSuccessDialog).toBeVisible();
await paymentSuccessDialog.getByRole("button", { name: "OK" }).click();
await completeVipPayment(page);
await expect(page).toHaveURL(new RegExp(`/chat/image/${paidImageMessageId}$`));
});
async function enterChatFromSplash(page: import("@playwright/test").Page) {
await page.goto("/splash");
const skipButton = page.getByRole("button", { name: "Skip" });
await expect(skipButton).toBeVisible({ timeout: 10_000 });
await expect(skipButton).toBeEnabled();
for (let attempt = 0; attempt < 3; attempt += 1) {
await skipButton.click();
try {
await expect(page).toHaveURL(/\/chat$/, { timeout: 3_000 });
return;
} catch {
await page.waitForTimeout(500);
}
}
await expect(page).toHaveURL(/\/chat$/);
}
async function dismissChatInterruptions(page: import("@playwright/test").Page) {
const laterButton = page.getByRole("button", { name: "Later" });
if (await laterButton.isVisible().catch(() => false)) {
await laterButton.click();
}
const cancelButton = page.getByRole("button", { name: "Cancel" });
if (await cancelButton.isVisible().catch(() => false)) {
await cancelButton.click();
}
const okButton = page.getByRole("button", { name: "OK" });
if (await okButton.isVisible().catch(() => false)) {
await okButton.click();
}
}