diff --git a/tests/course-package-store.spec.ts b/tests/course-package-store.spec.ts index f12810d..b44a598 100644 --- a/tests/course-package-store.spec.ts +++ b/tests/course-package-store.spec.ts @@ -191,39 +191,14 @@ test.describe('课包商店(已登录学生查看老师主页)', () => { return; } - // Login as student (status=2) - const q = new URLSearchParams({ unionid: moicenUnionid, status: '2' }); - await page.goto(`/?${q.toString()}`, { waitUntil: 'domcontentloaded', timeout: 60_000 }); - await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 }); - await page.waitForTimeout(5000); - - // Select student role if prompted - const roleSelect = page.getByText('请选择您的登录身份'); - 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(5000); - } - } - - // Handle org select if needed - if (page.url().includes('/org/select')) { - if (await page.locator('.course-package-store').isVisible().catch(() => false)) { - test.skip(true, '会话不可用'); - return; - } - const orgCell = page.locator('#app .van-cell-group .van-cell').first(); - if (await orgCell.isVisible().catch(() => false)) { - await orgCell.click(); - await page.waitForTimeout(5000); - } - } + // Login as student + const ok = await loginAsStudent(page, moicenUnionid); + if (!ok) { test.skip(true, '会话不可用'); return; } // Navigate to teacher home await page.goto('/teacher/home', { waitUntil: 'domcontentloaded', timeout: 60_000 }); await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 }); - await page.waitForTimeout(5000); + await page.waitForTimeout(3000); // Should NOT see the login prompt await expect(page.getByText('请返回微信小程序完成登录')).not.toBeVisible(); @@ -258,15 +233,20 @@ 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 expect(page.locator('#app')).toBeVisible({ timeout: 60_000 }); - 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 const roleSelect = page.getByText('请选择您的登录身份'); - if (await roleSelect.isVisible().catch(() => false)) { + if (await roleSelect.isVisible({ timeout: 5000 }).catch(() => false)) { const studentRole = page.locator('.van-grid-item').filter({ hasText: '学生' }); if (await studentRole.isVisible().catch(() => false)) { await studentRole.click(); - await page.waitForTimeout(5000); + await page.waitForTimeout(2000); } } @@ -276,9 +256,9 @@ async function loginAsStudent(page: any, moicenUnionid: string) { return false; // session not usable } const orgCell = page.locator('#app .van-cell-group .van-cell').first(); - if (await orgCell.isVisible().catch(() => false)) { + if (await orgCell.isVisible({ timeout: 5000 }).catch(() => false)) { await orgCell.click(); - await page.waitForTimeout(5000); + await page.waitForTimeout(2000); } } return true; diff --git a/tests/course-package.spec.ts b/tests/course-package.spec.ts index 2c66901..2724e8f 100644 --- a/tests/course-package.spec.ts +++ b/tests/course-package.spec.ts @@ -48,9 +48,11 @@ async function loginAndGetJwt(page: any): Promise { ) { await page.locator('.van-grid-item').first().click(); } - await expect( - page.getByText(/请选择您的登录身份|欢迎回来|进入工作台/), - ).toBeVisible({ timeout: 120_000 }); + // Wait for JWT to appear in localStorage (login completed) + await page.waitForFunction( + () => !!window.localStorage.getItem('Authorization'), + { timeout: 90_000 }, + ); if ( await page.getByText('请返回微信小程序完成登录').isVisible().catch(() => false)