enterprise-mini.page.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { Page, Locator, expect } from '@playwright/test';
  3. /**
  4. * 企业小程序 H5 URL
  5. */
  6. const MINI_BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:8080';
  7. const MINI_LOGIN_URL = `${MINI_BASE_URL}/mini`;
  8. /**
  9. * 企业小程序 Page Object
  10. *
  11. * 用于企业小程序 E2E 测试
  12. * H5 页面路径: /mini
  13. *
  14. * 主要功能:
  15. * - 小程序登录(手机号 + 密码)
  16. * - Token 管理
  17. * - 页面导航和验证
  18. *
  19. * @example
  20. * ```typescript
  21. * const miniPage = new EnterpriseMiniPage(page);
  22. * await miniPage.goto();
  23. * await miniPage.login('13800138000', 'password123');
  24. * await miniPage.expectLoginSuccess();
  25. * ```
  26. */
  27. export class EnterpriseMiniPage {
  28. readonly page: Page;
  29. // ===== 页面级选择器 =====
  30. /** 登录页面容器 */
  31. readonly loginPage: Locator;
  32. /** 页面标题 */
  33. readonly pageTitle: Locator;
  34. // ===== 登录表单选择器 =====
  35. /** 手机号输入框 */
  36. readonly phoneInput: Locator;
  37. /** 密码输入框 */
  38. readonly passwordInput: Locator;
  39. /** 登录按钮 */
  40. readonly loginButton: Locator;
  41. // ===== 主页选择器(登录后) =====
  42. /** 用户信息显示区域 */
  43. readonly userInfo: Locator;
  44. constructor(page: Page) {
  45. this.page = page;
  46. // 初始化登录页面选择器
  47. // Taro 组件在 H5 渲染时会传递 data-testid 到 DOM (使用 taro-view-core 等组件)
  48. this.loginPage = page.getByTestId('mini-login-page');
  49. this.pageTitle = page.getByTestId('mini-page-title');
  50. // 登录表单选择器 - 使用 data-testid
  51. this.phoneInput = page.getByTestId('mini-phone-input');
  52. this.passwordInput = page.getByTestId('mini-password-input');
  53. this.loginButton = page.getByTestId('mini-login-button');
  54. // 主页选择器(登录后可用)
  55. this.userInfo = page.getByTestId('mini-user-info');
  56. }
  57. // ===== 导航和基础验证 =====
  58. /**
  59. * 导航到企业小程序 H5 登录页面
  60. */
  61. async goto(): Promise<void> {
  62. await this.page.goto(MINI_LOGIN_URL);
  63. // 使用 auto-waiting 机制,等待页面容器可见
  64. await this.expectToBeVisible();
  65. }
  66. /**
  67. * 验证登录页面关键元素可见
  68. */
  69. async expectToBeVisible(): Promise<void> {
  70. // 等待页面容器可见
  71. await this.loginPage.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  72. // 验证页面标题
  73. await this.pageTitle.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  74. }
  75. // ===== 登录功能方法 =====
  76. /**
  77. * 填写手机号
  78. * @param phone 手机号(11位数字)
  79. */
  80. async fillPhone(phone: string): Promise<void> {
  81. // Taro 的 Input 组件使用自定义元素,使用 type() 代替 fill()
  82. await this.phoneInput.click();
  83. // 先全选已有内容(如果有),然后输入新内容
  84. await this.page.keyboard.press('Control+A');
  85. await this.phoneInput.type(phone, { delay: 10 });
  86. }
  87. /**
  88. * 填写密码
  89. * @param password 密码(6-20位)
  90. */
  91. async fillPassword(password: string): Promise<void> {
  92. // Taro 的 Input 组件使用自定义元素,使用 type() 代替 fill()
  93. await this.passwordInput.click();
  94. // 先全选已有内容(如果有),然后输入新内容
  95. await this.page.keyboard.press('Control+A');
  96. await this.passwordInput.type(password, { delay: 10 });
  97. }
  98. /**
  99. * 点击登录按钮
  100. */
  101. async clickLoginButton(): Promise<void> {
  102. await this.loginButton.click();
  103. }
  104. /**
  105. * 执行登录操作(完整流程)
  106. * @param phone 手机号
  107. * @param password 密码
  108. */
  109. async login(phone: string, password: string): Promise<void> {
  110. await this.fillPhone(phone);
  111. await this.fillPassword(password);
  112. await this.clickLoginButton();
  113. }
  114. /**
  115. * 验证登录成功
  116. *
  117. * 登录成功后应该跳转到主页或显示用户信息
  118. */
  119. async expectLoginSuccess(): Promise<void> {
  120. // 使用 auto-waiting 机制,等待 URL 变化或用户信息显示
  121. // 小程序登录成功后会跳转到 dashboard 页面
  122. // 等待 URL 变化,使用 Promise.race 实现超时
  123. await this.page.waitForURL(
  124. url => url.pathname.includes('/dashboard') || url.pathname.includes('/pages/yongren/dashboard'),
  125. { timeout: TIMEOUTS.PAGE_LOAD }
  126. ).catch(() => {
  127. // 如果没有跳转,检查是否显示用户信息
  128. // 注意:此验证将在 Story 12.5 E2E 测试中完全实现
  129. // 当前仅提供基础结构
  130. });
  131. }
  132. /**
  133. * 验证登录失败(错误提示显示)
  134. * @param expectedErrorMessage 预期的错误消息(可选)
  135. */
  136. async expectLoginError(expectedErrorMessage?: string): Promise<void> {
  137. // 等待一下,让后端响应或前端验证生效
  138. await this.page.waitForTimeout(1000);
  139. // 验证仍然在登录页面(未跳转)
  140. const currentUrl = this.page.url();
  141. expect(currentUrl).toContain('/mini');
  142. // 验证登录页面容器仍然可见
  143. await expect(this.loginPage).toBeVisible();
  144. // 如果提供了预期的错误消息,尝试验证
  145. if (expectedErrorMessage) {
  146. // 尝试查找错误消息(可能在 Toast、Modal 或表单验证中)
  147. const errorElement = this.page.getByText(expectedErrorMessage, { exact: false }).first();
  148. await errorElement.isVisible().catch(() => false);
  149. // 不强制要求错误消息可见,因为后端可能不会返回错误
  150. }
  151. }
  152. // ===== Token 管理方法 =====
  153. /**
  154. * 获取当前存储的 token
  155. * @returns token 字符串,如果不存在则返回 null
  156. */
  157. async getToken(): Promise<string | null> {
  158. const token = await this.page.evaluate(() => {
  159. return (
  160. localStorage.getItem('token') ||
  161. localStorage.getItem('auth_token') ||
  162. sessionStorage.getItem('token') ||
  163. sessionStorage.getItem('auth_token') ||
  164. null
  165. );
  166. });
  167. return token;
  168. }
  169. /**
  170. * 设置 token(用于测试前置条件)
  171. * @param token token 字符串
  172. */
  173. async setToken(token: string): Promise<void> {
  174. await this.page.evaluate((t) => {
  175. localStorage.setItem('token', t);
  176. localStorage.setItem('auth_token', t);
  177. }, token);
  178. }
  179. /**
  180. * 清除所有认证相关的存储
  181. */
  182. async clearAuth(): Promise<void> {
  183. await this.page.evaluate(() => {
  184. localStorage.removeItem('token');
  185. localStorage.removeItem('auth_token');
  186. sessionStorage.removeItem('token');
  187. sessionStorage.removeItem('auth_token');
  188. });
  189. }
  190. // ===== 主页元素验证方法 =====
  191. /**
  192. * 验证主页元素可见(登录后)
  193. * 根据实际小程序主页结构调整
  194. */
  195. async expectHomePageVisible(): Promise<void> {
  196. // 使用 auto-waiting 机制,等待主页元素可见
  197. // 注意:此方法将在 Story 12.5 E2E 测试中使用,当前仅提供基础结构
  198. // 根据实际小程序主页的 data-testid 调整
  199. const dashboard = this.page.getByTestId('mini-dashboard');
  200. await dashboard.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  201. }
  202. /**
  203. * 获取用户信息显示的文本
  204. * @returns 用户信息文本
  205. */
  206. async getUserInfoText(): Promise<string | null> {
  207. const userInfo = this.userInfo;
  208. const count = await userInfo.count();
  209. if (count === 0) {
  210. return null;
  211. }
  212. return await userInfo.textContent();
  213. }
  214. }