Examples

Here is an actual end-to-end test that we run against our own site's login process. The aim is to sign up a user — which requires the user to receive a verification code via email. This code must then be entered on the account verification page. Without MailSlurp this would be difficult to test.

Our test uses the MailSlurp REST API to:

  • Create a random inbox with an id and email address.
  • Use these details to sign up as a new user.
  • Check the emails in the inbox for a verification code.
  • Extract the code and use it to verify a user.

It is written in Javascript using the Cypress test framework and the official MailSlurp API client.

/**
 * A real test of the MailSlurp sign up process
 * as a demonstration of why and how you would use
 * the MailSlurp API. It's a JS file that is run by
 * Cypress, but the MailSlurp API client used works
 * anywhere. The client is asynchronous and uses promises.
 */

// import an apiKey (which you can find in your dashboard)
// and the official js client (available on npm)
const apiKey = require("./config.json").apiKey;
const MailSlurpClient = require("mailslurp-client");

// instantiate a client for the inbox endpoints
const api = new MailSlurpClient.InboxcontrollerApi();

// test the authentication flow on mailslurp.com
// using Cypress test runenr
describe(`The real MailSlurp.com user 
          signup process, which sends a verification
          code via email that the user must then enter`, () => {
  // placeholders we will set these later using MailSlurp
  let username;
  let emailAddress;

  const password = "test-password";

  before(
    `each test, create a new, random email
     inbox and use the address and id to 
     sign-up on Mailslurp.com`,
    done => {
      // create a new email inbox that returns an id and an address
      api
        .createRandomInboxUsingPOST(apiKey)
        .then(
          data => data.payload,
          err => {
            // log or inspect an error here if necessary
            throw err;
          }
        )
        .then(({ id, address }) => {
          username = id;
          emailAddress = address;
          expect(username).to.eq(id);
          done();
        });
    }
  );

  it(`allows signup with a generated inbox 
      and username that we got from MailSlurp`, () => {
    cy.visit("/sign-up");
    // use the username and email address we got
    // from MailSlurp to sign a user up by filling out
    // the sign-up form
    cy.get("#username").type(username);
    cy.get("#email").type(emailAddress);
    cy.get("#password").type(password);
    cy.get("#passwordConfirmation").type(password);
    cy.get("button[type='submit']").click();
  });

  it(`Mailslurp.com then sends an email to the new user's
      email address (using some transactional email service).
      In our test we'll use the MailSlurp API to wait for and return
      the email, from which we can read the verification code
      and enter it into the verification form`, () => {
    let verificationCode;
    cy.location("pathname").should("eq", `/verify/${username}`);
    // use cypress's async api
    cy.then(() => {
      // check the users inbox and hold the
      // connection until one email is present
      return api
        .getEmailsForInboxUsingGET(apiKey, username, {
          minCount: 1,
          maxWait: 90
        })
        .then(
          data => data.payload,
          err => {
            throw err;
          }
        )
        .then(([latestEmail]) => {
          // regex match for the confirmation code
          // within the email body
          const r = /\s(\d{6})\./g;
          // extract the verication code
          verificationCode = r.exec(latestEmail.body)[1];
        });
    });
    // submit the code we extracted from the email
    cy.get("#code").then(input => {
      cy.wrap(input).type(verificationCode);
    });
    cy.get("button[type='submit']").click();
  });

  it(`successfully verifies the user 
      and allows the user to login`, () => {
    cy.location("pathname").should("eq", "/login");
    cy.get("#username").type(username);
    cy.get("#password").type(password);
    cy.get("button[type='submit']").click();
    cy.location("pathname").should("eq", "/dashboard");
  });
  
});

If you want to run this test you can use this config file.

{"chromeWebSecurity":false,"baseUrl":"https://www.mailslurp.com","defaultCommandTimeout":90000,"requestTimeout":90000}