commands.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // ***********************************************
  2. // This example commands.js shows you how to
  3. // create various custom commands and overwrite
  4. // existing commands.
  5. //
  6. // For more comprehensive examples of custom
  7. // commands please read more here:
  8. // https://on.cypress.io/custom-commands
  9. // ***********************************************
  10. //
  11. //
  12. // -- This is a parent command --
  13. // Cypress.Commands.add('login', (email, password) => { ... })
  14. //
  15. //
  16. // -- This is a child command --
  17. // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
  18. //
  19. //
  20. // -- This is a dual command --
  21. // Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
  22. //
  23. //
  24. // -- This will overwrite an existing command --
  25. // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
  26. import 'cypress-wait-until';
  27. function isVisible($elem: JQuery<Element>) {
  28. return $elem.is(':visible');
  29. }
  30. function isHidden($elem: JQuery<Element>) {
  31. return !isVisible($elem);
  32. }
  33. function isVisibleByTestId(testId: string) {
  34. return isVisible(Cypress.$(`[data-testid=${testId}]`));
  35. }
  36. function isHiddenByTestId(testId: string) {
  37. return !isVisibleByTestId(testId);
  38. }
  39. Cypress.Commands.add('getByTestid', (selector, options?) => {
  40. return cy.get(`[data-testid=${selector}]`, options);
  41. });
  42. Cypress.Commands.add('login', (username, password) => {
  43. cy.session(username, () => {
  44. cy.visit('/page-to-return-after-login');
  45. cy.getByTestid('tiUsernameForLogin').type(username);
  46. cy.getByTestid('tiPasswordForLogin').type(password);
  47. cy.intercept('POST', '/_api/v3/login').as('login');
  48. cy.getByTestid('btnSubmitForLogin').click();
  49. cy.wait('@login')
  50. });
  51. });
  52. Cypress.Commands.add('waitUntilSkeletonDisappear', () => {
  53. if (isHidden(Cypress.$('.grw-skeleton'))) {
  54. return;
  55. }
  56. cy.get('.grw-skeleton').should('not.exist');
  57. });
  58. Cypress.Commands.add('waitUntilSpinnerDisappear', () => {
  59. if (isHidden(Cypress.$('.fa-spinner'))) {
  60. return;
  61. }
  62. cy.get('.fa-spinner').should('not.exist');
  63. });
  64. Cypress.Commands.add('collapseSidebar', (isCollapsed: boolean) => {
  65. const isSidebarExists = isVisibleByTestId('grw-sidebar-wrapper');
  66. if (!isSidebarExists) {
  67. return;
  68. }
  69. const isSidebarContextualNavigationHidden = isHiddenByTestId('grw-contextual-navigation-sub');
  70. if (isSidebarContextualNavigationHidden === isCollapsed) {
  71. return;
  72. }
  73. cy.waitUntil(() => {
  74. // do
  75. cy.getByTestid("grw-navigation-resize-button").click({force: true});
  76. // wait until saving UserUISettings
  77. // eslint-disable-next-line cypress/no-unnecessary-waiting
  78. cy.wait(1500);
  79. // wait until
  80. return cy.getByTestid('grw-contextual-navigation-sub').then($contents => isHidden($contents) === isCollapsed);
  81. });
  82. });