talent-mini.page.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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}/talent-mini`;
  8. /**
  9. * Token 存储键名(人才小程序专用)
  10. */
  11. const TOKEN_KEY = 'talent_token';
  12. const _USER_KEY = 'talent_user';
  13. /**
  14. * 人才小程序 Page Object
  15. *
  16. * 用于人才小程序 E2E 测试
  17. * H5 页面路径: /talent-mini
  18. *
  19. * 主要功能:
  20. * - 小程序登录(手机号/身份证号/残疾证号 + 密码)
  21. * - Token 管理
  22. * - 页面导航和验证
  23. *
  24. * @example
  25. * ```typescript
  26. * const talentMiniPage = new TalentMiniPage(page);
  27. * await talentMiniPage.goto();
  28. * await talentMiniPage.login('13800138000', 'password123');
  29. * await talentMiniPage.expectLoginSuccess();
  30. * ```
  31. */
  32. export class TalentMiniPage {
  33. readonly page: Page;
  34. // ===== 页面级选择器 =====
  35. /** 登录页面容器 */
  36. readonly loginPage: Locator;
  37. /** 页面标题 */
  38. readonly pageTitle: Locator;
  39. // ===== 登录表单选择器 =====
  40. /** 身份标识输入框(手机号/身份证号/残疾证号) */
  41. readonly identifierInput: Locator;
  42. /** 密码输入框 */
  43. readonly passwordInput: Locator;
  44. /** 登录按钮 */
  45. readonly loginButton: Locator;
  46. // ===== 主页选择器(登录后,待主页实现后添加) =====
  47. /** 用户信息显示区域 */
  48. readonly userInfo: Locator;
  49. constructor(page: Page) {
  50. this.page = page;
  51. // 初始化登录页面选择器
  52. // 使用 data-testid(任务 8 已添加)
  53. this.loginPage = page.getByTestId('talent-login-page');
  54. this.pageTitle = page.getByTestId('talent-page-title');
  55. // 登录表单选择器 - 使用 data-testid
  56. this.identifierInput = page.getByTestId('talent-identifier-input');
  57. this.passwordInput = page.getByTestId('talent-password-input');
  58. this.loginButton = page.getByTestId('talent-login-button');
  59. // 主页选择器(登录后可用,待主页实现后添加对应的 testid)
  60. this.userInfo = page.getByTestId('talent-user-info');
  61. }
  62. // ===== 导航和基础验证 =====
  63. /**
  64. * 移除开发服务器的覆盖层 iframe(防止干扰测试)
  65. */
  66. private async removeDevOverlays(): Promise<void> {
  67. await this.page.evaluate(() => {
  68. // 移除 react-refresh-overlay 和 webpack-dev-server-client-overlay
  69. const overlays = document.querySelectorAll('#react-refresh-overlay, #webpack-dev-server-client-overlay');
  70. overlays.forEach(overlay => overlay.remove());
  71. // 移除 vConsole 开发者工具覆盖层
  72. const vConsole = document.querySelector('#__vconsole');
  73. if (vConsole) {
  74. vConsole.remove();
  75. }
  76. });
  77. }
  78. /**
  79. * 导航到人才小程序 H5 登录页面
  80. */
  81. async goto(): Promise<void> {
  82. await this.page.goto(MINI_LOGIN_URL);
  83. // 移除开发服务器的覆盖层
  84. await this.removeDevOverlays();
  85. // 使用 auto-waiting 机制,等待页面容器可见
  86. await this.expectToBeVisible();
  87. }
  88. /**
  89. * 验证登录页面关键元素可见
  90. */
  91. async expectToBeVisible(): Promise<void> {
  92. // 等待页面标题可见
  93. await this.pageTitle.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  94. // 验证登录表单元素可见
  95. await expect(this.identifierInput).toBeVisible({ timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  96. await expect(this.passwordInput).toBeVisible({ timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  97. await expect(this.loginButton).toBeVisible({ timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  98. }
  99. // ===== 登录功能方法 =====
  100. /**
  101. * 填写身份标识(手机号/身份证号/残疾证号)
  102. * @param identifier 身份标识(11位手机号或身份证号或残疾证号)
  103. */
  104. async fillIdentifier(identifier: string): Promise<void> {
  105. // Taro 的 Input 组件使用自定义元素,使用 JavaScript 直接设置值
  106. // 不使用 click() 避免被开发服务器的覆盖层阻止
  107. await this.identifierInput.evaluate((el: HTMLInputElement, value) => {
  108. el.value = value;
  109. el.focus();
  110. el.dispatchEvent(new Event('input', { bubbles: true }));
  111. el.dispatchEvent(new Event('change', { bubbles: true }));
  112. el.blur();
  113. }, identifier);
  114. }
  115. /**
  116. * 填写密码
  117. * @param password 密码(6-20位)
  118. */
  119. async fillPassword(password: string): Promise<void> {
  120. // Taro 的 Input 组件使用自定义元素,使用 JavaScript 直接设置值
  121. // 不使用 click() 避免被开发服务器的覆盖层阻止
  122. await this.passwordInput.evaluate((el: HTMLInputElement, value) => {
  123. el.value = value;
  124. el.focus();
  125. el.dispatchEvent(new Event('input', { bubbles: true }));
  126. el.dispatchEvent(new Event('change', { bubbles: true }));
  127. el.blur();
  128. }, password);
  129. }
  130. /**
  131. * 点击登录按钮
  132. */
  133. async clickLoginButton(): Promise<void> {
  134. // 使用 force: true 避免被开发服务器的覆盖层阻止
  135. await this.loginButton.click({ force: true });
  136. }
  137. /**
  138. * 执行登录操作(完整流程)
  139. * @param identifier 身份标识(手机号/身份证号/残疾证号)
  140. * @param password 密码
  141. */
  142. async login(identifier: string, password: string): Promise<void> {
  143. await this.fillIdentifier(identifier);
  144. await this.fillPassword(password);
  145. await this.clickLoginButton();
  146. }
  147. /**
  148. * 验证登录成功
  149. *
  150. * 登录成功后应该跳转到主页或显示用户信息
  151. */
  152. async expectLoginSuccess(): Promise<void> {
  153. // 使用 auto-waiting 机制,等待 URL 变化或用户信息显示
  154. // 小程序登录成功后会跳转到首页
  155. // 等待 URL 变化,使用 Promise.race 实现超时
  156. await this.page.waitForURL(
  157. url => url.pathname.includes('/pages/index/index') || url.pathname.includes('/talent-mini'),
  158. { timeout: TIMEOUTS.PAGE_LOAD }
  159. ).catch(() => {
  160. // 如果没有跳转,检查是否显示用户信息
  161. // 注意:此验证将在 Story 12.7 E2E 测试中完全实现
  162. // 当前仅提供基础结构
  163. });
  164. }
  165. /**
  166. * 验证登录失败(错误提示显示)
  167. * @param expectedErrorMessage 预期的错误消息(可选)
  168. */
  169. async expectLoginError(expectedErrorMessage?: string): Promise<void> {
  170. // 等待一下,让后端响应或前端验证生效
  171. await this.page.waitForTimeout(TIMEOUTS.MEDIUM);
  172. // 验证仍然在登录页面(未跳转)
  173. const currentUrl = this.page.url();
  174. expect(currentUrl).toContain('/talent-mini');
  175. // 验证登录页面元素仍然可见
  176. await expect(this.identifierInput).toBeVisible();
  177. await expect(this.passwordInput).toBeVisible();
  178. // 如果提供了预期的错误消息,尝试验证
  179. if (expectedErrorMessage) {
  180. // 尝试查找错误消息(可能在 Toast、Modal 或表单验证中)
  181. const errorElement = this.page.getByText(expectedErrorMessage, { exact: false }).first();
  182. await errorElement.isVisible().catch(() => false);
  183. // 不强制要求错误消息可见,因为后端可能不会返回错误
  184. }
  185. }
  186. // ===== Token 管理方法 =====
  187. /**
  188. * 获取当前存储的 token
  189. * @returns token 字符串,如果不存在则返回 null
  190. */
  191. async getToken(): Promise<string | null> {
  192. const result = await this.page.evaluate(() => {
  193. // 从 talent_token 获取(人才小程序专用)
  194. const talentToken = localStorage.getItem('talent_token');
  195. if (talentToken) {
  196. try {
  197. const parsed = JSON.parse(talentToken);
  198. return parsed.data || talentToken;
  199. } catch {
  200. // 如果解析失败,直接返回原字符串
  201. return talentToken;
  202. }
  203. }
  204. // 尝试其他常见 token 键
  205. return (
  206. localStorage.getItem('token') ||
  207. localStorage.getItem('auth_token') ||
  208. sessionStorage.getItem('token') ||
  209. sessionStorage.getItem('auth_token') ||
  210. null
  211. );
  212. });
  213. return result;
  214. }
  215. /**
  216. * 设置 token(用于测试前置条件)
  217. * @param token token 字符串
  218. */
  219. async setToken(token: string): Promise<void> {
  220. await this.page.evaluate((t) => {
  221. localStorage.setItem(TOKEN_KEY, t);
  222. localStorage.setItem('token', t);
  223. localStorage.setItem('auth_token', t);
  224. }, token);
  225. }
  226. /**
  227. * 清除所有认证相关的存储
  228. */
  229. async clearAuth(): Promise<void> {
  230. await this.page.evaluate(() => {
  231. // 清除人才小程序相关的认证数据
  232. localStorage.removeItem('talent_token');
  233. localStorage.removeItem('talent_user');
  234. // 清除其他常见 token 键
  235. localStorage.removeItem('token');
  236. localStorage.removeItem('auth_token');
  237. sessionStorage.removeItem('token');
  238. sessionStorage.removeItem('auth_token');
  239. });
  240. }
  241. // ===== 主页元素验证方法 =====
  242. /**
  243. * 验证主页元素可见(登录后)
  244. * 根据实际小程序主页结构调整
  245. */
  246. async expectHomePageVisible(): Promise<void> {
  247. // 使用 auto-waiting 机制,等待主页元素可见
  248. // 注意:此方法将在 Story 12.7 E2E 测试中使用,当前仅提供基础结构
  249. // 根据实际小程序主页的 data-testid 调整
  250. const dashboard = this.page.getByTestId('talent-dashboard');
  251. await dashboard.waitFor({ state: 'visible', timeout: TIMEOUTS.PAGE_LOAD });
  252. }
  253. /**
  254. * 获取用户信息显示的文本
  255. * @returns 用户信息文本
  256. */
  257. async getUserInfoText(): Promise<string | null> {
  258. const userInfo = this.userInfo;
  259. const count = await userInfo.count();
  260. if (count === 0) {
  261. return null;
  262. }
  263. return await userInfo.textContent();
  264. }
  265. }