utils.ts 11 KB

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