utils.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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(keys: (string | {id: string; type: 'select'})[]) {
  184. cy.getTestId(tableName)
  185. .closest('.ant-card-body')
  186. .find('[data-testid="add_btn"]')
  187. .click();
  188. cy.getTestId(modalName).should('exist').and('be.visible');
  189. cy.getTestId(modalName).find('h3').should('include.text', `新增${label}`);
  190. keys.forEach(function (key) {
  191. if (typeof key === 'string') {
  192. cy.getTestId(`field_${key}`).type(key);
  193. } else {
  194. const {type, id} = key;
  195. switch (type) {
  196. case 'select':
  197. selectClick(`field_${id}`, 0);
  198. break;
  199. }
  200. }
  201. });
  202. validateModalOperation(modalName, '新增成功');
  203. }
  204. function validateEdit(
  205. keys: {id: string; type: 'field' | 'select'; value: string}[],
  206. eq = 0,
  207. ) {
  208. tableBtnClick(tableName, eq);
  209. cy.getTestId(modalName).should('exist').and('be.visible');
  210. cy.getTestId(modalName).find('h3').should('include.text', `修改${label}`);
  211. keys.forEach(function ({id, type, value}) {
  212. switch (type) {
  213. case 'field':
  214. cy.getTestId(`field_${id}`).should('have.value', value);
  215. break;
  216. case 'select':
  217. validateSelect(`field_${id}`, value);
  218. break;
  219. }
  220. });
  221. validateModalOperation(modalName, '修改成功');
  222. }
  223. return {validateAdd, validateEdit};
  224. }
  225. /** 校验antd dialog信息 */
  226. export function validateDialog(
  227. title: string,
  228. content: string,
  229. options?: {confirm?: boolean},
  230. ) {
  231. const {confirm = true} = options ?? {};
  232. cy.get('.ant-modal-confirm-title').should('include.text', title);
  233. cy.get('.ant-modal-confirm-content').should('include.text', content);
  234. cy.get('.ant-modal-confirm-btns')
  235. .children()
  236. .eq(confirm ? 1 : 0)
  237. .click();
  238. }
  239. /** 校验删除 */
  240. export function validateDelete(
  241. tableName: string,
  242. label: string,
  243. options: {eq?: number; title: string},
  244. ) {
  245. const {eq = 1, title} = options;
  246. tableBtnClick(tableName, eq);
  247. cy.get('.ant-modal-content').should('be.visible');
  248. validateDialog(title, `你确定要删除当前${label}吗?`);
  249. // 删除按钮在加载
  250. cy.getTestId(tableName)
  251. .find('table')
  252. .find('.ant-table-tbody')
  253. .children('.ant-table-row')
  254. .first()
  255. .find('td')
  256. .last()
  257. .children()
  258. .eq(eq)
  259. .should('have.class', 'ant-btn-loading');
  260. // 其他按钮禁用
  261. cy.getTestId(tableName)
  262. .find('table')
  263. .find('.ant-table-tbody')
  264. .children('.ant-table-row')
  265. .first()
  266. .find('td')
  267. .last()
  268. .children()
  269. .each(function (el, idx) {
  270. if (idx !== eq) {
  271. expect(el).to.have.attr('disabled');
  272. }
  273. });
  274. validateMessageContent('删除成功');
  275. }
  276. /** 校验导出 */
  277. export function validateExport(tableName: string) {
  278. const card = cy.getTestId(tableName).closest('.ant-card-body');
  279. card.getTestId('export_btn').click();
  280. card.getTestId('export_btn').should('have.class', 'ant-btn-loading');
  281. card.getTestId('export_btn').should('not.have.class', 'ant-btn-loading');
  282. }
  283. /** 关闭modal */
  284. export function closeModal() {
  285. cy.get('.ReactModal__Overlay').trigger('click', 'topRight');
  286. }
  287. /** 选择时间组件 */
  288. export function clickDatePicker(id: string) {
  289. cy.getTestId(id).click();
  290. cy.get('.ant-picker-panels')
  291. .children()
  292. .eq(0)
  293. .find('.ant-picker-content')
  294. .find('tbody')
  295. .find('td')
  296. .eq(0)
  297. .click();
  298. cy.get('.ant-picker-panels')
  299. .children()
  300. .eq(1)
  301. .find('.ant-picker-content')
  302. .find('tbody')
  303. .find('td')
  304. .eq(0)
  305. .click();
  306. }
  307. /** 选择所有的筛选框 */
  308. export function selectAllFilters(toolId: string, count: number) {
  309. indexedDB.deleteDatabase('filterGroup');
  310. cy.getTestId(toolId).getTestId('filter_btn').click();
  311. cy.get('.ant-transfer')
  312. .find('.ant-transfer-list')
  313. .first()
  314. .find('.ant-transfer-list-header')
  315. .find('input[type="checkbox"]')
  316. .check();
  317. cy.get('.ant-transfer')
  318. .find('.ant-transfer-operation')
  319. .find('button')
  320. .first()
  321. .click();
  322. cy.getTestId('filter_select_modal').find('form').submit();
  323. cy.getTestId(toolId)
  324. .children('.ant-space')
  325. .first()
  326. .children()
  327. .should('have.length', count + 1);
  328. }
  329. /** 生成返回内容 */
  330. export function generateResult<T extends Record<string, any>>(
  331. data: T,
  332. length: number,
  333. options: {
  334. limit: string;
  335. key?: string;
  336. isList?: boolean;
  337. title?: keyof T;
  338. value?: string;
  339. },
  340. ) {
  341. const {key = 'id', isList = false, title, value, limit} = options;
  342. const result = Array.from({length: Number(limit)}, function (_, idx) {
  343. const temp = {...data, [key]: idx + 1};
  344. return title ? {...temp, [title]: value ?? ''} : temp;
  345. });
  346. return {msg: '200', data: isList ? {total: length, list: result} : result};
  347. }
  348. /** 生成请求结果 */
  349. export function generateNetworkResult<
  350. T extends Record<string, unknown>,
  351. >(options: {
  352. search: URLSearchParams;
  353. basicData: T;
  354. reply: (params: StaticResponse) => void;
  355. title: keyof T;
  356. skipCondition?: (name: string) => boolean;
  357. }) {
  358. const {
  359. search,
  360. basicData,
  361. reply,
  362. title,
  363. skipCondition = () => false,
  364. } = options;
  365. const page = search.get('page'),
  366. limit = search.get('limit');
  367. for (const [name, searchValue] of search.entries()) {
  368. if (
  369. name === 'page' ||
  370. name === 'limit' ||
  371. name === 'id' ||
  372. skipCondition(name)
  373. )
  374. continue;
  375. if (searchValue) {
  376. return reply({
  377. body: generateResult(basicData, Number(limit) * 3, {
  378. limit,
  379. isList: true,
  380. title,
  381. value: searchValue,
  382. }),
  383. });
  384. }
  385. }
  386. if (search.has('id') && search.get('id').length > 0) {
  387. return reply({
  388. body: generateResult(basicData, 1, {limit: '1', isList: true}),
  389. });
  390. }
  391. reply({
  392. body: generateResult(basicData, Number(limit) * 3, {
  393. limit,
  394. isList: true,
  395. title,
  396. value: `page-${page}`,
  397. }),
  398. });
  399. }