commands.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. Cypress.Commands.add('getByTestid', (selector, options?) => {
  28. return cy.get(`[data-testid=${selector}]`, options);
  29. });
  30. Cypress.Commands.add('login', (username, password) => {
  31. cy.session(username, () => {
  32. cy.visit('/page-to-return-after-login');
  33. cy.getByTestid('tiUsernameForLogin').type(username);
  34. cy.getByTestid('tiPasswordForLogin').type(password);
  35. cy.intercept('POST', '/_api/v3/login').as('login');
  36. cy.getByTestid('btnSubmitForLogin').click();
  37. cy.wait('@login')
  38. });
  39. });
  40. /**
  41. * use only for the pages which use component with skeleton
  42. */
  43. Cypress.Commands.add('waitUntilSkeletonDisappear', () => {
  44. cy.get('.grw-skeleton').should('exist');
  45. cy.get('.grw-skeleton').should('not.exist');
  46. });
  47. Cypress.Commands.add('waitUntilSpinnerDisappear', () => {
  48. cy.get('.fa-spinner').should('exist');
  49. cy.get('.fa-spinner').should('not.exist');
  50. });
  51. function isVisible($elem: JQuery<Element>) {
  52. return $elem.is(':visible');
  53. }
  54. function isHidden($elem: JQuery<Element>) {
  55. return !isVisible($elem);
  56. }
  57. Cypress.Commands.add('collapseSidebar', (isCollapsed: boolean) => {
  58. cy.getByTestid('grw-contextual-navigation-sub', { timeout: 3000 }).then(($contents) => {
  59. // skip when the current state and isCoolapsed is match
  60. if (isHidden($contents) === isCollapsed) {
  61. return;
  62. }
  63. cy.waitUntil(() => {
  64. // do
  65. cy.getByTestid("grw-navigation-resize-button").click({force: true});
  66. // wait until saving UserUISettings
  67. // eslint-disable-next-line cypress/no-unnecessary-waiting
  68. cy.wait(1500);
  69. // wait until
  70. return cy.getByTestid('grw-contextual-navigation-sub').then($contents => isHidden($contents) === isCollapsed);
  71. });
  72. });
  73. });