508 lines
20 KiB
JavaScript
508 lines
20 KiB
JavaScript
import { chromium } from "playwright";
|
|
|
|
const baseURL = (process.env.BASE_URL || process.argv[2] || "http://114.111.24.223:30080").replace(/\/+$/, "");
|
|
const apiBaseURL = `${baseURL}`;
|
|
const suffix = Date.now().toString(36);
|
|
|
|
const names = {
|
|
admin: `PW Admin ${suffix}`,
|
|
adminEmail: `pw-admin-${suffix}@playwright.local`,
|
|
extraUser: `PW Operator ${suffix}`,
|
|
extraUserEmail: `pw-operator-${suffix}@playwright.local`,
|
|
project: `PW Project ${suffix}`,
|
|
workflow: `PW Workflow ${suffix}`,
|
|
template: `PW Template ${suffix}`,
|
|
projectTemplate: `PW Project Template ${suffix}`,
|
|
auditProject: `PW Audit ${suffix}`,
|
|
};
|
|
|
|
function log(step) {
|
|
process.stdout.write(`[playwright] ${step}\n`);
|
|
}
|
|
|
|
function assert(condition, message) {
|
|
if (!condition) {
|
|
throw new Error(message);
|
|
}
|
|
}
|
|
|
|
async function api(path, init = {}) {
|
|
let lastError;
|
|
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
|
try {
|
|
const response = await fetch(`${apiBaseURL}${path}`, {
|
|
...init,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...(init.headers || {}),
|
|
},
|
|
});
|
|
const text = await response.text();
|
|
if (!response.ok) {
|
|
throw new Error(`${init.method || "GET"} ${path} failed: ${response.status} ${text}`);
|
|
}
|
|
if (!text) {
|
|
return null;
|
|
}
|
|
return JSON.parse(text);
|
|
} catch (error) {
|
|
lastError = error;
|
|
if (attempt === 6) {
|
|
break;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 1000));
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|
|
|
|
async function poll(fn, predicate, label, timeoutMs = 120000, intervalMs = 2000) {
|
|
const deadline = Date.now() + timeoutMs;
|
|
let lastValue;
|
|
while (Date.now() < deadline) {
|
|
lastValue = await fn();
|
|
if (predicate(lastValue)) {
|
|
return lastValue;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
}
|
|
throw new Error(`Timed out waiting for ${label}. Last value: ${JSON.stringify(lastValue)}`);
|
|
}
|
|
|
|
async function gotoWithRetry(page, url) {
|
|
let lastError;
|
|
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
|
try {
|
|
await page.goto(url, { waitUntil: "networkidle" });
|
|
return;
|
|
} catch (error) {
|
|
lastError = error;
|
|
if (attempt === 6) {
|
|
break;
|
|
}
|
|
await page.waitForTimeout(Math.min(attempt, 3) * 1000);
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|
|
|
|
async function clickWithRetry(page, locator) {
|
|
let lastError;
|
|
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
|
try {
|
|
await locator.waitFor({ state: "visible" });
|
|
await locator.scrollIntoViewIfNeeded();
|
|
const box = await locator.boundingBox();
|
|
if (!box) {
|
|
throw new Error("Unable to resolve click target bounding box");
|
|
}
|
|
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
|
|
return;
|
|
} catch (error) {
|
|
lastError = error;
|
|
if (attempt === 6) {
|
|
break;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 500));
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|
|
|
|
async function cleanupTestData() {
|
|
log("cleanup test data");
|
|
const users = (await api("/api/users")).items;
|
|
for (const user of users) {
|
|
if (String(user.email || "").endsWith("@playwright.local")) {
|
|
await api(`/api/users/${encodeURIComponent(user.id)}`, { method: "DELETE" });
|
|
}
|
|
}
|
|
|
|
const projects = (await api("/api/projects")).items;
|
|
for (const project of projects) {
|
|
if (String(project.name || "").startsWith("PW ")) {
|
|
await api(`/api/projects/${encodeURIComponent(project.id)}`, { method: "DELETE" });
|
|
}
|
|
}
|
|
|
|
const templates = (await api("/api/templates")).items;
|
|
for (const template of templates) {
|
|
if (String(template.name || "").startsWith("PW ")) {
|
|
await api(`/api/templates/${encodeURIComponent(template.id)}`, { method: "DELETE" });
|
|
}
|
|
}
|
|
|
|
const operators = (await api("/api/operators")).items;
|
|
for (const operator of operators) {
|
|
if (String(operator.name || "").startsWith("PW ")) {
|
|
await api(`/api/operators/${encodeURIComponent(operator.id)}`, { method: "DELETE" });
|
|
}
|
|
}
|
|
}
|
|
|
|
async function createAuditWorkflow(projectName, workflowName) {
|
|
const project = await api("/api/projects", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
name: projectName,
|
|
description: "Created by Playwright audit setup",
|
|
}),
|
|
});
|
|
|
|
const workflow = await api(`/api/projects/${project.id}/workflows`, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
name: workflowName,
|
|
description: "Audit workflow created by Playwright",
|
|
type: "batch",
|
|
nodes: [
|
|
{
|
|
id: "review",
|
|
type: "operator",
|
|
data: {
|
|
label: "人工审核 (Human Review)",
|
|
category: "HUMAN",
|
|
description: "review",
|
|
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
|
outputs: [{ id: "approved", name: "审核通过", type: "json" }],
|
|
},
|
|
position: { x: 0, y: 0 },
|
|
},
|
|
{
|
|
id: "upload",
|
|
type: "operator",
|
|
data: {
|
|
label: "结果上传",
|
|
category: "IO",
|
|
description: "sink",
|
|
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
|
outputs: [],
|
|
},
|
|
position: { x: 200, y: 0 },
|
|
},
|
|
],
|
|
edges: [{ id: "review-upload", source: "review", sourceHandle: "approved", target: "upload", targetHandle: "in-1" }],
|
|
}),
|
|
});
|
|
|
|
const run = await api(`/api/workflows/${workflow.id}/run`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ trigger: "manual" }),
|
|
});
|
|
|
|
await poll(
|
|
async () => api(`/api/runs/${run.id}`),
|
|
(item) => item.status === "waiting_for_audit",
|
|
`audit run ${run.id} to enter waiting_for_audit`,
|
|
120000,
|
|
3000,
|
|
);
|
|
|
|
return { project, workflow, run };
|
|
}
|
|
|
|
async function fillLabeledInput(page, labelText, value) {
|
|
const group = page.locator(`label:has-text("${labelText}")`).locator("..");
|
|
const input = group.locator("input, textarea, select").first();
|
|
await input.click();
|
|
await input.fill(value);
|
|
}
|
|
|
|
async function main() {
|
|
await cleanupTestData();
|
|
|
|
const browser = await chromium.launch({ headless: true });
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 1100 } });
|
|
const page = await context.newPage();
|
|
|
|
try {
|
|
log("open bootstrap");
|
|
await gotoWithRetry(page, baseURL);
|
|
const bootstrapVisible = await page
|
|
.getByText("新增真实平台用户")
|
|
.isVisible({ timeout: 5000 })
|
|
.catch(() => false);
|
|
if (bootstrapVisible) {
|
|
await page.getByPlaceholder("输入真实姓名或账号名").fill(names.admin);
|
|
await page.getByPlaceholder("name@example.com").fill(names.adminEmail);
|
|
await page.locator("select").first().selectOption("admin");
|
|
await page.getByRole("button", { name: "创建并进入系统" }).click();
|
|
}
|
|
await page.getByRole("heading", { name: "DroboticFlow 仪表盘" }).waitFor();
|
|
|
|
log("settings save round-trip");
|
|
await page.getByRole("link", { name: "系统配置" }).click();
|
|
await page.getByRole("heading", { name: "系统配置" }).waitFor();
|
|
const pollGroup = page.locator('label:has-text("轮询间隔 (ms)")').locator("..");
|
|
const pollInput = pollGroup.locator("input");
|
|
const originalPoll = Number(await pollInput.inputValue());
|
|
await pollInput.fill(String(originalPoll + 1));
|
|
await page.getByRole("button", { name: /保存配置/ }).click();
|
|
await page.getByText("配置已保存到后端。").waitFor();
|
|
await pollInput.fill(String(originalPoll));
|
|
await page.getByRole("button", { name: /保存配置/ }).click();
|
|
await page.getByText("配置已保存到后端。").waitFor();
|
|
|
|
log("operators create edit delete");
|
|
await page.getByRole("link", { name: "算子管理" }).click();
|
|
await page.getByRole("heading", { name: "算子管理" }).waitFor();
|
|
await page.getByRole("button", { name: "注册算子" }).click();
|
|
await page.getByPlaceholder("例如: 图像锐化").fill(`PW Operator ${suffix}`);
|
|
await page.getByPlaceholder("描述该算子的功能与用途...").fill("Playwright created operator");
|
|
await page.getByRole("button", { name: "确认注册" }).click();
|
|
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
|
await page.getByText(`PW Operator ${suffix}`).waitFor();
|
|
const operatorCard = page.locator("div").filter({ hasText: `PW Operator ${suffix}` }).first();
|
|
await operatorCard.getByRole("button", { name: /配置详情|查看详情/ }).click();
|
|
await page.getByPlaceholder("描述该算子的功能与用途...").fill("Playwright updated operator");
|
|
await page.getByRole("button", { name: "保存修改" }).click();
|
|
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
|
await page.locator("button:has(svg.lucide-trash-2)").first().click();
|
|
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
|
await page.getByText("没有匹配的算子,请调整搜索条件。").waitFor();
|
|
await page.getByPlaceholder("搜索算子...").fill("");
|
|
|
|
log("projects create and open");
|
|
await page.getByRole("link", { name: "项目管理" }).click();
|
|
await page.getByRole("heading", { name: "项目管理" }).waitFor();
|
|
await page.getByRole("button", { name: "创建项目" }).click();
|
|
await page.getByPlaceholder("请输入项目名称").fill(names.project);
|
|
await page.getByPlaceholder("请输入项目描述,例如:处理机械臂抓取过程中的视觉数据").fill("Playwright project");
|
|
await page.getByRole("button", { name: "立即创建" }).click();
|
|
await page.getByPlaceholder("搜索项目...").fill(names.project);
|
|
await page.getByText(names.project).waitFor();
|
|
|
|
const projectList = (await api("/api/projects")).items;
|
|
const project = projectList.find((item) => item.name === names.project);
|
|
assert(project, "created project not found via api");
|
|
await gotoWithRetry(page, `${baseURL}/project/${project.id}`);
|
|
|
|
log("project detail create workflow");
|
|
await page.getByRole("heading", { name: names.project }).waitFor();
|
|
await page.getByRole("button", { name: "新建工作流" }).click();
|
|
await page.getByPlaceholder("例如: 实时障碍物检测").fill(names.workflow);
|
|
await page.getByRole("button", { name: "确认创建" }).click();
|
|
await page.getByRole("button", { name: "运行工作流" }).waitFor();
|
|
|
|
const workflows = (await api(`/api/projects/${project.id}/workflows`)).items;
|
|
const workflow = workflows.find((item) => item.name === names.workflow);
|
|
assert(workflow, "created workflow not found via api");
|
|
|
|
log("seed workflow graph");
|
|
await api(`/api/workflows/${workflow.id}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify({
|
|
nodes: [
|
|
{
|
|
id: "source-1",
|
|
type: "operator",
|
|
data: {
|
|
label: "真实数据源",
|
|
category: "IO",
|
|
description: "Playwright source node",
|
|
inputs: [],
|
|
outputs: [{ id: "out-1", name: "manifest", type: "json" }],
|
|
hyperparams: [{ key: "SOURCE_TEXT", value: `playwright-${suffix}`, description: "直接注入文本内容" }],
|
|
},
|
|
position: { x: 100, y: 100 },
|
|
},
|
|
{
|
|
id: "review-1",
|
|
type: "operator",
|
|
data: {
|
|
label: "人工审核 (Human Review)",
|
|
category: "HUMAN",
|
|
description: "Playwright review node",
|
|
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
|
outputs: [
|
|
{ id: "approved", name: "审核通过", type: "json" },
|
|
{ id: "rejected", name: "审核拒绝", type: "json" },
|
|
],
|
|
},
|
|
position: { x: 360, y: 100 },
|
|
},
|
|
{
|
|
id: "upload-1",
|
|
type: "operator",
|
|
data: {
|
|
label: "结果上传",
|
|
category: "IO",
|
|
description: "Playwright upload node",
|
|
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
|
outputs: [],
|
|
},
|
|
position: { x: 620, y: 100 },
|
|
},
|
|
],
|
|
edges: [
|
|
{
|
|
id: "source-review",
|
|
source: "source-1",
|
|
sourceHandle: "out-1",
|
|
target: "review-1",
|
|
targetHandle: "in-1",
|
|
},
|
|
{
|
|
id: "review-upload",
|
|
source: "review-1",
|
|
sourceHandle: "approved",
|
|
target: "upload-1",
|
|
targetHandle: "in-1",
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
|
|
await page.reload({ waitUntil: "networkidle" });
|
|
await page.getByText("真实数据源").dblclick();
|
|
const sourceGroup = page.locator('label:has-text("SOURCE_TEXT")').locator("..").locator("..");
|
|
await sourceGroup.locator("input").fill(`playwright-ui-${suffix}`);
|
|
await page.getByRole("button", { name: "保存" }).click();
|
|
|
|
log("run workflow from editor without redirect");
|
|
await page.getByRole("button", { name: "运行工作流" }).click();
|
|
await page.getByRole("button", { name: "查看执行监控" }).waitFor();
|
|
assert(/\/workflow\//.test(page.url()), `expected to stay on workflow editor, got ${page.url()}`);
|
|
await page.getByText(/当前节点:/).waitFor();
|
|
await poll(
|
|
async () => page.locator(".react-flow__edge.animated").count(),
|
|
(count) => count > 0,
|
|
"animated workflow edge on editor",
|
|
30000,
|
|
1000,
|
|
);
|
|
await poll(
|
|
async () => {
|
|
const runList = (await api("/api/runs")).items;
|
|
return runList.find((item) => item.workflowName === names.workflow) || null;
|
|
},
|
|
(item) => Boolean(item?.id && item.status === "waiting_for_audit"),
|
|
`editor run for ${names.workflow} waiting_for_audit`,
|
|
120000,
|
|
2000,
|
|
);
|
|
const editorRun = (await api("/api/runs")).items.find((item) => item.workflowName === names.workflow);
|
|
assert(editorRun, "editor run not found via api");
|
|
await page.getByRole("button", { name: "查看执行监控" }).click();
|
|
await page.waitForURL(new RegExp(`/monitoring\\?runId=${editorRun.id}`));
|
|
await page.getByRole("heading", { name: "执行监控" }).waitFor();
|
|
await page.getByPlaceholder("搜索运行 ID、工作流或项目...").fill(names.workflow);
|
|
await page.getByText(names.workflow).waitFor();
|
|
const runRow = page.locator("tr").filter({ hasText: names.workflow }).first();
|
|
await runRow.hover();
|
|
await runRow.getByTitle("查看日志").click();
|
|
await page.getByRole("heading", { name: "执行日志" }).waitFor();
|
|
await page.getByRole("button", { name: "关闭" }).click();
|
|
await runRow.hover();
|
|
await runRow.getByTitle("查看资源").click();
|
|
await page.getByRole("heading", { name: "资源监控" }).waitFor();
|
|
await page.getByRole("button", { name: "关闭" }).click();
|
|
await runRow.hover();
|
|
await runRow.getByTitle("查看数据").click();
|
|
await page.getByRole("heading", { name: "产出数据" }).waitFor();
|
|
await page.getByRole("button", { name: "关闭" }).click();
|
|
|
|
const pendingAudit = await poll(
|
|
async () => {
|
|
const tasks = (await api("/api/audit-tasks?status=all")).items;
|
|
return tasks.find((task) => task.runId === editorRun.id && task.status === "pending") || null;
|
|
},
|
|
(item) => Boolean(item?.id),
|
|
`audit task for editor run ${editorRun.id}`,
|
|
120000,
|
|
2000,
|
|
);
|
|
await api(`/api/audit-tasks/${pendingAudit.id}/approve`, { method: "POST" });
|
|
await poll(
|
|
async () => api(`/api/runs/${editorRun.id}`),
|
|
(item) => item.status === "completed",
|
|
`editor run ${editorRun.id} completion after approval`,
|
|
120000,
|
|
2000,
|
|
);
|
|
|
|
log("export template from workflow editor");
|
|
await runRow.dblclick();
|
|
await page.getByRole("button", { name: "导出模板" }).click();
|
|
await page.locator('input').filter({ has: page.locator('.. >> text=模板名称') }).first().fill(names.template);
|
|
await page.getByRole("button", { name: "创建模板" }).click();
|
|
await page.waitForURL(/type=template/);
|
|
|
|
log("templates search and use");
|
|
await page.getByRole("link", { name: "工作流模板" }).click();
|
|
await page.getByRole("heading", { name: "工作流模板" }).waitFor();
|
|
await page.getByPlaceholder("搜索模板...").fill(names.template);
|
|
await page.getByText(names.template).waitFor();
|
|
const templateCard = page.locator("div").filter({ hasText: names.template }).first();
|
|
await templateCard
|
|
.locator("button")
|
|
.filter({ hasText: /^使用此模板$/ })
|
|
.click();
|
|
await page.getByText("选择目标项目").waitFor();
|
|
await page.getByRole("button", { name: names.project }).click();
|
|
await page.getByRole("button", { name: "确认并进入编辑器" }).click();
|
|
await page.waitForURL(/\/workflow\//);
|
|
|
|
log("project detail save as template");
|
|
await gotoWithRetry(page, `${baseURL}/project/${project.id}`);
|
|
await page.getByRole("button", { name: "保存为模板" }).click();
|
|
await page.getByRole("heading", { name: "保存为模板" }).waitFor();
|
|
await page.locator("select").last().selectOption(workflow.id);
|
|
await page.locator("input").last().fill(names.projectTemplate);
|
|
await page.getByRole("button", { name: "保存模板" }).click();
|
|
await page.waitForURL(/type=template/);
|
|
|
|
log("users create edit delete");
|
|
await page.getByRole("link", { name: "用户管理" }).click();
|
|
await page.getByRole("heading", { name: "用户管理" }).waitFor();
|
|
await clickWithRetry(page, page.locator("button").filter({ hasText: /^创建用户$/ }).first());
|
|
await page.getByRole("heading", { name: "创建用户" }).waitFor();
|
|
await fillLabeledInput(page, "用户名", names.extraUser);
|
|
await fillLabeledInput(page, "邮箱地址", names.extraUserEmail);
|
|
await page.locator('label:has-text("角色权限")').locator("..").locator("select").selectOption("operator");
|
|
await clickWithRetry(page, page.locator("button").filter({ hasText: /^创建用户$/ }).last());
|
|
await page.getByText(names.extraUser).waitFor();
|
|
const userRow = page.locator("tr").filter({ hasText: names.extraUser }).first();
|
|
await userRow.getByRole("button", { name: "编辑" }).click();
|
|
await page.locator('label:has-text("角色权限")').locator("..").locator("select").selectOption("auditor");
|
|
await page.getByRole("button", { name: "保存修改" }).click();
|
|
await userRow.getByRole("button", { name: "删除" }).click();
|
|
await page.waitForTimeout(500);
|
|
|
|
log("audit approve and reject");
|
|
const auditOne = await createAuditWorkflow(`${names.auditProject} A`, `PW Audit Workflow A ${suffix}`);
|
|
const auditTwo = await createAuditWorkflow(`${names.auditProject} B`, `PW Audit Workflow B ${suffix}`);
|
|
await gotoWithRetry(page, `${baseURL}/audit`);
|
|
await page.getByRole("heading", { name: /任务审核/ }).waitFor();
|
|
await page.getByText(`${names.auditProject} A`).click();
|
|
await page.getByRole("button", { name: /审核通过/ }).click();
|
|
await page.getByText(`${names.auditProject} B`).click();
|
|
await page.getByRole("button", { name: /审核拒绝/ }).click();
|
|
await page.getByRole("button", { name: "已处理" }).click();
|
|
|
|
await poll(
|
|
async () => api(`/api/runs/${auditOne.run.id}`),
|
|
(item) => item.status === "completed",
|
|
`audit run ${auditOne.run.id} approved completion`,
|
|
);
|
|
await poll(
|
|
async () => api(`/api/runs/${auditTwo.run.id}`),
|
|
(item) => item.status === "completed",
|
|
`audit run ${auditTwo.run.id} rejected completion`,
|
|
);
|
|
|
|
log("functional check completed");
|
|
} catch (error) {
|
|
await page.screenshot({ path: `/tmp/droboticflow-playwright-failure-${suffix}.png`, fullPage: true }).catch(() => {});
|
|
throw error;
|
|
} finally {
|
|
await context.close();
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|