commands.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. Cypress.Commands.add('getByTestid', (selector, options?) => {
  27. return cy.get(`[data-testid=${selector}]`, options);
  28. });
  29. Cypress.Commands.add('login', (username, password) => {
  30. cy.session(username, () => {
  31. cy.visit('/page-to-return-after-login');
  32. cy.getByTestid('tiUsernameForLogin').type(username);
  33. cy.getByTestid('tiPasswordForLogin').type(password);
  34. cy.getByTestid('btnSubmitForLogin').click();
  35. });
  36. });
  37. let isSidebarCollapsed: boolean | undefined;
  38. Cypress.Commands.add('collapseSidebar', (isCollapsed) => {
  39. if (isSidebarCollapsed === isCollapsed) {
  40. return;
  41. }
  42. const isGrowiPage = Cypress.$('body.growi').length > 0;
  43. if (!isGrowiPage) {
  44. cy.visit('/page-to-toggle-sidebar-collapsed');
  45. }
  46. cy.getByTestid('grw-contextual-navigation-sub').then(($contents) => {
  47. const isCurrentCollapsed = $contents.hasClass('d-none');
  48. // toggle when the current state and isCoolapsed is not match
  49. if (isCurrentCollapsed !== isCollapsed) {
  50. cy.getByTestid("grw-navigation-resize-button").click({force: true});
  51. // wait until saving UserUISettings
  52. // eslint-disable-next-line cypress/no-unnecessary-waiting
  53. cy.wait(1500);
  54. }
  55. });
  56. isSidebarCollapsed = isCollapsed;
  57. });