commands.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'cypress-file-upload';
  2. Cypress.Commands.add('resetDB', () => {
  3. // Although cypress clearly states that a webserver should not be run
  4. // from within it, listmonk is killed, the DB reset, and run again
  5. // in the background. If the DB is reset without restartin listmonk,
  6. // the live Postgres connections in the app throw errors because the
  7. // schema changes midway.
  8. cy.exec(Cypress.env('server_init_command'));
  9. });
  10. // Takes a th class selector of a Buefy table, clicks it sorting the table,
  11. // then compares the values of [td.data-id] attri of all the rows in the
  12. // table against the given IDs, asserting the expected order of sort.
  13. Cypress.Commands.add('sortTable', (theadSelector, ordIDs) => {
  14. cy.get(theadSelector).click();
  15. cy.get('tbody td[data-id]').each(($el, index) => {
  16. expect(ordIDs[index]).to.equal(parseInt($el.attr('data-id')));
  17. });
  18. });
  19. Cypress.Commands.add('loginAndVisit', (url) => {
  20. cy.visit(url, {
  21. auth: {
  22. username: Cypress.env('username'),
  23. password: Cypress.env('password'),
  24. },
  25. });
  26. });
  27. Cypress.Commands.add('clickMenu', (...selectors) => {
  28. selectors.forEach((s) => {
  29. cy.get(`.menu a[data-cy="${s}"]`).click();
  30. });
  31. });
  32. // https://www.nicknish.co/blog/cypress-targeting-elements-inside-iframes
  33. Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe, callback = () => {}) => cy
  34. .wrap($iframe)
  35. .should((iframe) => expect(iframe.contents().find('body')).to.exist)
  36. .then((iframe) => cy.wrap(iframe.contents().find('body')))
  37. .within({}, callback));