utils.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. export function selectClick(testid: string, eq = 0) {
  2. cy.getTestId(testid).click();
  3. cy.getTestId(testid).find('.rc-virtual-list-holder-inner')
  4. .children().eq(eq).click();
  5. }
  6. export function menuTrigger(parent: number, child: number) {
  7. cy.getTestId('menu').children().each(function(el, idx) {
  8. if (idx === parent) {
  9. el.find('.ant-menu-submenu-title').trigger('click');
  10. el.find('ul>li').children().eq(child).trigger('click');
  11. }
  12. });
  13. }
  14. export function validateSelect(testid: string, value: string) {
  15. cy.getTestId(testid).find('.ant-select-selection-item')
  16. .should('have.attr', 'title', value).and('have.text', value);
  17. }
  18. export function validateTableList(tableName: string, firstPageNumber = 3, secondPageNumber = 2) {
  19. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  20. .children('.ant-table-row').should('have.length', firstPageNumber);
  21. cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="2"]').click();
  22. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  23. .children('.ant-table-row').should('have.length', secondPageNumber);
  24. }
  25. export function validateTableSearch(
  26. tableName: string,
  27. value?: string,
  28. options?: {btnTestId?: string},
  29. ) {
  30. cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="2"]').click();
  31. const {btnTestId} = options ?? {};
  32. return function(text?: string) {
  33. cy.getTestId(btnTestId ?? 'search_btn').click();
  34. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  35. .children('.ant-table-row').first().find('td').first()
  36. .should('include.text', text ?? value);
  37. cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="1"]')
  38. .should('have.class', 'ant-pagination-item-active');
  39. };
  40. }
  41. export function tableBtnClick(tableName: string, index: number) {
  42. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  43. .children('.ant-table-row').first().find('td').last().children().eq(index)
  44. .click();
  45. }
  46. export function validatePut(
  47. modalName: string,
  48. label: string,
  49. options?: {addBtnTestId?: string},
  50. ) {
  51. const {addBtnTestId} = options ?? {};
  52. function validateBtnGroup() {
  53. cy.getTestId('modal_btn_group').find('.ant-btn').first()
  54. .should('have.class', 'ant-btn-loading');
  55. cy.getTestId('modal_btn_group').find('.ant-btn').last()
  56. .should('have.attr', 'disabled');
  57. }
  58. function validateAdd(fn: () => void) {
  59. cy.getTestId(addBtnTestId ?? 'add_btn').click();
  60. cy.getTestId(modalName).should('exist').and('be.visible');
  61. cy.getTestId(modalName).find('h3')
  62. .should('include.text', `新增${label}`);
  63. fn();
  64. cy.getTestId(modalName).find('form').submit();
  65. validateBtnGroup();
  66. cy.get('.ant-message-notice-content').should('include.text', '新增成功');
  67. cy.getTestId(modalName).should('not.exist');
  68. }
  69. function validateEdit(tableName: string, validateFn: () => void) {
  70. tableBtnClick(tableName, 0);
  71. cy.getTestId(modalName).should('exist').and('be.visible');
  72. cy.getTestId(modalName).find('h3').should('include.text', `修改${label}`);
  73. validateFn();
  74. cy.getTestId(modalName).find('form').submit();
  75. validateBtnGroup();
  76. cy.get('.ant-message-notice-content').should('include.text', '修改成功');
  77. cy.getTestId(modalName).should('not.exist');
  78. }
  79. return {validateAdd, validateEdit};
  80. }
  81. export function validateDelete(
  82. tableName: string,
  83. label: string,
  84. ) {
  85. tableBtnClick(tableName, 1);
  86. cy.get('.ant-modal-content').should('be.visible');
  87. cy.get('.ant-modal-confirm-title').should('include.text', `删除${label}`);
  88. cy.get('.ant-modal-confirm-content').should(
  89. 'include.text',
  90. `你确定要删除当前${label}吗?`,
  91. );
  92. cy.get('.ant-modal-confirm-btns').children().last().trigger('click');
  93. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  94. .children('.ant-table-row').first().find('td').last().children().eq(1)
  95. .should('have.class', 'ant-btn-loading');
  96. cy.getTestId(tableName).find('table').find('.ant-table-tbody')
  97. .children('.ant-table-row').first().find('td').last().children().first()
  98. .should('have.attr', 'disabled');
  99. cy.get('.ant-message-notice-content').should('include.text', '删除成功');
  100. }
  101. export function validateExport() {
  102. cy.getTestId('export_btn').click();
  103. cy.getTestId('export_btn').should('have.class', 'ant-btn-loading');
  104. cy.wait('@export');
  105. cy.getTestId('export_btn').should('not.have.class', 'ant-btn-loading');
  106. }