utils.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import {StaticResponse} from 'cypress/types/net-stubbing';
  2. import menuJson from '../../fixtures/menu/basic.json';
  3. /** 点击select具体项 */
  4. export function selectClick(testid: string, eq = 0) {
  5. cy.getTestId(testid).click();
  6. cy.getTestId(testid)
  7. .find('.rc-virtual-list-holder-inner')
  8. .children('.ant-select-item')
  9. .eq(eq)
  10. .click();
  11. }
  12. /** 进入具体菜单 */
  13. export function intoMenu(parent: string, child: string) {
  14. cy.wait('@loginIntercept');
  15. const list = menuJson.data;
  16. const menu = list.find(val => val.name === child);
  17. menu && cy.visit(menu);
  18. }
  19. export function clickMenu(parent: string, child: string) {
  20. cy.getTestId('menu')
  21. .children()
  22. .each(function (el) {
  23. const titleElement = el.find('.ant-menu-submenu-title');
  24. if (titleElement.text() === parent) {
  25. titleElement.trigger('click');
  26. el.find(`ul>li[title="${child}"]`).trigger('click');
  27. }
  28. });
  29. }
  30. /** 校验select */
  31. export function validateSelect(testid: string, value: string) {
  32. cy.getTestId(testid)
  33. .find('.ant-select-selection-item')
  34. .should('have.attr', 'title', value)
  35. .and('have.text', value);
  36. }
  37. /** 校验table内容 */
  38. export function validateTableList(tableName: string) {
  39. cy.getTestId(tableName)
  40. .find('table')
  41. .find('.ant-table-tbody')
  42. .children('.ant-table-row')
  43. .first()
  44. .find('td')
  45. .eq(1)
  46. .should('have.text', 'page-1');
  47. cy.getTestId(tableName)
  48. .siblings('.ant-pagination')
  49. .find('li[title="2"]')
  50. .click();
  51. cy.getTestId(tableName)
  52. .find('table')
  53. .find('.ant-table-tbody')
  54. .children('.ant-table-row')
  55. .first()
  56. .find('td')
  57. .eq(1)
  58. .should('have.text', 'page-2');
  59. }
  60. /** 选择时间组件 */
  61. export function clickDatePicker(id: string) {
  62. cy.getTestId(id).click();
  63. cy.get('.ant-picker-panels')
  64. .children()
  65. .eq(0)
  66. .find('.ant-picker-content')
  67. .find('tbody')
  68. .find('td')
  69. .eq(0)
  70. .click();
  71. cy.get('.ant-picker-panels')
  72. .children()
  73. .eq(1)
  74. .find('.ant-picker-content')
  75. .find('tbody')
  76. .find('td')
  77. .eq(0)
  78. .click();
  79. }
  80. /** 校验table search */
  81. export function validateTableSearch(
  82. tableName: string,
  83. keys: (
  84. | {
  85. id: string;
  86. type: 'select';
  87. value: string;
  88. eq?: number;
  89. defaultValue?: string;
  90. }
  91. | {id: string; type: 'date'}
  92. | string
  93. )[],
  94. options: {
  95. toolId: string;
  96. url: string;
  97. },
  98. ) {
  99. const {toolId, url} = options;
  100. cy.getTestId(tableName)
  101. .siblings('.ant-pagination')
  102. .find('li[title="2"]')
  103. .click();
  104. function clear(data?: (typeof keys)[0]) {
  105. function clearData(key: (typeof keys)[0]) {
  106. if (typeof key === 'string') {
  107. cy.getTestId(toolId).find(`[data-testid="filter_${key}"]`).clear();
  108. } else {
  109. const {id, type} = key;
  110. const clearId =
  111. type === 'select' ? '.ant-select-clear' : '.ant-picker-clear';
  112. cy.getTestId(toolId)
  113. .find(`[data-testid="filter_${id}"]`)
  114. .trigger('mouseover')
  115. .find(clearId)
  116. .click();
  117. }
  118. }
  119. if (data) {
  120. return clearData(data);
  121. }
  122. keys.forEach(clearData);
  123. }
  124. function validate(text: string, reset?: boolean) {
  125. !reset && cy.getTestId(toolId).find('[data-testid="search_btn"]').click();
  126. cy.getTestId(toolId)
  127. .find('[data-testid="search_btn"]')
  128. .should('have.class', 'ant-btn-loading');
  129. cy.getTestId(tableName)
  130. .find('table')
  131. .find('.ant-table-tbody')
  132. .children('.ant-table-row')
  133. .first()
  134. .find('td')
  135. .eq(1)
  136. .should('have.text', text);
  137. cy.getTestId(tableName)
  138. .siblings('.ant-pagination')
  139. .find('li[title="1"]')
  140. .should('have.class', 'ant-pagination-item-active');
  141. cy.getTestId(toolId)
  142. .getTestId('search_btn')
  143. .should('not.have.class', 'ant-btn-loading');
  144. }
  145. function eachAllField(skipValidate = false) {
  146. keys.forEach(function (key, idx) {
  147. if (idx > 0 && !skipValidate) {
  148. clear(keys[idx - 1]);
  149. }
  150. let validateText: string;
  151. if (typeof key === 'string') {
  152. cy.getTestId(toolId).find(`[data-testid="filter_${key}"]`).type(key);
  153. validateText = key;
  154. } else {
  155. const {id, type} = key;
  156. switch (type) {
  157. case 'select':
  158. selectClick(`filter_${id}`, key?.eq ?? 0);
  159. validateText = key.value;
  160. break;
  161. case 'date':
  162. clickDatePicker(`filter_${id}`);
  163. validateText = 'searchTime';
  164. break;
  165. }
  166. }
  167. !skipValidate && validate(validateText);
  168. });
  169. }
  170. eachAllField();
  171. // 校验重置功能
  172. eachAllField(true);
  173. cy.getTestId(toolId).find('[data-testid="reset_btn"]').click();
  174. keys.forEach(function (key) {
  175. if (typeof key === 'string') {
  176. cy.getTestId(`filter_${key}`).invoke('val').should('be.empty');
  177. } else {
  178. switch (key.type) {
  179. case 'select':
  180. if (key.defaultValue) {
  181. validateSelect(`filter_${key.id}`, key.defaultValue);
  182. break;
  183. }
  184. cy.getTestId(`filter_${key.id}`)
  185. .find('.ant-select-selection-placeholder')
  186. .should('exist');
  187. break;
  188. case 'date':
  189. cy.getTestId(`filter_${key.id}`)
  190. .children('.ant-picker-input')
  191. .first()
  192. .find('input')
  193. .invoke('val')
  194. .should('be.empty');
  195. cy.getTestId(`filter_${key.id}`)
  196. .children('.ant-picker-input')
  197. .last()
  198. .find('input')
  199. .invoke('val')
  200. .should('be.empty');
  201. break;
  202. }
  203. }
  204. });
  205. validate('page-1', true);
  206. // 校验刷新功能
  207. cy.getTestId(tableName)
  208. .closest('.ant-card-body')
  209. .find('[data-testid="refresh_btn"]')
  210. .click();
  211. cy.getTestId(tableName)
  212. .closest('.ant-card-body')
  213. .find('[data-testid="refresh_btn"]')
  214. .should('have.class', 'ant-btn-loading');
  215. cy.wait(`@${url}`);
  216. cy.getTestId(tableName)
  217. .closest('.ant-card-body')
  218. .find('[data-testid="refresh_btn"]')
  219. .should('not.have.class', 'ant-btn-loading');
  220. }
  221. /** 表格内按钮点击 */
  222. export function tableBtnClick(tableName: string, index: number) {
  223. function getBtn() {
  224. return cy
  225. .getTestId(tableName)
  226. .find('table')
  227. .find('.ant-table-tbody')
  228. .children('.ant-table-row')
  229. .first()
  230. .find('td')
  231. .last()
  232. .children()
  233. .eq(index);
  234. }
  235. getBtn().click();
  236. return getBtn;
  237. }
  238. /** 弹窗按钮校验 */
  239. export function validateModalBtnGroup(modalName: string) {
  240. cy.getTestId(modalName)
  241. .getTestId('modal_btn_group')
  242. .find('.ant-btn')
  243. .should('have.class', 'ant-btn-loading');
  244. }
  245. // 校验antd message内容
  246. export function validateMessageContent(msg: string) {
  247. cy.get('.ant-message-notice-content').should('include.text', msg);
  248. }
  249. // 校验modal的操作结果
  250. export function validateModalOperation(modalName: string, text: string) {
  251. cy.getTestId(modalName).find('form').submit();
  252. validateModalBtnGroup(modalName);
  253. validateMessageContent(text);
  254. cy.getTestId(modalName).should('not.exist');
  255. }
  256. /** 校验新增或修改事件 */
  257. export function validatePut(
  258. modalName: string,
  259. tableName: string,
  260. options: {label: string},
  261. ) {
  262. const {label} = options;
  263. function validateAdd(
  264. keys: (
  265. | string
  266. | {id: string; type: 'select'}
  267. | {id: string; type: 'keySelect'}
  268. | {id: string; type: 'field'; value: string}
  269. )[],
  270. ) {
  271. cy.getTestId(tableName)
  272. .closest('.ant-card-body')
  273. .find('[data-testid="add_btn"]')
  274. .click();
  275. cy.getTestId(modalName).should('exist').and('be.visible');
  276. cy.getTestId(modalName).find('h3').should('have.text', `新增${label}`);
  277. keys.forEach(function (key) {
  278. if (typeof key === 'string') {
  279. cy.getTestId(`field_${key}`).type(key);
  280. } else {
  281. const {type, id} = key;
  282. switch (type) {
  283. case 'select':
  284. selectClick(`field_${id}`, 0);
  285. break;
  286. case 'field':
  287. cy.getTestId(`field_${id}`).clear().type(key.value);
  288. break;
  289. case 'keySelect':
  290. cy.get(`#field_${id}`).clear().type(id);
  291. selectClick(`field_${id}`, 0);
  292. break;
  293. }
  294. }
  295. });
  296. validateModalOperation(modalName, '新增成功');
  297. }
  298. function validateEdit(
  299. keys: {id: string; type: 'field' | 'select'; value: string}[],
  300. eq = 0,
  301. ) {
  302. tableBtnClick(tableName, eq);
  303. cy.getTestId(modalName).should('exist').and('be.visible');
  304. cy.getTestId(modalName).find('h3').should('include.text', `修改${label}`);
  305. keys.forEach(function ({id, type, value}) {
  306. switch (type) {
  307. case 'field':
  308. cy.getTestId(`field_${id}`).should('have.value', value);
  309. break;
  310. case 'select':
  311. validateSelect(`field_${id}`, value);
  312. break;
  313. }
  314. });
  315. validateModalOperation(modalName, '修改成功');
  316. }
  317. return {validateAdd, validateEdit};
  318. }
  319. /** 校验antd dialog信息 */
  320. export function validateDialog(
  321. title: string,
  322. content: string,
  323. options?: {confirm?: boolean},
  324. ) {
  325. const {confirm = true} = options ?? {};
  326. cy.get('.ant-modal-confirm-title').should('include.text', title);
  327. cy.get('.ant-modal-confirm-content').should('include.text', content);
  328. cy.get('.ant-modal-confirm-btns')
  329. .children()
  330. .eq(confirm ? 1 : 0)
  331. .click();
  332. }
  333. /** 校验删除 */
  334. export function validateDelete(
  335. tableName: string,
  336. label: string,
  337. options: {eq?: number; title: string},
  338. ) {
  339. const {eq = 1, title} = options;
  340. tableBtnClick(tableName, eq);
  341. cy.get('.ant-modal-content').should('be.visible');
  342. validateDialog(title, `你确定要删除当前${label}吗?`);
  343. // 删除按钮在加载
  344. cy.getTestId(tableName)
  345. .find('table')
  346. .find('.ant-table-tbody')
  347. .children('.ant-table-row')
  348. .first()
  349. .find('td')
  350. .last()
  351. .children()
  352. .eq(eq)
  353. .should('have.class', 'ant-btn-loading');
  354. // 其他按钮禁用
  355. cy.getTestId(tableName)
  356. .find('table')
  357. .find('.ant-table-tbody')
  358. .children('.ant-table-row')
  359. .first()
  360. .find('td')
  361. .last()
  362. .children()
  363. .each(function (el, idx) {
  364. if (idx !== eq) {
  365. expect(el).to.have.attr('disabled');
  366. }
  367. });
  368. validateMessageContent('删除成功');
  369. }
  370. /** 校验导出 */
  371. export function validateExport(tableName: string) {
  372. cy.getTestId(tableName)
  373. .closest('.ant-card-body')
  374. .find('[data-testid="export_btn"]')
  375. .click();
  376. cy.getTestId(tableName)
  377. .closest('.ant-card-body')
  378. .find('[data-testid="export_btn"]')
  379. .should('have.class', 'ant-btn-loading');
  380. cy.getTestId(tableName)
  381. .closest('.ant-card-body')
  382. .find('[data-testid="export_btn"]')
  383. .should('not.have.class', 'ant-btn-loading');
  384. }
  385. /** 关闭modal */
  386. export function closeModal() {
  387. cy.get('.ReactModal__Overlay').trigger('click', 'topRight');
  388. }
  389. /** 选择所有的筛选框 */
  390. export function selectAllFilters(toolId: string, count: number) {
  391. cy.getTestId(toolId).getTestId('filter_btn').click();
  392. cy.get('.ant-transfer')
  393. .find('.ant-transfer-list')
  394. .first()
  395. .find('.ant-transfer-list-header')
  396. .find('input[type="checkbox"]')
  397. .check();
  398. cy.get('.ant-transfer')
  399. .find('.ant-transfer-operation')
  400. .find('button')
  401. .first()
  402. .click();
  403. cy.getTestId('filter_select_modal').find('form').submit();
  404. cy.getTestId(toolId)
  405. .children('.ant-space')
  406. .first()
  407. .children()
  408. .should('have.length', count + 1);
  409. }
  410. /** 生成返回内容 */
  411. export function generateResult<T extends Record<string, any>>(
  412. data: T,
  413. length: number,
  414. options: {
  415. limit: string;
  416. key?: string;
  417. isList?: boolean;
  418. title?: keyof T;
  419. value?: string;
  420. },
  421. ) {
  422. const {key = 'id', isList = false, title, value, limit} = options;
  423. const result = Array.from({length: Number(limit)}, function (_, idx) {
  424. const temp = {...data, [key]: idx + 1};
  425. return title ? {...temp, [title]: value ?? ''} : temp;
  426. });
  427. return {msg: '200', data: isList ? {total: length, list: result} : result};
  428. }
  429. /** 生成请求结果 */
  430. export function generateNetworkResult<
  431. T extends Record<string, unknown>,
  432. >(options: {
  433. search: URLSearchParams;
  434. basicData: T;
  435. reply: (params: StaticResponse) => void;
  436. title: keyof T;
  437. skipCondition?: (name: string) => boolean;
  438. replaceValue?: string;
  439. }) {
  440. const {
  441. search,
  442. basicData,
  443. reply,
  444. title,
  445. skipCondition = () => false,
  446. replaceValue,
  447. } = options;
  448. const page = search.get('page'),
  449. limit = search.get('limit');
  450. for (const [name, searchValue] of search.entries()) {
  451. if (
  452. name === 'page' ||
  453. name === 'limit' ||
  454. name === 'id' ||
  455. skipCondition(name)
  456. )
  457. continue;
  458. if (searchValue) {
  459. return reply({
  460. body: generateResult(basicData, Number(limit) * 3, {
  461. limit,
  462. isList: true,
  463. title,
  464. value:
  465. name === 'startTime' || name === 'endTime'
  466. ? 'searchTime'
  467. : replaceValue ?? searchValue,
  468. }),
  469. });
  470. }
  471. }
  472. if (search.has('id') && search.get('id').length > 0) {
  473. return reply({
  474. body: generateResult(basicData, 1, {limit: '1', isList: true}),
  475. });
  476. }
  477. reply({
  478. body: generateResult(basicData, Number(limit) * 3, {
  479. limit,
  480. isList: true,
  481. title,
  482. value: replaceValue ?? `page-${page}`,
  483. }),
  484. });
  485. }