TestRaptor
TestRaptor

Reputation: 1315

Cypress - Visit a URL obtained from a 3rd party(email)

In my test, I want to visit a URL that is sent in an email. I can successfully save the URL in a variable, but am unable to get cypress to go to that URL. I keep getting the below error. I don't fully understand cypress being "promise-like", so I'm not sure how to resolve the issue

Uncaught (in promise) CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

cy.get()

The cy command you invoked inside the promise was:

cy.request()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

Cypress.Commands.add("clickForgotPwdLink", (emailaddress) => {
    const MailosaurClient = require('mailosaur');
    const client = new MailosaurClient('1234');
    let latestEmail;
    var emailLower = emailaddress.toLowerCase();
    client.messages.search('abc', {
        sentTo: emailLower
    }).then((results) => {
        latestEmail = results.items[0];
        let email = latestEmail.id;
        client.messages.get(email)
            .then((newlink) => {
                var forgotpwd = newlink.html.links[0].href;
                console.log(forgotpwd)
                cy.request(''+forgotpwd+'');
            })
    })
});

Upvotes: 2

Views: 1015

Answers (3)

Juampi Caceres
Juampi Caceres

Reputation: 316

I have to face this today. If you are getting a raw body response (string) you have to get the link in this way:

            const links = body.split("href=http://my.url.com");
            var activation_link = links[0].match(/\bhttp?:\/\/\S+/);
            activation_link= activation_link[0].replace('"','')
            cy.visit(activation_link)

Upvotes: 1

sergiomap
sergiomap

Reputation: 11

I also tried like you but Cypress doens't allowed. You have to do it via cy.request() to Mailosaur API.

Try something like this:

cy.request({
  method: 'POST',
  url: 'https://mailosaur.com/api/messages/search',
  auth: {
    username: 'API_key',
  },
  body: {
    sentTo: 'yourEmail',
  },
  qs: {
    server: 'yourServer',
    page: 0,
    itemsPerPage: 50,
  },
});

Upvotes: 0

Mr. J.
Mr. J.

Reputation: 3741

Isn't it just as simple as removing all the quotes and plusses from the cy.request? That is how we use a stored url and that works. But that stored url doesn't come from an email but from an element on the page

Upvotes: 0

Related Questions