What you'll find in this guide
- How to test various aspects of SMS
- Testing links and codes
- Managing SMS tests
What you'll need
- An understanding of Selenium and how it works.
- A Mailosaur account with the SMS feature enabled. You can start a free trial if you don't have one.
- A Selenium project with Mailosaur's API client configured. See our short guide on this.
Basic usage
To perform SMS testing with Selenium, all you need to do is:
- Send an SMS message to your dedicated Mailosaur phone number.
- Connect to the Mailosaur API with the official client library.
- Search for the message you sent in step 1.
- Perform assertions in the same way you would for any other test.
To learn about Server IDs, and what to replace SERVER_ID with, see sending test emails to Mailosaur.
// Setup the API client
MailosaurClient mailosaur = new MailosaurClient("API_KEY");
MessageSearchParams params = new MessageSearchParams();
params.withServer("SERVER_ID");
SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.withSentTo("123456789"); // Phone number
// Search for the message
Message message = mailosaur.messages().get(params, searchCriteria);
// Perform test assertions
assertEquals("Support", message.from().get(0).name());
assertEquals("654321", message.from().get(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
Search 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 will return the full message result.
MessageSearchParams params = new MessageSearchParams();
params.withServer("SERVER_ID");
SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.withSentTo("123456789"); // Phone number
Message message = mailosaur.messages().get(params, searchCriteria);
System.out.println(message.text().body());
Other ways to fetch SMS messages
It is usually better to use
messages.get()
The
list
andsearch
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 themessages.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 someone@SERVER_ID.mailosaur.net.
// Limit results to the first 10 matches only.
MessageSearchParams params = new MessageSearchParams();
params.withServer("SERVER_ID")
.withPage(0)
.withItemsPerPage(10);
MessageListResult result = mailosaur.messages().search(params, criteria);
// Get the most recent message (the first one in the list)
MessageSummary latestMessage = result.items().get(0);
// Get the full message object
Message message = mailosaur.messages().getById(latestMessage.id());
System.out.println(message.text().body());
List current inbox contents
See a full list of everything currently in your inbox.
// List the most recent messages
MessageListParams params = new MessageListParams();
params.withServer("SERVER_ID");
MessageListResult result = mailosaur.messages().list(params);
// Get the most recent message (the first one in the list)
MessageSummary latestMessage = result.items().get(0);
// Get the full message object
Message message = mailosaur.messages().getById(latestMessage.id());
System.out.println(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());
Testing carbon-copy recipients
// Carbon copy (CC) recipients
assertEquals("Jane Smith", message.cc().get(0).name());
assertEquals("1234567890", message.cc().get(0).phone());
// Blind carbon copy (BCC) recipients
assertEquals("Jill Smith", message.bcc().get(0).name());
assertEquals("1325476980", message.bcc().get(0).phone());
Testing SMS contents
The content of an SMS message is available via the text.body
property:
System.out.println(message.text().body()); // "Hi Jason, ..."
Testing links
Any links that are found in the content of your email are automatically available via the text.links
array:
// How many links?
System.out.println(message.text().links().size()); // 2
Link firstLink = message.text().links().get(0);
assertEquals("Google Search", firstLink.text());
assertEquals("https://www.google.com/", firstLink.href());
Testing verification codes
Codes are automatically extracted from the content of your SMS message. They are then made available via the text.codes
array:
Code otp = message.text().codes().get(0);
assertEquals("456812", otp.value());
Replying to an SMS message
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:
MessageReplyOptions options = new MessageReplyOptions();
options.withText("FYI");
mailosaur.messages().reply("MESSAGE_ID", options);
Parameter | Description |
---|---|
text | 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:
MessageForwardOptions options = new MessageForwardOptions();
options.withTo("verified-address@example.com")
.withText("FYI");
mailosaur.messages().forward("MESSAGE_ID", options);
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:
mailosaur.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:
mailosaur.messages().deleteAll("SERVER_ID");