3

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

Fody
  • 23,754
  • 3
  • 20
  • 37
ItsNotAndy
  • 563
  • 5
  • 17

5 Answers5

2

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)
    })
  })
Pablo
  • 310
  • 1
  • 6
1

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)
    })
  })
Fody
  • 23,754
  • 3
  • 20
  • 37
  • I still get `confirmationLink not defined` – ItsNotAndy Jul 18 '22 at 07:42
  • Sorry, I can't see the problem - is the link ok to visit? Try pasting it into a browser window. – Fody Jul 18 '22 at 07:48
  • Yeah, the link works when visiting through the browser – ItsNotAndy Jul 18 '22 at 08:00
  • It may be that the Cypress runner is getting reset completely when using `cy.visit()`. Normally links are tested with `cy.request(confirmationLink).then(() => { // check response here })`. What other checks do you want to do after the visit? – Fody Jul 18 '22 at 08:03
  • If it is resetting, you would see the page start to load, then clear and a 2nd attempt is made. – Fody Jul 18 '22 at 08:05
  • You can try visiting inside `cy.origin()` wrapper. – Fody Jul 18 '22 at 08:08
  • By the way, you need to set configuration `experimentalSessionAndOrigin` flag to `true`. I also added an extra line that was needed in another question (in case you need it too). – Fody Jul 18 '22 at 09:06
  • I ended up just making it all one command for now. I will revisit this in a few days and let you know if this worked. Adding `experimentalSessionAndOrigin` started causing issues with retrieving the link from the email for some reason. – ItsNotAndy Jul 18 '22 at 13:32
1

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'))
  })
TesterDick
  • 3,830
  • 5
  • 18
  • When trying to save it as an env variable im getting `cy.visit() must be called with a url or an options object containing a url as its 1st argument ` I still see in the test runner that it does find the link I want it to redirect to. It then freezes up and fails with that error. – ItsNotAndy Jul 18 '22 at 07:42
0

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'))
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • When trying to save it as an env variable im getting `cy.visit() must be called with a url or an options object containing a url as its 1st argument` I still see in the test runner that it does find the link I want it to redirect to. It then freezes up and fails with that error. – ItsNotAndy Jul 18 '22 at 07:41
  • can you log this `cy.log(email.text.links[1].href)` and check what you get ? – Alapan Das Jul 18 '22 at 07:55
  • When I `cy.log` I get the link I am wanting to redirect to and I checked that the link does work when pasting it in my browser. – ItsNotAndy Jul 18 '22 at 07:59
  • If you are getting a link(with https ike `https://www.google.com`) then the above should work. Add `trim()` just to make sure there aren't any empty spaces, `email.text.links[1].href.trim()`. – Alapan Das Jul 18 '22 at 08:02
0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 19:50