What you'll find in this guide
- How to test differing aspects of SMS
- Testing links and codes
- Managing SMS tests
What you'll need
- Knowledge of how Playwright works.
- A Mailosaur account with the SMS feature. Start a free trial if you don't already have one.
- A Playwright project with Mailosaur's API client configured.
Basic usage
To use Playwright and Mailosaur for SMS testing, you need to:
- Send an SMS to your dedicated Mailosaur phone number.
- Connect to the Mailosaur API with the official client library.
- Search for the message sent earlier.
- 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
const mailosaur = new MailosaurClient("API_KEY");
// Search for the message
const message = mailosaur.messages.get("SERVER_ID", {
sentTo: "123456789", // Phone number
});
// Perform test assertions
expect(message.from[0].name).toEqual("Support");
expect(message.from[0].phone).toEqual("654321");
This will search for the phone number a message was sent to, but you can also use 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
Find 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.
const message = mailosaur.messages.get("SERVER_ID", {
sentTo: "123456789",
});
console.log(message.text.body);
Other ways to fetch SMS messages
It is usually better to use
messages.get()
list
andsearch
methods only return basic summaries. To get the full data, such as the message body, you need to use.getById()
. It's generally easier to usemessages.get()
as this automatically waits for a matching result and returns full data.
Search for multiple messages
Spot multiple messages that meet the same criteria.
// Search for all messages sent to someone@SERVER_ID.mailosaur.net.
// Limit results to the first 10 matches only.
const result = mailosaur.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 object
const message = mailosaur.messages.getById(latestMessage.id);
console.log(message.text.body);
List current inbox contents
See a full list of everything currently in your inbox.
// List the most recent messages
const result = mailosaur.messages.list("SERVER_ID");
// Get the most recent message (the first one in the list)
const latestMessage = result.items[0];
// Get the full message object
const message = mailosaur.messages.getById(latestMessage.id);
console.log(message.text.body);
Common test scenarios
Testing basic properties
Once an SMS has been retrieved, test the properties of that text:
// Test sender information
expect(message.from[0].name).toEqual("Support");
expect(message.from[0].phone).toEqual("654321");
// Test recipient information
expect(message.to[0].name).toEqual("John Smith");
expect(message.to[0].phone).toEqual("1234567890");
Testing carbon-copy recipients
// Carbon copy (CC) recipients
expect(message.cc[0].name).toEqual("Jane Smith");
expect(message.cc[0].phone).toEqual("1234567890");
// Blind carbon copy (BCC) recipients
expect(message.bcc[0].name).toEqual("Jill Smith");
expect(message.bcc[0].phone).toEqual("1325476980");
Testing SMS contents
SMS message content is available via the text.body
property:
console.log(message.text.body); // "Hi Jason, ..."
Testing links
Any links in the content of your email are automatically available via the text.links
array:
// How many links?
console.log(message.text.links.length); // 2
const firstLink = message.text.links[0];
expect(firstLink.text).toEqual("Google Search");
expect(firstLink.href).toEqual("https://www.google.com/");
Testing verification codes
Codes are automatically extracted from the content of your SMS message. They are available via the text.codes
array:
const otp = message.text.codes[0];
expect(otp.value).toEqual("456812");
Replying to an SMS message
If you have a product that handles SMS replies, you can use our reply feature to simulate this. When you reply, the SMS is sent back to the phone number it was originally sent from:
await mailosaur.messages.reply("MESSAGE_ID", {
text: "FYI",
});
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 via the creation of automated forwarding rules, or one at a time. 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 and attachments. This operation cannot be undone:
await mailosaur.messages.del("MESSAGE_ID");
Delete all messages
Permanently deletes all messages and attachments in the specified server/inbox. This operation cannot be undone:
await mailosaur.messages.deleteAll("SERVER_ID");