Catching emails with Cypress-Mailosaur

Viewed 107

I have a test where I need to test a confirmation link in an email being sent.

Im at a point where I am catching the email and getting the link, but when I then try to visit that link I am getting an error link not defined

  it("I fill out the form", () => {
    cy.visit(Cypress.env("gettingStartedUrl"))
    cy.getElementById('firstName').click().type("Test")
    cy.getElementById('lastName').click().type("Test")
    cy.contains("Next").click()
  })
 it("I get the link from the confirmation email", () => {
    cy.mailosaurGetMessage(serverId, {
      sentTo: emailAddress
    }).then(email => {
      expect(email.subject).to.equal('Welcome to Dashboard')
      confirmationLink = email.text.links[1].href;
    })
  })
  it('Visits the login link', () => {
    // This is where I get confirmationLink not defined
    cy.visit(confirmationLink)
  })

There is a second in the test runner that I can see it is trying to redirect to the right link, but then the test runner freezes up and I get the confirmationLink is not defined

5 Answers

You can save the link to an environment variable

 it("I fill out the form", () => {
    cy.visit(Cypress.env("gettingStartedUrl"))
    cy.getElementById('firstName').click().type("Test")
    cy.getElementById('lastName').click().type("Test")
    cy.contains("Next").click()
  })
 it("I get the link from the confirmation email", () => {
    cy.mailosaurGetMessage(serverId, {
      sentTo: emailAddress
    }).then(email => {
      expect(email.subject).to.equal('Welcome to Dashboard')
      Cypress.env('confirmationLink', email.text.links[1].href)
    })
  })
  it('Visits the login link', () => {
    // This is where I get confirmationLink not defined
    cy.visit(Cypress.env('confirmationLink'))
  })

You can combine the 2nd and 3rd tests, they are really parts of the same test

it("I fill out the form", () => {
    cy.visit(Cypress.env("gettingStartedUrl"))
    cy.getElementById('firstName').click().type("Test")
    cy.getElementById('lastName').click().type("Test")
    cy.contains("Next").click()
  })
 it("I get the link from the confirmation email", () => {
    cy.mailosaurGetMessage(serverId, {
      sentTo: emailAddress
    }).then(email => {
      expect(email.subject).to.equal('Welcome to Dashboard')
      const confirmationLink = email.text.links[1].href;
      cy.visit(confirmationLink)
    })
  })

Just declare the confirmationLink variable

 let confirmationLink;

 it("I fill out the form", () => {
    cy.visit(Cypress.env("gettingStartedUrl"))
    cy.getElementById('firstName').click().type("Test")
    cy.getElementById('lastName').click().type("Test")
    cy.contains("Next").click()
  })
 it("I get the link from the confirmation email", () => {
    cy.mailosaurGetMessage(serverId, {
      sentTo: emailAddress
    }).then(email => {
      expect(email.subject).to.equal('Welcome to Dashboard')
      confirmationLink = email.text.links[1].href;
    })
  })

  it('Visits the login link', () => {
    const newOrigin = confirmationLink.split('?')[0] 
    cy.origin(newOrigin, { args: { confirmationLink } }, ({ confirmationLink }) => {
      cy.visit(confirmationLink)
    })
  })

This is mostly because the scope for the variable confirmationLink is local. To make sure you can access the link value in the next tests, you can use Cypress.env(), something like:

it('I fill out the form', () => {
  cy.visit(Cypress.env('gettingStartedUrl'))
  cy.getElementById('firstName').click().type('Test')
  cy.getElementById('lastName').click().type('Test')
  cy.contains('Next').click()
})
it('I get the link from the confirmation email', () => {
  cy.mailosaurGetMessage(serverId, {
    sentTo: emailAddress,
  }).then((email) => {
    expect(email.subject).to.equal('Welcome to Dashboard')
    Cypress.env('confirmationLink', email.text.links[1].href)
  })
})
it('Visits the login link', () => {
  // This is where I get confirmationLink not defined
  cy.visit(Cypress.env('confirmationLink'))
})

try this way: Cypress.env('confirmationLink', email.text.links[0].href)

Related