What you’ll need
- You’ll need a Mailosaur account. If you don’t already have one, just create a free trial account first.
- Log in to your account and generate an API key, which you will need to run automated tests.
- Finally, make sure you know how to send emails (or SMS) into your account.
Create a Cucumber starter project
Create a new project for your programming language, then run the following commands to install Cucumber and any testing libraries you need.
Create and initialize a Node.js project
mkdir mailosaur-nodejs-cucumber
cd mailosaur-nodejs-cucumber
npm init -y
Install Cucumber
Use npm to install Cucumber. There's no need to also install an assertion library, as you can just use Node.js's built-in Assert module.
npm install --save-dev @cucumber/cucumber
Integrate Mailosaur with an existing Cucumber project
If you have an existing Cucumber project (or created one just now), here is how to install and configure Mailosaur's API client library:
Install the Mailosaur Node.js library
Use npm to install the Mailosaur Node.js library.
npm install --save-dev mailosaur
Write your first Cucumber test
Cucumber tests are BDD (behavior-driven development) tests and have two parts:
- Feature files: Cucumber groups its tests by feature, and a feature file consists of lists of scenarios made up of steps, all written in human-like Gherkin syntax in the form of Given, When, and Then statements.
- Step definition files: These contain the code that implements each step from the feature files, including the test setup, actions, and assertions.
Your first test is to check that the Mailosaur library is able to connect to the Mailosaur API and see at least one inbox. This includes:
- Writing a Gherkin scenario in a feature file using Given, When, and Then steps, such as:
- Retrieving the Mailosaur API key
- Connecting to the Mailosaur API using the Mailosaur library
- Confirming that at least one inbox is present
Create the following scenario in a Quickstart.feature
file:
Feature: Quick start testing with Mailosaur
Scenario: Check the Mailosaur API works by checking we can access an email inbox
Given the Mailosaur API client is setup
When I connect to the Mailosaur API
Then I should see at least one inbox
To implement the scenario, define the step definitions for each Cucumber step.
let apiKey;
let mailosaurClient;
let emailServers;
Given('the Mailosaur API client is setup', function () {
apiKey = process.env.MAILOSAUR_API_KEY;
assert.ok(apiKey, 'MAILOSAUR_API_KEY environment variable must be set');
});
When('I connect to the Mailosaur API', async function () {
mailosaurClient = new MailosaurClient(apiKey);
// Make a simple API call to get a list of email servers
const result = await mailosaurClient.servers.list();
emailServers = result.items;
});
Then('I should see at least one inbox', function () {
assert.ok(emailServers, 'Expected emailServers to be defined');
assert.ok(emailServers.length > 0, 'Expected at least one inbox/server');
});
Start testing
Now that you have a project connected to the Mailosaur API, you can start creating all kinds of tests:
- Email testing with Cucumber
- SMS testing with Cucumber
- Email testing — test password resets, account verification, order confirmations, etc.
- SMS testing — test alerts, notifications, and 2FA logins
- 2FA app testing — mimic the functionality of apps like Google Authenticator and OAuth
- Email deliverability testing