test: align loginAsStudent with teacher-switching pattern

Match the exact login flow used by teacher-switching.spec.ts:
replace JWT wait with settle timeout, increase wait durations, always
navigate to /student/profile via page.goto after org selection.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-01 17:56:09 +08:00
parent 29c1c9fee7
commit c132825710
+28 -17
View File
@@ -231,38 +231,49 @@ test.describe('课包商店(已登录学生查看老师主页)', () => {
async function loginAsStudent(page: any, moicenUnionid: string) { async function loginAsStudent(page: any, moicenUnionid: string) {
const q = new URLSearchParams({ unionid: moicenUnionid, status: '2' }); const q = new URLSearchParams({ unionid: moicenUnionid, status: '2' });
await page.goto(`/?${q.toString()}`, { waitUntil: 'domcontentloaded', timeout: 60_000 }); await page.goto(`/?${q.toString()}`, {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 }); await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
// Wait for SPA to settle and login/role resolution to complete
await page.waitForTimeout(5000);
// Wait for JWT to appear (login completed) // Pick STUDENT role explicitly
await page.waitForFunction(
() => !!window.localStorage.getItem('Authorization'),
{ timeout: 90_000 },
);
// Select student role if prompted
const roleSelect = page.getByText('请选择您的登录身份'); const roleSelect = page.getByText('请选择您的登录身份');
if (await roleSelect.isVisible({ timeout: 5000 }).catch(() => false)) { if (await roleSelect.isVisible().catch(() => false)) {
const studentRole = page.locator('.van-grid-item').filter({ hasText: '学生' }); const studentRole = page.locator('.van-grid-item').filter({ hasText: '学生' });
if (await studentRole.isVisible().catch(() => false)) { if (await studentRole.isVisible().catch(() => false)) {
await studentRole.click(); await studentRole.click();
await page.waitForTimeout(2000); await page.waitForTimeout(5000);
} }
} }
// Handle org select if needed // Handle org select if needed
if (page.url().includes('/org/select')) { if (page.url().includes('/org/select')) {
if (await page.locator('.course-package-store').isVisible().catch(() => false)) { const guest = page.getByText('请返回微信小程序完成登录');
return false; // session not usable if (await guest.isVisible().catch(() => false)) {
return false; // 会话不可用
} }
const firstOrgCell = page.locator('#app .van-cell-group .van-cell').first(); const orgCells = page.locator('#app .van-cell-group .van-cell');
if (await firstOrgCell.isVisible({ timeout: 10_000 }).catch(() => false)) { const n = await orgCells.count();
await firstOrgCell.click(); if (n === 0) return false;
await page.waitForTimeout(3000); await orgCells.first().click();
await page.waitForTimeout(5000);
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
if (await guest.isVisible().catch(() => false)) {
return false; // org switch 后会话不可用
} }
await page.goto('/student/profile', {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
} }
// Ensure we're on a settled student page // Ensure we're on a student page
const currentPath = new URL(page.url()).pathname; const currentPath = new URL(page.url()).pathname;
if (!currentPath.startsWith('/student/')) { if (!currentPath.startsWith('/student/')) {
await page.goto('/student/profile', { await page.goto('/student/profile', {