Frameworks and toolsSeleniumSMS testing

SMS testing for Selenium

Learn how to comprehensively test every element of your SMS communication within Selenium

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:

  1. Send an SMS message to your dedicated Mailosaur phone number.
  2. Connect to the Mailosaur API with the official client library.
  3. Search for the message you sent in step 1.
  4. 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());
# Setup the API client
mailosaur = MailosaurClient("API_KEY")

criteria = SearchCriteria()
criteria.sent_to = "123456789" # Phone number

# Search for the message
message = mailosaur.messages.get("SERVER_ID", criteria)

# Perform test assertions
self.assertEqual("Support", message.sender[0].name)
self.assertEqual("654321", message.sender[0].phone)
// Setup the API client
var mailosaur = new MailosaurClient("API_KEY");

var criteria = new SearchCriteria() {
    SentTo = "123456789" // Phone number
};

// Search for the message
var message = mailosaur.Messages.Get("SERVER_ID", criteria);

// Perform test assertions
Assert.Equal("Support", message.From[0].Name);
Assert.Equal("654321", message.From[0].Phone);
# Setup the API client
mailosaur = Mailosaur::MailosaurClient.new("API_KEY")

criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "123456789" # Phone number

# Search for the message
message = mailosaur.messages.get("SERVER_ID", criteria)

# Perform test assertions
assert_equal("Support", message.from[0].name)
assert_equal("654321", message.from[0].phone)
// Setup the API client
const MailosaurClient = require("mailosaur");
const mailosaur = new MailosaurClient("API_KEY");

// Search for the message
const message = await mailosaur.messages.get("SERVER_ID", {
  sentTo: "123456789", // Phone number
});

// Perform test assertions
expect(message.from[0].name).toBe("Support");
expect(message.from[0].phone).toBe("654321");

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());
criteria = SearchCriteria()
criteria.sent_to = "123456789" # Phone number

message = mailosaur.messages.get("SERVER_ID", criteria)

print(message.text.body)
var criteria = new SearchCriteria() {
    SentTo = "123456789" // Phone number
};

var message = mailosaur.Messages.Get("SERVER_ID", criteria);

Console.WriteLine(message.Text.Body);
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "123456789" # Phone number

message = mailosaur.messages.get("SERVER_ID", criteria)

puts(message.text.body)
// Search for the message
const message = await mailosaur.messages.get("SERVER_ID", {
  sentTo: "123456789", // Phone number
});

console.log(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, If you want to look further back than this, just set the 'received after' parameter.

// Calculate a timestamp for yesterday
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -1);
long yesterday = calendar.getTimeInMillis();

// Use this timestamp in the search
MessageSearchParams params = new MessageSearchParams();
params.withServer("SERVER_ID");
params.withReceivedAfter(yesterday);

SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.withSentTo("123456789");

Message message = mailosaur.messages().get(params, searchCriteria);
# Calculate a datetime for yesterday
yesterday = datetime.today() - timedelta(days=1)

criteria = SearchCriteria()
criteria.sent_to = "123456789"

# Use this timestamp in the search
message = mailosaur.messages.get("SERVER_ID", criteria, received_after=yesterday)
// Calculate a datetime for yesterday
var yesterday = DateTime.Today.AddDays(-1);

var criteria = new SearchCriteria() {
    SentTo = "123456789"
};

// Use this timestamp in the search
var message = mailosaur.Messages.Get("SERVER_ID", criteria, receivedAfter: yesterday);
# 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 = mailosaur.messages.get("SERVER_ID", criteria)
// Calculate a datetime for yesterday
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);

// Use this timestamp in the search
const message = await mailosaur.messages.get("SERVER_ID", {
  sentTo: "123456789",
}, {
  receivedAfter: yesterday
});

"No matching messages" troubleshooting

If you see the error "No matching messages found in time" when searching for messages:

  • Ensure that 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 on how to override this.
  • Check you have correctly set the sent to parameter in your search criteria.

Other ways to fetch SMS messages

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.
SearchCriteria criteria = new SearchCriteria();
criteria.withSentTo("123456789");

// 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());
# Search for all messages sent to 123456789.
criteria = SearchCriteria()
criteria.sent_to = "123456789"

