commands.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, waitUntilSaving = false) => {
  65. cy.getByTestid('grw-sidebar').should('be.visible');
  66. cy.getByTestid('grw-sidebar').within(($elem) => {
  67. // skip if .grw-sidebar-dock does not exist
  68. if ($elem.hasClass('grw-sidebar-dock')) {
  69. return;
  70. }
  71. const isSidebarContextualNavigationHidden = isHiddenByTestId('grw-contextual-navigation-sub');
  72. if (isSidebarContextualNavigationHidden === isCollapsed) {
  73. return;
  74. }
  75. cy.waitUntil(() => {
  76. // do
  77. cy.getByTestid("grw-navigation-resize-button").click({force: true});
  78. // wait until saving UserUISettings
  79. if (waitUntilSaving) {
  80. // eslint-disable-next-line cypress/no-unnecessary-waiting
  81. cy.wait(1500);
  82. }
  83. // wait until
  84. return cy.getByTestid('grw-contextual-navigation-sub').then($contents => isHidden($contents) === isCollapsed);
  85. });
  86. });
  87. });