51 lines
2.4 KiB
JavaScript
51 lines
2.4 KiB
JavaScript
'use strict'
|
|
// e2e 条件等待工具(2026-11-06):替代固定 waitForTimeout,消除机器快慢导致的 flaky。
|
|
const { expect } = require('@playwright/test')
|
|
|
|
// 通用轮询:fn 返回布尔/数值/字符串,直到满足期望
|
|
const poll = (page, fn, opts) => expect.poll(fn, Object.assign({ timeout: 8000, intervals: [50, 100, 200, 400] }, opts || {}))
|
|
|
|
// 首页卡片渲染完成(含"我的画布"卡片)
|
|
async function waitHomeReady(page) {
|
|
await expect(page.locator('.iwb-home-card:not(.iwb-home-new)').first()).toBeVisible({ timeout: 15000 })
|
|
}
|
|
// 画布打开完成
|
|
async function waitCanvasOpen(page) {
|
|
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible({ timeout: 15000 })
|
|
}
|
|
// 节点数达到期望(粘贴/删除/撤销后用它替代 sleep)
|
|
async function waitNodeCount(page, n, opts) {
|
|
await poll(page, () => page.locator('.iwb-node').count(), opts).toBe(n)
|
|
}
|
|
// 选中节点数达到期望
|
|
async function waitSelectedCount(page, n, opts) {
|
|
await poll(page, () => page.locator('.iwb-node-selected').count(), opts).toBe(n)
|
|
}
|
|
// 内部剪贴板已写入(Ctrl+C 之后)
|
|
async function waitInternalClipboard(page, opts) {
|
|
await poll(page, () => page.evaluate(() => localStorage.getItem('iwb_clipboard')), opts).not.toBeNull()
|
|
}
|
|
|
|
// 等保存落定:状态栏出现"保存成功/保存失败"(正在保存 → 等它结束;无状态栏也放行)
|
|
async function waitForSaved(page, opts) {
|
|
await poll(page, () => page.evaluate(() => {
|
|
const el = document.querySelector('.iwb-status-save')
|
|
return el ? String(el.textContent || '').trim() : ''
|
|
}), Object.assign({ timeout: 10000 }, opts || {})).not.toBe('正在保存')
|
|
}
|
|
// 等指定文本已经写进 localStorage(保存落盘的精确条件;比状态栏更可靠)
|
|
async function waitForSavedText(page, text, opts) {
|
|
await poll(page, () => page.evaluate((t) => {
|
|
try {
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
const k = localStorage.key(i)
|
|
if (!k || k.indexOf('iwb_') !== 0) continue
|
|
const v = localStorage.getItem(k) || ''
|
|
if (v.indexOf(t) >= 0) return true
|
|
}
|
|
} catch (e) { /* ignore */ }
|
|
return false
|
|
}, text), Object.assign({ timeout: 10000 }, opts || {})).toBe(true)
|
|
}
|
|
module.exports = { waitForSavedText, waitForSaved, poll, waitHomeReady, waitCanvasOpen, waitNodeCount, waitSelectedCount, waitInternalClipboard }
|