From c1328257107414a76affe11e23f949ef48330d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Fri, 1 May 2026 17:56:09 +0800 Subject: [PATCH] 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 --- tests/course-package-store.spec.ts | 45 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/tests/course-package-store.spec.ts b/tests/course-package-store.spec.ts index cc7b4c5..a7c2f6e 100644 --- a/tests/course-package-store.spec.ts +++ b/tests/course-package-store.spec.ts @@ -231,38 +231,49 @@ test.describe('课包商店(已登录学生查看老师主页)', () => { async function loginAsStudent(page: any, moicenUnionid: string) { 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 }); + // Wait for SPA to settle and login/role resolution to complete + await page.waitForTimeout(5000); - // Wait for JWT to appear (login completed) - await page.waitForFunction( - () => !!window.localStorage.getItem('Authorization'), - { timeout: 90_000 }, - ); - - // Select student role if prompted + // Pick STUDENT role explicitly 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: '学生' }); if (await studentRole.isVisible().catch(() => false)) { await studentRole.click(); - await page.waitForTimeout(2000); + await page.waitForTimeout(5000); } } // Handle org select if needed if (page.url().includes('/org/select')) { - if (await page.locator('.course-package-store').isVisible().catch(() => false)) { - return false; // session not usable + const guest = page.getByText('请返回微信小程序完成登录'); + if (await guest.isVisible().catch(() => false)) { + return false; // 会话不可用 } - const firstOrgCell = page.locator('#app .van-cell-group .van-cell').first(); - if (await firstOrgCell.isVisible({ timeout: 10_000 }).catch(() => false)) { - await firstOrgCell.click(); - await page.waitForTimeout(3000); + const orgCells = page.locator('#app .van-cell-group .van-cell'); + const n = await orgCells.count(); + if (n === 0) return false; + 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; if (!currentPath.startsWith('/student/')) { await page.goto('/student/profile', {