enterprise-mini.page.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { Page, Locator } 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. // 初始化登录页面选择器(使用 data-testid)
  47. this.loginPage = page.getByTestId('mini-login-page');
  48. this.pageTitle = page.getByText('企业用户登录');
  49. // 登录表单选择器
  50. this.phoneInput = page.getByTestId('mini-phone-input');
  51. this.passwordInput = page.getByTestId('mini-password-input');
  52. this.loginButton = page.getByTestId('mini-login-button');
  53. // 主页选择器(登录后可用)
  54. // 用户信息显示 - 根据实际小程序主页结构调整
  55. this.userInfo = page.locator('[data-testid="mini-user-info"]');
  56. }
  57. // ===== 导航和基础验证 =====
  58. /**
  59. * 导航到企业小程序 H5 登录页面
  60. */
  61. async goto(): Promise<void> {
  62. await this.page.goto(MINI_LOGIN_URL);
  63. await this.page.waitForLoadState('domcontentloaded');
  64. // 等待页面加载完成
  65. await this.page.waitForTimeout(TIMEOUTS.MEDIUM);
  66. await this.expectToBeVisible();
  67. }
  68. /**
  69. * 验证登录页面关键元素可见
  70. */
  71. async expectToBeVisible(): Promise<void> {
  72. // 等待页面容器可见
  73. await this.loginPage.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  74. // 验证页面标题
  75. await this.pageTitle.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  76. }
  77. // ===== 登录功能方法 =====
  78. /**
  79. * 填写手机号
  80. * @param phone 手机号(11位数字)
  81. */
  82. async fillPhone(phone: string): Promise<void> {
  83. await this.phoneInput.fill(phone);
  84. }
  85. /**
  86. * 填写密码
  87. * @param password 密码(6-20位)
  88. */
  89. async fillPassword(password: string): Promise<void> {
  90. await this.passwordInput.fill(password);
  91. }
  92. /**
  93. * 点击登录按钮
  94. */
  95. async clickLoginButton(): Promise<void> {
  96. await this.loginButton.click();
  97. }
  98. /**
  99. * 执行登录操作(完整流程)
  100. * @param phone 手机号
  101. * @param password 密码
  102. */
  103. async login(phone: string, password: string): Promise<void> {
  104. await this.fillPhone(phone);
  105. await this.fillPassword(password);
  106. await this.clickLoginButton();
  107. }
  108. /**
  109. * 验证登录成功
  110. *
  111. * 登录成功后应该跳转到主页或显示用户信息
  112. */
  113. async expectLoginSuccess(): Promise<void> {
  114. // 等待页面跳转或用户信息显示
  115. // 小程序登录成功后会跳转到 dashboard 页面
  116. await this.page.waitForTimeout(TIMEOUTS.LONGER);
  117. // 验证登录成功 - 可以检查 URL 变化或用户信息显示
  118. // 根据实际登录成功后的页面调整
  119. const currentUrl = this.page.url();
  120. if (!currentUrl.includes('/dashboard') && !currentUrl.includes('/pages/yongren/dashboard')) {
  121. // 如果没有跳转,检查是否显示用户信息
  122. const hasUserInfo = await this.userInfo.count() > 0;
  123. if (!hasUserInfo) {
  124. throw new Error('期望登录成功,但未检测到页面跳转或用户信息显示');
  125. }
  126. }
  127. }
  128. /**
  129. * 验证登录失败(错误提示显示)
  130. * @param expectedErrorMessage 预期的错误消息(可选)
  131. */
  132. async expectLoginError(expectedErrorMessage?: string): Promise<void> {
  133. // 等待错误提示显示
  134. await this.page.waitForTimeout(TIMEOUTS.MEDIUM);
  135. // Taro 的 showToast 会显示错误消息
  136. // 可以通过文本内容或特定选择器来验证
  137. if (expectedErrorMessage) {
  138. const errorElement = this.page.getByText(expectedErrorMessage);
  139. await errorElement.waitFor({ state: 'visible', timeout: TIMEOUTS.TOAST_LONG });
  140. }
  141. }
  142. // ===== Token 管理方法 =====
  143. /**
  144. * 获取当前存储的 token
  145. * @returns token 字符串,如果不存在则返回 null
  146. */
  147. async getToken(): Promise<string | null> {
  148. const token = await this.page.evaluate(() => {
  149. return (
  150. localStorage.getItem('token') ||
  151. localStorage.getItem('auth_token') ||
  152. sessionStorage.getItem('token') ||
  153. sessionStorage.getItem('auth_token') ||
  154. null
  155. );
  156. });
  157. return token;
  158. }
  159. /**
  160. * 设置 token(用于测试前置条件)
  161. * @param token token 字符串
  162. */
  163. async setToken(token: string): Promise<void> {
  164. await this.page.evaluate((t) => {
  165. localStorage.setItem('token', t);
  166. localStorage.setItem('auth_token', t);
  167. }, token);
  168. }
  169. /**
  170. * 清除所有认证相关的存储
  171. */
  172. async clearAuth(): Promise<void> {
  173. await this.page.evaluate(() => {
  174. localStorage.removeItem('token');
  175. localStorage.removeItem('auth_token');
  176. sessionStorage.removeItem('token');
  177. sessionStorage.removeItem('auth_token');
  178. });
  179. }
  180. // ===== 主页元素验证方法 =====
  181. /**
  182. * 验证主页元素可见(登录后)
  183. * 根据实际小程序主页结构调整
  184. */
  185. async expectHomePageVisible(): Promise<void> {
  186. // 等待主页加载
  187. await this.page.waitForTimeout(TIMEOUTS.LONG);
  188. // 验证主页关键元素存在
  189. // 根据实际小程序主页的 data-testid 调整
  190. const dashboard = this.page.locator('[data-testid="mini-dashboard"]');
  191. await dashboard.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  192. }
  193. /**
  194. * 获取用户信息显示的文本
  195. * @returns 用户信息文本
  196. */
  197. async getUserInfoText(): Promise<string | null> {
  198. const userInfo = this.userInfo;
  199. const count = await userInfo.count();
  200. if (count === 0) {
  201. return null;
  202. }
  203. return await userInfo.textContent();
  204. }
  205. }