import { test, expect, type Page } from './fixtures';
import { gotoLeafPage, assertPageRendered, collectErrors } from './helpers';
/**
* 星尘å‰ç«¯ã€Œåˆ›å»ºé¡¹ç›®ã€æ£è§„ e2e(TDD é—环)
*
* 业务背景:
* - Stardust.Web.Vue 的 Platform/GalaxyProject 列表页(自定义视图 views/Platform/GalaxyProject/index.vue)
* é¡¶éƒ¨æœ‰ã€Œæ–°å»ºé¡¹ç›®ã€æŒ‰é’®ï¼Œå¼¹å‡ºè¡¨å•å¯¹è¯æ¡†å¹¶æäº¤ `POST /api/Platform/GalaxyProject/Add`。
* - åŽç«¯ GalaxyProjectController 是 MVCEntityController<GalaxyProject>ï¼Œæ ‡å‡† CRUD。
*
* 测试约æŸï¼š
* - 真实对接åŽç«¯ http://localhost:6681(SQLite),ç»ä¸ mock。
* - 新建项目å使用 `E2E_` + 时间戳 唯一å,验è¯åŽåœ¨åˆ—表内真实触å‘åˆ é™¤æ¸…ç†ã€‚
*
* è¿è¡Œï¼š
* cd X/Stardust/Stardust.Web.Vue
* $env:PLAYWRIGHT_BROWSERS_PATH=0
* pnpm exec playwright test e2e/CrudPlatformGalaxyProject.spec.ts --project=chromium --reporter=line
*/
/** 在 GalaxyProject åˆ—è¡¨é¡µè§¦å‘æ–°å»ºå¯¹è¯æ¡†å¹¶å¡«å†™è¡¨å•ã€‚è¿”å›žæ˜¯å¦æˆåŠŸæ‰“å¼€å¯¹è¯æ¡†ã€‚ */
async function openCreateDialog(page: Page, name: string): Promise<boolean> {
// ä¼˜å…ˆç‚¹å‡»è‡ªå®šä¹‰è§†å›¾çš„ã€Œæ–°å»ºé¡¹ç›®ã€æŒ‰é’®ï¼›å…œåº• AutoPage 的「新建ã€
const createBtn = page
.getByRole('button', { name: '新建项目' })
.or(page.getByRole('button', { name: '新建' }))
.first();
if (!(await createBtn.isVisible({ timeout: 3000 }).catch(() => false))) return false;
await createBtn.click();
// ç‰å¾…å¯¹è¯æ¡†å‡ºçŽ°ï¼ˆè‡ªå®šä¹‰å¯¹è¯æ¡† + 表å•);或 AutoPage 对象表å•
const dialogOrForm = page.locator('.el-dialog').first().or(
page.locator('main.cosmic-content .object-page .form-content').first(),
);
await dialogOrForm.waitFor({ state: 'visible', timeout: 8000 }).catch(() => {});
// 填写åç§°ï¼ˆå¯¹è¯æ¡†å†…第一个输入框 / AutoPage 表å•第一个输入框)
const nameInput = page.locator('.el-dialog input').first().or(
page.locator('main.cosmic-content .object-page form input').first(),
);
await nameInput.waitFor({ state: 'visible', timeout: 5000 }).catch(() => {});
const editable = await nameInput.isEditable({ timeout: 2000 }).catch(() => false);
if (!editable) return false;
await nameInput.fill(name);
return true;
}
/** æäº¤å¯¹è¯æ¡†/表å•(自定义「ä¿å˜ã€æŒ‰é’® 或 AutoPage「ä¿å˜ã€ï¼‰ã€‚ */
async function submitForm(page: Page): Promise<void> {
const saveBtn = page
.getByRole('button', { name: /ä¿å˜|æäº¤|确定/ })
.first();
await saveBtn.waitFor({ state: 'visible', timeout: 5000 }).catch(() => {});
await saveBtn.click();
}
/** 在列表内定ä½å«æŒ‡å®šæ–‡æœ¬çš„å®žè·µæŒ‰é’®å¹¶ç‚¹å‡»ï¼ˆç”¨äºŽåˆ é™¤æ¸…ç†ï¼‰ã€‚ */
async function deleteRowByName(page: Page, name: string): Promise<void> {
const row = page
.locator('main.cosmic-content .el-table__body-wrapper tbody tr')
.filter({ hasText: name })
.first();
if (await row.count() === 0) return;
// åˆ—è¡¨è¡Œå†…åˆ é™¤æŒ‰é’®ï¼ˆAutoPage é»˜è®¤åˆ é™¤åœ¨è¡Œæœ«æ“作列)
const del = row.getByRole('button', { name: /åˆ é™¤|Delete/ }).first();
if (await del.isVisible({ timeout: 2000 }).catch(() => false)) {
await del.click();
// 确认弹窗
const confirm = page.getByRole('button', { name: /确定|确认|是/ }).first();
await confirm.waitFor({ state: 'visible', timeout: 3000 }).catch(() => {});
await confirm.click().catch(() => {});
await page.waitForTimeout(800);
}
}
test('Platform/GalaxyProject 新建项目(UI é—环:新建→列表出现→清ç†ï¼‰', async ({ page }) => {
const errs = collectErrors(page);
const projectName = `E2E_${Date.now()}`;
await gotoLeafPage(page, 'Platform/GalaxyProject');
expect(await assertPageRendered(page)).toBe('list');
await expect(page.locator('main.cosmic-content .el-table').first()).toBeVisible();
const rowsBefore = await page
.locator('main.cosmic-content .el-table__body-wrapper tbody tr')
.count();
console.log('ROWS_BEFORE=' + rowsBefore);
// æ‰“å¼€æ–°å»ºå¯¹è¯æ¡†å¹¶å¡«å†™
const opened = await openCreateDialog(page, projectName);
expect(opened, 'åº”æ‰“å¼€æ–°å»ºé¡¹ç›®å¯¹è¯æ¡†/表å•').toBe(true);
await submitForm(page);
await page.waitForTimeout(1200);
// 表å•/å¯¹è¯æ¡†å…³é—åŽå›žåˆ°åˆ—表,ç‰å¾…新行出现(最多 8s)
let found = 0;
for (let i = 0; i < 16; i++) {
found = await page
.locator('main.cosmic-content .el-table__body-wrapper tbody tr')
.filter({ hasText: projectName })
.count();
if (found > 0) break;
await page.waitForTimeout(500);
}
console.log('NEW_PROJECT_FOUND=' + found);
expect(found > 0, '新建项目应出现在列表ä¸').toBeTruthy();
// æ–言新建åŽçš„行数增åŠ
const rowsAfter = await page
.locator('main.cosmic-content .el-table__body-wrapper tbody tr')
.count();
console.log('ROWS_AFTER=' + rowsAfter);
expect(rowsAfter).toBeGreaterThan(rowsBefore);
// 清ç†ï¼šåˆ 除刚æ‰åˆ›å»ºçš„项目,é¿å…污染数æ®
await deleteRowByName(page, projectName);
await page.waitForTimeout(1000);
const remains = await page
.locator('main.cosmic-content .el-table__body-wrapper tbody tr')
.filter({ hasText: projectName })
.count();
console.log('AFTER_CLEANUP=' + remains);
expect(remains === 0, '测试创建的项目应被清ç†').toBeTruthy();
// æ— >=400 接å£é”™è¯¯
const captured = await errs;
if (captured.errors.length) {
console.log('NET_ERR=' + JSON.stringify(captured.errors));
throw new Error('å˜åœ¨æŽ¥å£/èµ„æº >=400 错误: ' + JSON.stringify(captured.errors));
}
await page.screenshot({
path: 'e2e/screenshots/Crud/Platform/GalaxyProject/create.png',
fullPage: true,
});
});
// åŒæ—¶ä¿ç•™åŸºç¡€æ¸²æŸ“用例(与既有 PlatformGalaxyProject.spec.ts äº’è¡¥ï¼šæœ¬ç”¨ä¾‹éªŒè¯æ–°å»ºé—环)
test('Platform/GalaxyProject 列表渲染 + 新建按钮å¯è§', async ({ page }) => {
const errs = collectErrors(page);
await gotoLeafPage(page, 'Platform/GalaxyProject');
expect(await assertPageRendered(page)).toBe('list');
await expect(page.locator('main.cosmic-content .el-table').first()).toBeVisible();
// 应有「新建项目ã€å…¥å£ï¼ˆè‡ªå®šä¹‰è§†å›¾é¡¶éƒ¨æŒ‰é’®ï¼‰
const createBtn = page.getByRole('button', { name: '新建项目' }).first();
expect(await createBtn.isVisible({ timeout: 3000 }).catch(() => false)).toBe(true);
const captured = await errs;
if (captured.errors.length) {
console.log('NET_ERR=' + JSON.stringify(captured.errors));
throw new Error('å˜åœ¨æŽ¥å£/èµ„æº >=400 错误: ' + JSON.stringify(captured.errors));
}
});
|