uni-h5.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * uni-app H5 + Playwright 公共操作(登录页起复用)
  3. */
  4. export function env() {
  5. return {
  6. base: process.env.E2E_BASE || 'http://localhost:5173',
  7. user: process.env.E2E_USER || 'admin',
  8. pass: process.env.E2E_PASS || 'admin123'
  9. }
  10. }
  11. export async function clearSession(page) {
  12. await page.goto(env().base + '/', { waitUntil: 'domcontentloaded' })
  13. await page.evaluate(() => {
  14. try {
  15. localStorage.clear()
  16. sessionStorage.clear()
  17. } catch (_) {}
  18. })
  19. }
  20. export async function openLoginFresh(page) {
  21. await clearSession(page)
  22. await page.goto(env().base + '/', { waitUntil: 'networkidle' })
  23. await page.waitForTimeout(800)
  24. // 若仍被跳到工作台,再清一次
  25. if (page.url().includes('workbench')) {
  26. await clearSession(page)
  27. await page.goto(env().base + '/pages/login/login', { waitUntil: 'networkidle' })
  28. await page.waitForTimeout(800)
  29. }
  30. }
  31. export function userInput(page) {
  32. return page.locator('input.uni-input-input[type="text"]').first()
  33. }
  34. export function passInput(page) {
  35. return page.locator('input.uni-input-input[type="password"]').first()
  36. }
  37. export async function fillLogin(page, username, password) {
  38. await userInput(page).fill(username ?? '')
  39. await passInput(page).fill(password ?? '')
  40. }
  41. export async function clickLoginButton(page) {
  42. await page.locator('uni-button.btn-login, button.btn-login').first().click({ force: true })
  43. }
  44. export async function clickAgreeCheck(page) {
  45. await page.locator('.agree-check').first().click({ force: true })
  46. }
  47. export async function waitToast(page, textRe, timeout = 5000) {
  48. // uni-app H5 toast 常见:.uni-sample-toast / .uni-toast / uni-toast
  49. const toast = page.locator('.uni-sample-toast, .uni-toast, uni-toast, .uni-toast__content').filter({
  50. hasText: textRe
  51. })
  52. await toast.first().waitFor({ state: 'visible', timeout })
  53. return toast.first()
  54. }
  55. export async function isOnLogin(page) {
  56. const url = page.url()
  57. if (url.includes('/pages/login/login') || url.endsWith('/') || url.match(/:5173\/?$/)) {
  58. const btn = await page.getByText('账号密码登录').first().isVisible().catch(() => false)
  59. const loading = await page.getByText('登录中...').first().isVisible().catch(() => false)
  60. return btn || loading
  61. }
  62. return false
  63. }
  64. export async function isOnWorkbench(page) {
  65. return (
  66. page.url().includes('/pages/workbench/workbench') &&
  67. (await page.getByText('工作台', { exact: true }).first().isVisible().catch(() => false))
  68. )
  69. }