完善案例库最终验证修正

This commit is contained in:
czj
2026-07-08 14:22:26 +08:00
parent cf74ad920c
commit fd1b792f2c
5 changed files with 37 additions and 11 deletions
+31 -3
View File
@@ -1,7 +1,12 @@
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { expect, type APIRequestContext, type Page } from "@playwright/test";
import {
expect,
type APIRequestContext,
type Locator,
type Page,
} from "@playwright/test";
import type {
ArticleSample,
@@ -48,10 +53,15 @@ export async function runSamplePageFlow({
await page.goto(baseURL);
await fillIfVisible(page, "访问密钥", apiAccessKey);
await fillFirstAvailable(page, ["文章内容", "粘贴文章", "正文"], sample.input.body);
const bodyField = await fillFirstAvailable(
page,
["文章内容", "粘贴文章", "正文"],
sample.input.body,
);
await fillIfVisible(page, "图片描述或图片链接", sample.input.image_lines);
await page.getByLabel("目标平台").selectOption(sample.input.platform);
await fillIfVisible(page, "用户要求", sample.input.user_instructions);
await ensureSubmitEnabled(page, bodyField, sample.input.body);
await page.getByRole("button", { name: "开始优化" }).click();
@@ -115,13 +125,31 @@ async function fillFirstAvailable(page: Page, labels: string[], value: string) {
const locator = page.getByLabel(label);
if ((await locator.count()) > 0) {
await locator.fill(value);
return;
return locator;
}
}
throw new Error(`none of these labels were found: ${labels.join(", ")}`);
}
async function ensureSubmitEnabled(
page: Page,
bodyField: Locator,
body: string,
) {
const submitButton = page.getByRole("button", { name: "开始优化" });
for (let attempt = 0; attempt < 4; attempt += 1) {
await bodyField.fill("");
await bodyField.fill(body);
if (await submitButton.isEnabled()) {
return;
}
await page.waitForTimeout(250);
}
await expect(bodyField).toHaveValue(body);
await expect(submitButton).toBeEnabled();
}
async function fillIfVisible(page: Page, label: string, value: string) {
const locator = page.getByLabel(label);
if ((await locator.count()) > 0) {