email.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Helper function for opening an email in Roundcube based mailtrap.
  3. * We need to cross an origin boundary, which complicates the use of variables.
  4. * Any variables need to be explicitly defined and the "runner" may only reference these and none from its scope.
  5. * It is not possible to use Cypress helper functions, e.g. from the testing library or other functions like "activateUser", inside the "runner".
  6. * REF: https://github.com/testing-library/cypress-testing-library/issues/221
  7. */
  8. export function openEmail<T>(
  9. subject: string | RegExp,
  10. runner: (frame: Cypress.Chainable<JQuery<any>>, args: T) => void,
  11. args?: T
  12. ) {
  13. const runnerS = runner.toString()
  14. cy.origin(
  15. 'http://mailtrap',
  16. { args: { args, runnerS, subject } },
  17. ({ args, runnerS, subject }) => {
  18. cy.visit('/')
  19. cy.get('input[name="_user"]').type('mailtrap')
  20. cy.get('input[name="_pass"]').type('password-for-mailtrap')
  21. cy.get('button[type="submit"]').click()
  22. cy.log('mailtrap login is flaky in cypress, submit again')
  23. cy.get('input[name="_pass"]').type('password-for-mailtrap')
  24. cy.get('button[type="submit"]').click()
  25. // Use force as the subject is partially hidden
  26. cy.contains(subject).click({ force: true })
  27. cy.log('wait for iframe loading')
  28. cy.wait(1000)
  29. cy.get('iframe[id="messagecontframe"]').then(frame => {
  30. // runnerS='(frame, args) => { runner body }'. Extract the runnable function.
  31. const runner = new Function('return ' + runnerS)()
  32. runner(cy.wrap(frame.prop('contentWindow').document.body), args)
  33. })
  34. }
  35. )
  36. }