Library Reference

The Mailosaur Cypress library lets you integrate email and SMS testing into your continuous integration process.

This guide provides details on all the methods available within the library.

To check out some common use cases, take a look at getting started with Cypress.

Getting started

Install the Mailosaur Cypress library

Install the package via npm:

cd your/project/path
npm install cypress-mailosaur --save-dev

Register the commands by appending an import statement to the cypress/support/commands.js file, within your project folder:

echo "require('cypress-mailosaur');" >> cypress/support/commands.js

Your API key and Server IDs

In the examples below, just replace YOUR_API_KEY with the your account API key. To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Messages

Messages is the collective name given to the email and/or SMS messages that are sent into Mailosaur for testing. The message object contains everything you need to perform in-depth automated testing.

cy.mailosaurGetMessage(server, criteria, options)

Waits for a message to be found. Returns as soon as a message matching the specified search criteria is found.

Recommended: This is the most efficient method of looking up a message, therefore we recommend using it whereever possible.

cy.mailosaurGetMessage('SERVER_ID', {
  sentTo: 'someone.SERVER_ID@mailosaur.io'
}).then((message) => {
  cy.log('Message subject:', message.subject);
});

To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Criteria

  • sentFrom - The full email address from which the target message was sent.
  • sentTo - The full email address to which the target message was sent.
  • subject - The subject line of the target email.
  • body - Search for part of the message body.
  • match - If set to ALL (default), then only results that match all specified criteria will be returned. If set to ANY, results that match any of the specified criteria will be returned.

Options

  • timeout - Specify how long to wait for a matching result (in milliseconds, default value is 10 seconds).
  • receivedAfter - Limits results to only messages received after this date/time (default 20 seconds ago).
cy.mailosaurGetMessage('SERVER_ID', {
  sentTo: 'someone.SERVER_ID@mailosaur.io'
}, {
  // Search all messages received after midnight 5th Jan 2020
  receivedAfter: new Date(2020, 1, 5)
});

cy.mailosaurListMessages(server, options)

Returns a list of your messages in summary form. The summaries are returned sorted by received date, with the most recently-received messages appearing first.

Message summaries The method returns a message summary, rather than the full message object. This means that several properties, like the message body, are not included. To get this data, you’ll need to call mailosaurGetMessageById. Alternatively, we recommend using the mailosaurGetMessage method, which is a far more efficient approach.
// List the most recent messages
cy.mailosaurListMessages('SERVER_ID').then((result) => {
  // Get the most recent message (the first one in the list)
  const message = result.items[0];

  cy.log('Subject:', message.subject);
});

Options

  • receivedAfter - Allows you to customise how far back to look for messages.
  • page - Used alongside itemsPerPage to paginate through results. This is zero-based, meaning 0 is the first page of results.
  • itemsPerPage - A limit on the number of results to be returned. This can be set between 1 to 1000, with the default being 50.
// List all results received after midnight on 3rd January 2020
cy.mailosaurListMessages('SERVER_ID', {
  page: 0,
  itemsPerPage: 10,
  receivedAfter: new Date(2020, 1, 3)
});

cy.mailosaurSearchMessages(server, criteria, options)

Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

Message summaries The method returns a message summary, rather than the full message object. This means that several properties, like the message body, are not included. To get this data, you’ll need to call mailosaurGetMessageById. Alternatively, we recommend using the mailosaurGetMessage method, which is a far more efficient approach.
cy.mailosaurSearchMessages('SERVER_ID', {
  sentTo: 'someone.SERVER_ID@mailosaur.io'
}).then((result) => {
  // Get the most recent match
  const message = result.items[0];

  cy.log('Subject:', message.subject);
});

Criteria

  • sentFrom - The full email address from which the target message was sent.
  • sentTo - The full email address to which the target message was sent.
  • subject - The subject line of the target email.
  • body - Search for part of the message body.
  • match - If set to ALL (default), then only results that match all specified criteria will be returned. If set to ANY, results that match any of the specified criteria will be returned.

Options

  • timeout - If provided, determines how long to wait for a matching result (provided in milliseconds).
  • errorOnTimeout - When set to false, an error will not be throw if timeout is reached (default: true).
  • receivedAfter - Allows you to customise how far back to look for messages.
  • page - Used alongside itemsPerPage to paginate through results. This is zero-based, meaning 0 is the first page of results.
  • itemsPerPage - A limit on the number of results to be returned. This can be set between 1 to 1000, with the default being 50.
