workbench.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { expect, test } from '@playwright/test'
  2. test('home, local previews and profile work without business API requests', async ({ page }) => {
  3. const failures: string[] = []
  4. const apiRequests: string[] = []
  5. page.on('pageerror', (error) => failures.push(error.message))
  6. page.on('request', (request) => {
  7. if (['fetch', 'xhr'].includes(request.resourceType()) && !request.url().includes('/@vite/')) apiRequests.push(request.url())
  8. })
  9. await page.goto('/')
  10. await expect(page.locator('.home-header').getByText('教师工作台', { exact: true })).toBeVisible()
  11. await expect(page.locator('.home-header').getByText(/开发环境|测试环境|正式环境/)).toHaveCount(0)
  12. await expect(page.locator('.notification-button .bell-icon')).toBeVisible()
  13. await expect(page.locator('.notification-button .notification-dot')).toBeVisible()
  14. await page.locator('.notification-button').click()
  15. await expect(page.getByText('有 3 条题目讲解待查看')).toBeVisible()
  16. await page.locator('.sheet-close').click()
  17. for (const title of ['上传错题', '上传试卷', '上传答题卡', '题目讲解']) {
  18. await expect(page.getByText(title, { exact: true })).toBeVisible()
  19. }
  20. const cards = await page.locator('.workbench-card').all()
  21. const firstCard = await cards[0].boundingBox()
  22. const secondCard = await cards[1].boundingBox()
  23. expect(firstCard).not.toBeNull()
  24. expect(secondCard).not.toBeNull()
  25. if (firstCard && secondCard) expect(secondCard.y - firstCard.y - firstCard.height).toBeLessThan(16)
  26. await page.locator('.workbench-card').filter({ hasText: '上传错题' }).click()
  27. await expect(page.getByText('小明(演示) · 七年级')).toBeVisible()
  28. await page.getByText('选择演示图片').click()
  29. await expect(page.getByText('演示图片已选择')).toBeVisible()
  30. await page.getByText('完成演示', { exact: true }).click()
  31. await page.locator('.workbench-card').filter({ hasText: '题目讲解' }).click()
  32. await expect(page.getByText('一元一次方程的解法')).toBeVisible()
  33. await page.locator('.sheet-close').click()
  34. await page.getByRole('link', { name: '我的', exact: true }).click()
  35. await expect(page.getByText('张老师', { exact: true })).toBeVisible()
  36. await expect(page.locator('.profile-header').getByText(/开发环境|测试环境|正式环境/)).toHaveCount(0)
  37. await page.locator('.settings-row').filter({ hasText: '账号信息' }).click()
  38. await expect(page.getByText('10000001', { exact: true })).toBeVisible()
  39. await page.locator('.sheet-close').click()
  40. await page.locator('.settings-row').filter({ hasText: '修改密码' }).click()
  41. await page.locator('input[placeholder="输入至少 8 位演示密码"]').fill('demo12345')
  42. await page.locator('input[placeholder="再次输入演示密码"]').fill('demo12345')
  43. await page.getByText('演示确认', { exact: true }).click()
  44. await expect(page.getByText('校验通过,展示模式不会修改密码')).toBeHidden()
  45. await page.locator('.settings-row').filter({ hasText: '关于知了数学' }).click()
  46. await expect(page.getByText('版本 0.1.0')).toBeVisible()
  47. await page.locator('.sheet-close').click()
  48. await page.screenshot({ path: `test-results/profile-${test.info().project.name}.png`, fullPage: true, scale: 'css' })
  49. await page.getByText('退出登录', { exact: true }).click()
  50. await page.getByText('退出', { exact: true }).click()
  51. await expect(page.getByText('游客', { exact: true })).toBeVisible()
  52. await page.getByText('进入演示账号', { exact: true }).click()
  53. await expect(page.getByText('张老师', { exact: true })).toBeVisible()
  54. await page.getByRole('link', { name: '首页', exact: true }).click()
  55. await page.screenshot({ path: `test-results/home-${test.info().project.name}.png`, fullPage: true, scale: 'css' })
  56. expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBeTruthy()
  57. expect(failures).toEqual([])
  58. expect(apiRequests).toEqual([])
  59. })