test: replace flaky login assertions with localStorage JWT check

loginAndGetJwt / loginAsStudent no longer wait 120s for text patterns
that may not appear. Instead they wait for Authorization in localStorage,
which is faster and more reliable.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-01 16:15:54 +08:00
parent 90f3bbf442
commit 2f73bea4b6
2 changed files with 19 additions and 37 deletions
+14 -34
View File
@@ -191,39 +191,14 @@ test.describe('课包商店(已登录学生查看老师主页)', () => {
return; return;
} }
// Login as student (status=2) // Login as student
const q = new URLSearchParams({ unionid: moicenUnionid, status: '2' }); const ok = await loginAsStudent(page, moicenUnionid);
await page.goto(`/?${q.toString()}`, { waitUntil: 'domcontentloaded', timeout: 60_000 }); if (!ok) { test.skip(true, '会话不可用'); return; }
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);
}
}
// Navigate to teacher home // Navigate to teacher home
await page.goto('/teacher/home', { waitUntil: 'domcontentloaded', timeout: 60_000 }); await page.goto('/teacher/home', { waitUntil: 'domcontentloaded', timeout: 60_000 });
await expect(page.locator('#app')).toBeVisible({ 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 // Should NOT see the login prompt
await expect(page.getByText('请返回微信小程序完成登录')).not.toBeVisible(); 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' }); 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 });
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 // Select student role if prompted
const roleSelect = page.getByText('请选择您的登录身份'); 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: '学生' }); 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(5000); await page.waitForTimeout(2000);
} }
} }
@@ -276,9 +256,9 @@ async function loginAsStudent(page: any, moicenUnionid: string) {
return false; // session not usable return false; // session not usable
} }
const orgCell = page.locator('#app .van-cell-group .van-cell').first(); 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 orgCell.click();
await page.waitForTimeout(5000); await page.waitForTimeout(2000);
} }
} }
return true; return true;
+5 -3
View File
@@ -48,9 +48,11 @@ async function loginAndGetJwt(page: any): Promise<string> {
) { ) {
await page.locator('.van-grid-item').first().click(); await page.locator('.van-grid-item').first().click();
} }
await expect( // Wait for JWT to appear in localStorage (login completed)
page.getByText(/请选择您的登录身份|欢迎回来|进入工作台/), await page.waitForFunction(
).toBeVisible({ timeout: 120_000 }); () => !!window.localStorage.getItem('Authorization'),
{ timeout: 90_000 },
);
if ( if (
await page.getByText('请返回微信小程序完成登录').isVisible().catch(() => false) await page.getByText('请返回微信小程序完成登录').isVisible().catch(() => false)