// Search for all messages sent to someone.SERVER_ID@mailosaur.io,
// received after midnight on 3rd Jan 2020. Limit results to
// the first 10 matches only.
cy.mailosaurSearchMessages('SERVER_ID', {
  sentTo: 'someone.SERVER_ID@mailosaur.io'
}, {
  page: 0,
  itemsPerPage: 10,
  receivedAfter: new Date(2020, 1, 3)
});

cy.mailosaurGetMessagesBySubject(server, subject)

Shorthand for a mailosaurSearchMessages query on message subject. Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

cy.mailosaurGetMessagesBySubject('SERVER_ID', 'Password reset');

cy.mailosaurGetMessagesBySentFrom(server, emailAddress)

Shorthand for a mailosaurSearchMessages query on message sender. Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

cy.mailosaurGetMessagesBySentFrom('SERVER_ID', 'someone.SERVER_ID@mailosaur.io');

cy.mailosaurGetMessagesBySentTo(server, emailAddress)

Shorthand for a mailosaurSearchMessages query on message recipient. Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

cy.mailosaurGetMessagesBySentTo('SERVER_ID', 'someone.SERVER_ID@mailosaur.io');

cy.mailosaurGetMessagesByBody(server, text)

Shorthand for a mailosaurSearchMessages query on message body. Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

cy.mailosaurGetMessagesBySubject('SERVER_ID', 'Hey there');

cy.mailosaurGetMessageById(id)

Retrieves the detail for a single email message. Must be used in conjunction with either mailosaurListMessages or mailosaurSearchMessages in order to get the unique identifier for the required message. We always recommend using the mailosaurGetMessage method instead.

cy.mailosaurListMessages('SERVER_ID').then((result) => {
  const messageId = result.items[0].id;

  cy.mailosaurGetMessageById(messageId).then((message) => {
    cy.log('Subject:', message.subject);
  });
});

cy.mailosaurDeleteMessage(id)

Permanently deletes a message. Also deletes any attachments related to the message. This operation cannot be undone.

cy.mailosaurDeleteMessage(messageId)

cy.mailosaurDeleteAllMessages(server)

Permanently deletes all messages held by the specified server. Also deletes any attachments related to each message. This operation cannot be undone.

cy.mailosaurDeleteAllMessages('SERVER_ID')

Servers

Servers contain all the configuration and set up for.

cy.mailosaurListServers()

Returns a list of your virtual servers. Servers are returned sorted in alphabetical order.

cy.mailosaurListServers().then((result) => {
  cy.log('You have a server called:', result.items[0].name);
});

cy.mailosaurCreateServer(server)

Creates a new virtual server. Only the name property is required to create a new server via the API.

cy.mailosaurCreateServer({
  name: 'My email tests'
});

cy.mailosaurGetServer(id)

Retrieves the detail for a single server. Simply supply the unique identifier for the required server.

cy.mailosaurGetServer('SERVER_ID').then((server) => {
  cy.log(server.name);
});

cy.mailosaurUpdateServer(server)

Updates a single server.

retrievedServer.name = 'Updated server name';

cy.mailosaurUpdateServer(retrievedServer);

cy.mailosaurDeleteServer(id)

Permanently deletes a server. Also deletes all messages and associated attachments within the server. This operation cannot be undone.

cy.mailosaurDeleteServer('SERVER_ID');

cy.mailosaurGenerateEmailAddress(id)

Utility method to help you generate a random email address for a given server.

cy.mailosaurGenerateEmailAddress('SERVER_ID').then((emailAddress) => {
  cy.log(emailAddress); // "bgwqj@SERVER_ID.mailosaur.io"
});

To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Files

cy.mailosaurDownloadMessage(messageId)

Downloads an EML file representing the specified email. Simply supply the unique identifier for the required email.

cy.mailosaurDownloadAttachment(attachmentId)

Downloads a single attachment. Simply supply the unique identifier for the required attachment.

Analysis

cy.mailosaurGetSpamAnalysis(messageId)

Perform spam testing on the specified email.