utils.ts 11 KB

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