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', {