commands.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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('serverInitCmd'));
  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.wait(100);
  16. cy.get('tbody td[data-id]').each(($el, index) => {
  17. expect(ordIDs[index]).to.equal(parseInt($el.attr('data-id')));
  18. });
  19. });
  20. Cypress.Commands.add('loginAndVisit', (url) => {
  21. cy.visit(url, {
  22. auth: {
  23. username: Cypress.env('username'),
  24. password: Cypress.env('password'),
  25. },
  26. });
  27. });
  28. Cypress.Commands.add('clickMenu', (...selectors) => {
  29. selectors.forEach((s) => {
  30. cy.get(`.menu a[data-cy="${s}"]`).click();
  31. });
  32. });
  33. // https://www.nicknish.co/blog/cypress-targeting-elements-inside-iframes
  34. Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe, callback = () => { }) => cy
  35. .wrap($iframe)
  36. .should((iframe) => expect(iframe.contents().find('body')).to.exist)
  37. .then((iframe) => cy.wrap(iframe.contents().find('body')))
  38. .within({}, callback));
  39. Cypress.on('uncaught:exception', (err, runnable) => {
  40. if (err.hasOwnProperty('request')) {
  41. const u = err.request.url;
  42. if (u.includes('config') || u.includes('settings') || u.includes('events')) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. });