# Limit results to the first 10 matches only.
result = mailosaur.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 = mailosaur.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 = mailosaur.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 object
var message = mailosaur.Messages.GetById(latestMessage.Id);

Console.WriteLine(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 = mailosaur.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 = mailosaur.messages.get_by_id(latest_message.id)

puts(message.text.body)
// Search for all messages sent to 123456789.
// Limit results to the first 10 matches only.
const result = await 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 = await 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
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());
# List the most recent messages
result = mailosaur.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 = mailosaur.messages.get_by_id(latest_message.id)

print(message.text.body)
// List the most recent messages
var result = mailosaur.Messages.List("SERVER_ID");

// Get the most recent message (the first one in the list)
var latestMessage = result.Items[0];

// Get the full message object
var message = mailosaur.Messages.GetById(latestMessage.Id);

Console.WriteLine(message.Text.Body);
# List the most recent messages
result = mailosaur.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 = mailosaur.messages.get_by_id(latest_message.id)

puts(message.text.body)
// List the most recent messages
const result = await 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 = await mailosaur.messages.getById(latestMessage.id);

console.log(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 information
self.assertEqual("Support", message.sender[0].name)
self.assertEqual("654321", message.sender[0].phone)

# Test recipient information
self.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 information
expect(message.from[0].name).toBe("Support");
expect(message.from[0].phone).toBe("654321");

// Test recipient information
expect(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:

System.out.println(message.text().body()); // "Hi Jason, ..."
print(message.text.body) # "Hi Jason, ..."
Console.WriteLine(message.Text.Body); // "Hi Jason, ..."
puts(message.text.body) # "Hi Jason, ..."
console.log(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());
# How many links?
print(len(message.text.links)) # 2

first_link = message.text.links[0]
self.assertEqual("Google Search", first_link.text)
self.assertEqual("https://www.google.com/", first_link.href)
// How many links?
Console.WriteLine(message.Text.Links.Count); // 2

var firstLink = message.Text.Links[0];
Assert.Equal("Google Search", firstLink.Text);
Assert.Equal("https://www.google.com/", firstLink.Href);
# How many links?
puts(message.text.links.length) # 2

first_link = message.text.links[0]
assert_equal("Google Search", first_link.text)
assert_equal("https://www.google.com/", first_link.href)
// How many links?
console.log(message.text.links.length); // 2

const firstLink = message.text.links[0];
expect(firstLink.text).toBe("Google Search");
expect(firstLink.href).toBe("https://www.google.com/");

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());
otp = message.text.codes[0]
self.assertEqual("456812", otp.value)
var otp = message.Text.Codes[0];
Assert.Equal("456812", otp.Value);
otp = message.text.codes[0]
assert_equal("456812", otp.value)
const otp = message.text.codes[0];
expect(otp.value).toBe("456812");

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);
options = MessageReplyOptions(text="FYI")

mailosaur.messages.reply("MESSAGE_ID", options)
mailosaur.Messages.Reply("MESSAGE_ID", new MessageReplyOptions()
  {
      Text = "FYI"
  }
);
options = Mailosaur::Models::MessageReplyOptions.new()
options.text = "FYI"

mailosaur.messages.reply("MESSAGE_ID", options)
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 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);
recipient = "verified-address@example.com"
options = MessageForwardOptions(recipient, text="FYI")

mailosaur.messages.forward("MESSAGE_ID", options)
mailosaur.Messages.Forward("MESSAGE_ID", new MessageForwardOptions()
  {
      To = "verified-address@example.com",
      Text = "FYI"
  }
);
options = Mailosaur::Models::MessageForwardOptions.new()
options.to = 'verified-address@example.com'
options.text = 'FYI'

mailosaur.messages.forward('MESSAGE_ID', options)
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:

mailosaur.messages().delete("MESSAGE_ID");
mailosaur.messages.delete("MESSAGE_ID")
mailosaur.Messages.Delete("MESSAGE_ID");
mailosaur.messages.delete("MESSAGE_ID")
await mailosaur.messages.del("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");
mailosaur.messages.delete_all("SERVER_ID")
mailosaur.Messages.DeleteAll("SERVER_ID");
mailosaur.messages.delete_all("SERVER_ID")
await mailosaur.messages.deleteAll("SERVER_ID");

See also