import { expect, type Page } from './fixtures';
/**
* 进入一个 cube-vue å¶å页(列表 / 对象 / 自定义视图å‡å¯ï¼‰ã€‚
* 路由为 history 模å¼ï¼š/<path>,path 如 Platform/GalaxyProject 或 Admin/Index/Main。
* èœå•路由在登录åŽå¼‚æ¥æ³¨å†Œï¼ŒbeforeEach 守å«ä¼šé‡å¯¼èˆªï¼Œæ•…需ç‰å¾…主区域渲染出内容。
*/
export async function gotoLeafPage(page: Page, path: string): Promise<void> {
await page.goto(`/${path.replace(/^\/+/, '')}`, { waitUntil: 'domcontentloaded' });
// ç‰å¾…列表页 / 对象页 / 自定义视图(主区域有渲染内容)出现
await page.waitForFunction(
() => {
const c = document.querySelector('main.cosmic-content');
if (!c) return false;
// AutoPage 分æµä¸çš„ã€ŒåŠ è½½ä¸ã€å ä½ä¸ç®—渲染完æˆ
if (c.querySelector('.autopage-loading')) return false;
if (c.querySelector('.list-page')) return true;
if (c.querySelector('.object-page .form-content')) return true;
// 自定义视图:主区域内有实际渲染内容(且éžåŠ è½½å ä½ï¼‰
return c.children.length > 0 && (c.innerText ?? '').trim().length > 0;
},
{ timeout: 25_000 },
);
}
/** æ–è¨€åˆ—è¡¨é¡µå·²æ¸²æŸ“ï¼šæ ‡é¢˜ + è¡¨æ ¼å¯è§ã€‚ */
export async function assertListRendered(page: Page): Promise<void> {
await expect(page.locator('main.cosmic-content .list-page .lph-title').first()).toBeVisible({ timeout: 25_000 });
await expect(page.locator('main.cosmic-content .el-table').first()).toBeVisible({ timeout: 25_000 });
// ç‰é¦–屿•°æ®/骨架稳定
await page.waitForTimeout(300);
}
/**
* æ–言å¶å页已渲染(兼容「列表页ã€ã€Œå•对象/é…置页ã€ã€Œè‡ªå®šä¹‰è§†å›¾ã€ï¼‰ã€‚
* 列表页:.list-page å¯è§ï¼›å¯¹è±¡é¡µï¼š.object-page .form-content å¯è§ï¼›
* 自定义视图(如æ“ä½œé¢æ¿ï¼‰ï¼šä¸»åŒºåŸŸå†…有渲染内容。
*/
export async function assertPageRendered(page: Page): Promise<'list' | 'object' | 'custom'> {
const isList = await page.locator('main.cosmic-content .list-page').first().isVisible().catch(() => false);
if (isList) return 'list';
const isObject = await page.locator('main.cosmic-content .object-page .form-content').first().isVisible().catch(() => false);
if (isObject) return 'object';
const hasCustom = await page
.$eval('main.cosmic-content', (el) => el.children.length > 0 && (el.innerText ?? '').trim().length > 0)
.catch(() => false);
if (hasCustom) return 'custom';
throw new Error('页颿—¢æœªæ¸²æŸ“åˆ—è¡¨ä¹Ÿæœªæ¸²æŸ“å¯¹è±¡è¡¨å•æˆ–自定义视图');
}
/** 读å–åˆ—è¡¨é¦–å±æ•°æ®è¡Œæ•°ï¼ˆ>=0)。对象页返回 0。 */
export async function getRowCount(page: Page): Promise<number> {
return page.locator('main.cosmic-content .el-table__body-wrapper tbody tr').count();
}
/**
* 收集页é¢å…³é”®ç½‘络/控制å°é”™è¯¯ï¼ˆè¿‡æ»¤å·²çŸ¥å¯å¿½ç•¥é¡¹ï¼‰ã€‚
* 返回 { errors: string[], consoleErrors: string[] }
*/
export async function collectErrors(page: Page): Promise<{ errors: string[]; consoleErrors: string[] }> {
const errors: string[] = [];
const consoleErrors: string[] = [];
page.on('response', (res) => {
const s = res.status();
if (s >= 400 && !res.url().includes('/Cube/Avatar/')) {
errors.push(`${s} ${res.url()}`);
}
});
page.on('console', (msg) => {
// è¿‡æ»¤å·²çŸ¥æ— å®³å™ªéŸ³ï¼šå¤´åƒ 404 çš„ "Failed to load resource" é€šç”¨æŠ¥é”™ï¼ˆæ— URL,é 上方 response æ•获);
// 真æ£çš„æŽ¥å£/资æºé”™è¯¯ä¼šä»¥å¸¦ URL çš„ response å½¢å¼è¿›å…¥ errors。
if (msg.type() === 'error' && !msg.text().includes('/Cube/Avatar/') && !msg.text().includes('Failed to load resource')) {
consoleErrors.push(msg.text());
}
});
return { errors, consoleErrors };
}
|