commands.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-wrapper', { timeout: 5000 }).within(() => {
  66. // process only when Dock Mode
  67. cy.get('.grw-sidebar-dock').within(() => {
  68. const isSidebarContextualNavigationHidden = isHiddenByTestId('grw-contextual-navigation-sub');
  69. if (isSidebarContextualNavigationHidden === isCollapsed) {
  70. return;
  71. }
  72. cy.waitUntil(() => {
  73. // do
  74. cy.getByTestid("grw-navigation-resize-button").click({force: true});
  75. // wait until saving UserUISettings
  76. if (waitUntilSaving) {
  77. // eslint-disable-next-line cypress/no-unnecessary-waiting
  78. cy.wait(1500);
  79. }
  80. // wait until
  81. return cy.getByTestid('grw-contextual-navigation-sub').then($contents => isHidden($contents) === isCollapsed);
  82. });
  83. });
  84. });
  85. });