commands.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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').within(($sidebar) => {
  66. // skip if .grw-sidebar-dock does not exist
  67. if (!$sidebar.hasClass('grw-sidebar-dock')) {
  68. return;
  69. }
  70. });
  71. cy.getByTestid('grw-sidebar').should('be.visible').within(() => {
  72. const isSidebarContentsHidden = isHiddenByTestId('grw-sidebar-contents');
  73. if (isSidebarContentsHidden === isCollapsed) {
  74. return;
  75. }
  76. cy.waitUntil(() => {
  77. // do
  78. cy.getByTestid("grw-switch-collapse-button").click({force: true});
  79. // wait until saving UserUISettings
  80. if (waitUntilSaving) {
  81. // eslint-disable-next-line cypress/no-unnecessary-waiting
  82. cy.wait(1500);
  83. }
  84. // wait until
  85. return cy.getByTestid('grw-sidebar-contents').then($contents => isHidden($contents) === isCollapsed);
  86. });
  87. });
  88. });
  89. Cypress.Commands.add('appendTextToEditorUntilContains', (inputText: string) => {
  90. const lines: string[] = [];
  91. cy.waitUntil(() => {
  92. // do
  93. cy.get('.CodeMirror textarea').type(inputText, { force: true });
  94. // until
  95. return cy.get('.CodeMirror-line')
  96. .each(($item) => {
  97. lines.push($item.text());
  98. }).then(() => {
  99. return lines.join('\n').endsWith(inputText);
  100. });
  101. });
  102. });