https://cypress.io logo
Click on link sent by email to sign up into system...
# i-need-help
m
🧵 I'm registering users that needs to click on a link, sent by email, to access a system I test. I'm using MailSlurp (I'm following this tutorial: https://www.mailslurp.com/examples/cypress-js/) . Doing some modifications to the actual code, I have this:
example.cy.js
Copy code
javascript
describe('Sign up', () => {

  const password = "test-password";
  let inboxId;
  let emailAddress;
  let link;

  it('sample', () => {
    cy.visit('http://localhost:3000/');
    
    cy.get('h2').contains('Iniciar');

    cy.wait(5000);

    cy.get('a').contains('Don\'t').click(); cy.get('label[for="password"]').contains('Create a Password');

    cy.createInbox().then(inbox => {
      // verify a new inbox was created
      assert.isDefined(inbox)

      // save the inboxId for later checking the emails
      inboxId = inbox.id
      emailAddress = inbox.emailAddress;

      // sign up with inbox email address and the password
      cy.get('input[name="email"]').type(emailAddress);
      cy.get('input[name="password"]').type(password);
      cy.get('button[type="submit"]').contains('Sign up').click();

      cy.waitForLatestEmail(inboxId).then(email => {
        // verify we received an email
        assert.isDefined(email);
  
        console.log("print the body")
        console.log(email.body)
  
        // extract the link
        link = /<a\s+(?:[^>]*?\s+)?href=(["'])(.*?)\1/.exec(email.body)[2];
        console.log(link)
      });
    });
  });

  it('should works', () => {
    console.log('Print the URL: ' + link)
    cy.visit(link);
  });

it('should works', () => {
    // it displays the link sent through email, but when I visit it throws an error
    // see image attached
    console.log('Print the URL: ' + link)
    cy.visit(link);
  });
commands.js
Copy code
javascript
//<gen>cypress_client_add_command
const { MailSlurp } = require('mailslurp-client');
// set your api key with an environment variable `CYPRESS_API_KEY` or configure using `env` property in config file
// (cypress prefixes environment variables with CYPRESS)
const apiKey = Cypress.env('API_KEY')
const mailslurp = new MailSlurp({ apiKey });

Cypress.Commands.add("createInbox", () => {
  return mailslurp.createInbox();
});

Cypress.Commands.add("waitForLatestEmail", (inboxId) => {
  // how long we should hold connection waiting for an email to arrive
  const timeoutMillis = 30_000;
  return mailslurp.waitForLatestEmail(inboxId, timeoutMillis)
});
//</gen>
My first approach was to just save the link in a variable and then visit it, but when I do that I get this URL in the browser:
http://localhost:3000/#error=invalid_request&error_code=400&error_description=Verify+requires+a+verification+type
. I noticed that the only way to access the system through the link is by clicking it directly through the email (once it's clicked, the user can see the home page). I'll appreciate any help you can provide me to solve my issue (: