Your Mailosaur account's API key and Server ID. To learn about Server IDs and what to replace “SERVER_ID” with, see our guide on sending test emails to Mailosaur.
Basic usage
To perform SMS testing with Cucumber, all you need to do is:
1 - Send an SMS message to your dedicated Mailosaur phone number. (You can use your own mobile device to do this.) 2 - Write a Gherkin scenario using Given, When, and Then steps, such as:
Connect to the Mailosaur API with the official client library.
Search for the SMS you sent in step 1.
Verify that the sender's phone number is correct.
3 - Implement each Given, When and Then step in code, including assertions.
Cucumber tests are BDD (behavior-driven development) tests and have two parts:
Feature files: These define testing scenarios with Given, When, and Then steps to define the journey being tested.
Step definition files: These contain the code that implements each step from the feature files, including the test setup, actions, and assertions.
Create a scenario in a feature file, replacing "123456789" with the number of the SMS mailbox you messaged earlier and replacing "123456787" with your own phone number. Phone numbers should be entered in E.164 format without the plus sign:
Scenario: Basic SMS usage
Given the Mailosaur API client is setup for SMS
When I search for an SMS sent to "123456789"
Then that SMS should be sent from "123456787"
Next, implement these steps in a step definition file:
Given('the Mailosaur API client is setup for SMS', function () {
const apiKey = process.env.MAILOSAUR_API_KEY;
serverId = process.env.MAILOSAUR_SERVER_ID;
assert.ok(apiKey, 'MAILOSAUR_API_KEY must be set in .env');
assert.ok(serverId, 'MAILOSAUR_SERVER_ID must be set in .env');
assert.notStrictEqual(apiKey, '', 'MAILOSAUR_API_KEY must not be empty');
assert.notStrictEqual(serverId, '', 'MAILOSAUR_SERVER_ID must not be empty');
mailosaurClient = newMailosaurClient(apiKey);
});
When('I search for an SMS sent to {string}', asyncfunction (toNumber) {
const searchCriteria = {
sentTo: toNumber
};
message = await mailosaurClient.messages.get(serverId, searchCriteria);
});
Then('that SMS should be sent from {string}', function (fromNumber) {
assert.strictEqual(message.from[0].phone, fromNumber, 'Sender phone number does not match');
});
@Given("the Mailosaur API client is setup for SMS")publicvoidthe_mailosaur_api_client_is_setup() {
Dotenvdotenv= Dotenv.load();
StringapiKey= dotenv.get("MAILOSAUR_API_KEY");
serverId = dotenv.get("MAILOSAUR_SERVER_ID");
assertNotNull("MAILOSAUR_API_KEY must be set", apiKey);
assertNotNull("MAILOSAUR_SERVER_ID must be set", serverId);
assertFalse("MAILOSAUR_API_KEY must not be empty", apiKey.isEmpty());
assertFalse("MAILOSAUR_SERVER_ID must not be empty", serverId.isEmpty());
mailosaurClient = newMailosaurClient(apiKey);
assertNotNull("Client should be initialized", client);
}
@When("I search for an SMS sent to {string}")publicvoidi_search_for_an_sms_sent_to(String phoneNumber)throws Exception {
// Setup search criteriaSearchCriteriasearchCriteria=newSearchCriteria();
searchCriteria.withSentTo(phoneNumber);
// Search for the message
message = mailosaurClient.messages().get(serverId, searchCriteria);
}
@Then("that SMS should be sent from {string}")publicvoidthat_sms_should_be_sent_from(String phoneNumber) {
assertNotNull("Expected to find a message", message);
assertNotNull("Message sender should not be null", message.from());
assertEquals(phoneNumber, message.from().get(0).phone());
}
Given("the Mailosaur API client is setup for SMS") do
api_key = ENV['MAILOSAUR_API_KEY']
@server_id = ENV['MAILOSAUR_SERVER_ID']
expect(api_key).not_to be_nil, 'MAILOSAUR_API_KEY must be set'
expect(api_key.strip).not_to be_empty, 'MAILOSAUR_API_KEY must not be empty'
expect(@server_id).not_to be_nil, 'MAILOSAUR_SERVER_ID must be set'
expect(@server_id.strip).not_to be_empty, 'MAILOSAUR_SERVER_ID must not be empty'@mailosaurClient = Mailosaur::MailosaurClient.new(api_key)
expect(@client).not_to be_nil
endWhen("I search for an SMS sent to {string}") do |phone_number|
criteria = Mailosaur::Models::SearchCriteria.new
criteria.sent_to = phone_number
@message = @client.messages.get(@server_id, criteria)
endThen("that SMS should be sent from {string}") do |expected_number|
expect(@message).not_to be_nil
expect(@message.from.first.phone).to eq(expected_number)
end
@given('the Mailosaur API client is setup for SMS')defstep_impl(context):
global apiKey
global serverId
global mailosaurClient
apiKey = os.getenv('MAILOSAUR_API_KEY')
serverId = os.getenv('MAILOSAUR_SERVER_ID')
assert apiKey, 'MAILOSAUR_API_KEY environment variable must be set'assert serverId, 'MAILOSAUR_SERVER_ID environment variable must be set'
mailosaurClient = MailosaurClient(apiKey)
@when('I search for an SMS sent to "{toNumber}"')defstep_impl(context, toNumber):
global message
criteria = SearchCriteria()
criteria.sent_to = toNumber
message = mailosaurClient.messages.get(serverId, criteria)
assert message, 'Message not found'@then('that SMS should be sent from "{fromNumber}"')defstep_impl(context, fromNumber):
assert message.sender[0].phone == fromNumber, 'Sender phone number does not match'
[Given("the Mailosaur API client is setup for SMS")]
publicvoidGivenTheMailosaurApiClientIsSetup()
{
var apiKey = _config["appSettings:MAILOSAUR_API_KEY"];
serverId = _config["appSettings:MAILOSAUR_SERVER_ID"];
Assert.NotNull(apiKey);
Assert.NotNull(serverId);
Assert.NotEmpty(apiKey);
Assert.NotEmpty(serverId);
mailosaurClient = new MailosaurClient(apiKey);
}
[When("I search for an SMS sent to {string}")]
publicvoidWhenISearchForAnSmsSentTo(string toNumber)
{
// Setup search criteriavar searchCriteria = new SearchCriteria()
{
SentTo = toNumber
};
// Search for the message
message = mailosaurClient!.Messages.Get(serverId, searchCriteria);
}
[Then("that SMS should be sent from {string}")]
publicvoidThenThatSmsShouldBeSentFrom(string fromNumber)
{
Assert.Equal(fromNumber, message.From[0].Phone);
}
This example will search for the phone number that a message was sent to, but you can also search using any of the below criteria:
Parameter
Description
sentTo
The full phone number to which the target message was sent
sentFrom
The full phone number from which the target message was sent
body
Finds messages where the message body contains this text
Find an SMS message
When searching for a specific message within your inbox, it’s always better to use messages.get(), as it will automatically wait for messages to arrive and return the full message result.
// Search for the messageconst message = await mailosaurClient.messages.get("SERVER_ID", {
sentTo: "123456789", // Phone number
});
console.log(message.text.body);
var criteria = new SearchCriteria() {
SentTo = "123456789"// Phone number
};
var message = mailosaurClient.Messages.Get("SERVER_ID", criteria);
Console.WriteLine(message.Text.Body);
Time range for searching
By default, searches only look for messages received in the last hour. To look back further in your message history, set the “received after” parameter.
// Calculate a datetime for yesterdayconst yesterday = newDate(Date.now() - 24 * 60 * 60 * 1000);
// Use this timestamp in the searchconst message = await mailosaurClient.messages.get("SERVER_ID", {
sentTo: "123456789",
}, {
receivedAfter: yesterday
});
// Calculate a timestamp for yesterdayCalendarcalendar= Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -1);
longyesterday= calendar.getTimeInMillis();
// Use this timestamp in the searchMessageSearchParamsparams=newMessageSearchParams();
params.withServer("SERVER_ID");
params.withReceivedAfter(yesterday);
SearchCriteriasearchCriteria=newSearchCriteria();
searchCriteria.withSentTo("123456789");
Messagemessage= mailosaurClient.messages().get(params, searchCriteria);
# Calculate a datetime for yesterday
yesterday = Date.today - 1
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "123456789"# Use this timestamp in the search
message = mailosaurClient.messages.get("SERVER_ID", criteria, received_after: yesterday)
# Calculate a datetime for yesterday
yesterday = datetime.today() - timedelta(days=1)
criteria = SearchCriteria()
criteria.sent_to = "123456789"# Use this timestamp in the search
message = mailosaurClient.messages.get("SERVER_ID", criteria, received_after=yesterday)
// Calculate a datetime for yesterdayvar yesterday = DateTime.Today.AddDays(-1);
var criteria = new SearchCriteria() {
SentTo = "123456789"
};
// Use this timestamp in the searchvar message = mailosaurClient.Messages.Get("SERVER_ID", criteria, receivedAfter: yesterday);
“No matching messages” troubleshooting
If you see the error “No matching messages found in time” when searching for messages:
Ensure that the target email or SMS message is visible in the Mailosaur Dashboard, as it may not have arrived at all.
Check when the message arrived. By default, searches only include messages received within the last hour. See “Time range for searching” above to learn how to override this.
Check that you have correctly set the sent to parameter in your search criteria.
It is usually better to use messages.get()
The list and search methods will only return basic summaries, meaning several properties (including the message body) are not included. To get this data, you need to use .getById(). Instead, we recommend using the messages.get() as this automatically waits for a matching result and returns the message in the result.
##Search for multiple messages
Identify if multiple messages meet the same criteria.
// Search for all messages sent to 123456789.// Limit results to the first 10 matches only.const result = await mailosaurClient.messages.search(
"SERVER_ID",
{
sentTo: "123456789",
},
{
page: 0,
itemsPerPage: 10,
}
);
// Get the most recent message (the first one in the list)const latestMessage = result.items[0];
// Get the full message objectconst message = await mailosaurClient.messages.getById(latestMessage.id);
console.log(message.text.body);
// Search for all messages sent to 123456789.SearchCriteriacriteria=newSearchCriteria();
criteria.withSentTo("123456789");
// Limit results to the first 10 matches only.MessageSearchParamsparams=newMessageSearchParams();
params.withServer("SERVER_ID")
.withPage(0)
.withItemsPerPage(10);
MessageListResultresult= mailosaurClient.messages().search(params, criteria);
// Get the most recent message (the first one in the list)MessageSummarylatestMessage= result.items().get(0);
// Get the full message objectMessagemessage= mailosaurClient.messages().getById(latestMessage.id());
System.out.println(message.text().body());
# Search for all messages sent to 123456789.
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "123456789"# Limit results to the first 10 matches only.
result = mailosaurClient.messages.search("SERVER_ID", criteria, page:0, items_per_page:10)
# Get the most recent message (the first one in the list)
latest_message = result.items[0]
# Get the full message object
message = mailosaurClient.messages.get_by_id(latest_message.id)
puts(message.text.body)
# Search for all messages sent to 123456789.
criteria = SearchCriteria()
criteria.sent_to = "123456789"# Limit results to the first 10 matches only.
result = mailosaurClient.messages.search("SERVER_ID", criteria, page=0, items_per_page=10)
# Get the most recent message (the first one in the list)
latest_message = result.items[0]
# Get the full message object
message = mailosaurClient.messages.get_by_id(latest_message.id)
print(message.text.body)
// Search for all messages sent to 123456789.var criteria = new SearchCriteria() {
SentTo = "123456789"
};
// Limit results to the first 10 matches only.var result = mailosaurClient.Messages.Search("SERVER_ID", criteria, page: 0, itemsPerPage: 10);
// Get the most recent message (the first one in the list)var latestMessage = result.Items[0];
// Get the full message objectvar message = mailosaurClient.Messages.GetById(latestMessage.Id);
Console.WriteLine(message.Text.Body);
List current inbox contents
See a full list of everything currently in your inbox.
// List the most recent messagesconst result = await mailosaurClient.messages.list("SERVER_ID");
// Get the most recent message (the first one in the list)const latestMessage = result.items[0];
// Get the full message objectconst message = await mailosaurClient.messages.getById(latestMessage.id);
console.log(message.text.body);
// List the most recent messagesMessageListParamsparams=newMessageListParams();
params.withServer("SERVER_ID");
MessageListResultresult= mailosaurClient.messages().list(params);
// Get the most recent message (the first one in the list)MessageSummarylatestMessage= result.items().get(0);
// Get the full message objectMessagemessage= mailosaurClient.messages().getById(latestMessage.id());
System.out.println(message.text().body());
# List the most recent messages
result = mailosaurClient.messages.list("SERVER_ID")
# Get the most recent message (the first one in the list)
latest_message = result.items[0]
# Get the full message object
message = mailosaurClient.messages.get_by_id(latest_message.id)
puts(message.text.body)
# List the most recent messages
result = mailosaurClient.messages.list("SERVER_ID")
# Get the most recent message (the first one in the list)
latest_message = result.items[0]
# Get the full message object
message = mailosaurClient.messages.get_by_id(latest_message.id)
print(message.text.body)
// List the most recent messagesvar result = mailosaurClient.Messages.List("SERVER_ID");
// Get the most recent message (the first one in the list)var latestMessage = result.Items[0];
// Get the full message objectvar message = mailosaurClient.Messages.GetById(latestMessage.Id);
Console.WriteLine(message.Text.Body);
Common test scenarios
Testing basic properties
Once you have fetched an SMS, you can simply test the properties of that text:
// Test sender information
assertEquals("Support", message.from().get(0).name());
assertEquals("654321", message.from().get(0).phone());
// Test recipient information
assertEquals("John Smith", message.to().get(0).name());
assertEquals("1234567890", message.to().get(0).phone());
# Test sender informationself.assertEqual("Support", message.sender[0].name)
self.assertEqual("654321", message.sender[0].phone)
# Test recipient informationself.assertEqual("John Smith", message.to[0].name)
self.assertEqual("1234567890", message.to[0].phone)
// Test sender information
Assert.Equal("Support", message.From[0].Name);
Assert.Equal("654321", message.From[0].Phone);
// Test recipient information
Assert.Equal("John Smith", message.To[0].Name);
Assert.Equal("1234567890", message.To[0].Phone);
# Test sender information
assert_equal("Support", message.from[0].name)
assert_equal("654321", message.from[0].phone)
# Test recipient information
assert_equal("John Smith", message.to[0].name)
assert_equal("1234567890", message.to[0].phone)
// Test sender informationexpect(message.from[0].name).toBe("Support");
expect(message.from[0].phone).toBe("654321");
// Test recipient informationexpect(message.from[0].name).toBe("John Smith");
expect(message.from[0].phone).toBe("1234567890");
Testing SMS contents
The content of an SMS message is available via the text.body property:
If your product is capable of handling SMS replies from your customers, you can use Mailosaur’s reply feature to simulate this. When you reply, the SMS is sent back to the phone number it was originally sent to Mailosaur from:
Any additional text content to include in the reply
html
Any additional HTML content to include in the reply
subject
Optionally override the default subject line
attachments
Optional attachments (see 'include attachments' above)
Forwarding a message to email
You can forward messages from your Mailosaur account to external email addresses either one-by-one, or via the creation of automated forwarding rules. Before you can forward messages, you must set up a verified external email address, so you can send email to it:
await mailosaur.messages.forward("MESSAGE_ID", {
to: "verified-address@example.com",
text: "FYI",
});
Parameter
Description
to
The email address to which the message will be sent. Must be a verified email address
text
Any additional text content to forward the message with
html
Any additional HTML content to forward the message with
subject
Optionally override the default subject line
Deleting messages
Deleting an individual message
Permanently deletes a single message. Also deletes any attachments related to the message. This operation cannot be undone:
await mailosaurClient.messages.del("MESSAGE_ID");
mailosaurClient.messages().delete("MESSAGE_ID");
mailosaurClient.messages.delete("MESSAGE_ID")
mailosaurClient.messages.delete("MESSAGE_ID")
mailosaurClient.Messages.Delete("MESSAGE_ID");
Delete all messages
Permanently deletes all messages held in the specified server/inbox. Also deletes any attachments related to each message. This operation cannot be undone: