How to test order confirmation notifications with Selenium

Catch mistakes early by automating the testing of order-to-delivery notification process using Selenium .

Generic AI Artwork

When a customer orders something from you, they expect to be kept informed at every step of the way. But manually testing something as complex as an end-to-end delivery process takes a huge amount of time and overlooks the fact that a seemingly unrelated code change could silently leave customers confused and dissatisfied.

That’s where automation steps in. With Selenium and Mailosaur, you can automate the whole testing process, ensuring that every order confirmation, notification and update is accurately delivered and validated, freeing you up to focus on other critical testing tasks.

Automating tests with Selenium

Selenium is a widely-used tool for automating web browsers, allowing QA testers to quickly replicate the actions a typical user might take when interacting with a website or product. One of its primary benefits is cross-browser compatibility, allowing testers to write scripts that can run on different browsers like Chrome, Firefox, and Edge, ensuring that web applications perform consistently across platforms. Additionally, Selenium supports multiple programming languages, including Java, Python, and C#, which makes it adaptable to most organisations, regardless of their tech stack.

However, whilst Selenium is great for simulating a wide array of user interactions it has no mechanism for capturing and processing email or SMS notifications that are sent to a user throughout a suite of tests, for example an order confirmation or delivery update.

Using Selenium to test email or SMS notifications

The simplest way for testers to add email or SMS support to Selenium is by integrating in a tool like Mailosaur. By adding a few lines of code to any Selenium project you can suddenly test any notification that would have otherwise have to be tested manually.

In this article we’ll show you how to use Mailosaur to automate the testing of order confirmations, delivery updates and any other communication with customers.

If you want to dive right in, then this code sample illustrates a typical email test, using Selenium with Mailosaur. This covers everything you need to test the entire order-to-delivery notification process; from triggering the order confirmation to validating its contents, testing links, and verifying attachments. But don't worry, we'll break this down in more detail below.

String apiKey = "API_KEY"; 
String serverId = "SERVER_ID"; // The unique ID of the inbox (server) that you are using 
 
// Create a MailosaurClient instance, using your API key 
MailosaurClient m = new MailosaurClient(apiKey); 
 
// Using the serverId you can create an email address for this test case 
String testEmail = m.servers().generateEmailAddress(serverId); 
 
// NOTE You don't need to create email addresses, you could just make one up 
// String testEmail = String.format("my-test@%s.mailosaur.net", serverId); 
 
// ----------------------------------------------------------------------- 
// TODO Do something here that triggers an email to be sent to `testEmail` 
// ----------------------------------------------------------------------- 
 
// Provide the ID of the inbox (server) that you're using 
MessageSearchParams params = new MessageSearchParams(); 
params.withServer(serverId); 
 
// Provide the search criteria (this searches for any message sent to this address) 
SearchCriteria criteria = new SearchCriteria(); 
criteria.withSentTo(testEmail); 
// ^ If you were doing SMS testing, just make this your Mailosaur phone number 
 
// Get the latest message that matches your criteria 
Message msg = m.messages().get(params, criteria); 
 
// Now you can perform test assertions 
assertEquals("Order 643241 received", msg.subject());

// Check for content within an email
assertTrue(msg.html().body().contains("January 19, 2028")); 
assertTrue(msg.html().body().contains("between 09:30 and 11:30")); 

// You can use msg.text() instead of msg.html() if working with an SMS message 
Link link = msg.html().links().get(0); 
assertEquals("Update my delivery preferences", link.text()); 

// Check the recipient is correct
assertEquals("Jill Smith", msg.to[0].name);
assertEquals("jill@example.com", msg.to[0].email);

// Check the sender is correct
assertEquals("Customer Support", msg.from[0].name);
assertEquals("support@example.com", msg.from[0].email);

// Check attachment attributes 
Attachment attachment = msg.attachments().get(0); 
assertEquals("receipt-643241.pdf", attachment.fileName());
assertEquals("application/pdf", attachment.contentType());

Trigger an order confirmation email

Start by generating a unique email address using Mailosaur—available on all plans—to guarantee that each test receives a distinct confirmation email. This isolation prevents overlap or misidentification between test emails, which is crucial when validating order confirmations across multiple scenarios.

// Using the serverId you can create an email address for this test case 
String testEmail = m.servers().generateEmailAddress(serverId); 

// NOTE You don't need to create email addresses, you could just make one up 
// String testEmail = String.format("my-test@%s.mailosaur.net", serverId); 

// ----------------------------------------------------------------------- 
// TODO Do something here that triggers an email to be sent to `testEmail` 
// ----------------------------------------------------------------------- 

Locate the email in your inbox

Once the confirmation email is sent, it's vital to confirm its receipt. Use 'SearchCriteria' to programmatically locate the email associated with the unique address, ensuring it was successfully sent and received for the test scenario.

// Provide the ID of the inbox (server) that you're using 
MessageSearchParams params = new MessageSearchParams(); 
params.withServer(serverId); 

// Provide the search criteria (this searches for any message sent to this address) 
SearchCriteria criteria = new SearchCriteria(); 
criteria.withSentTo(testEmail); 
// ^ If you were doing SMS testing, just make this your Mailosaur phone number 

// Get the latest message that matches your criteria 
Message msg = m.messages().get(params, criteria); 

Checking the email subject

Customers are usually expect to see their order/invoice number, or perhaps their name as a way of significantly improving the email’s validity to them. With this simple assertion we that the subject line contains the order number we expect:

assertEquals("Order 643241 received", msg.subject());

Testing content within an email

Order and delivery updates will always contains crucial details in the body of an email, such as a delivery date and order summary. Missing or incorrect elements can result in a negative customer experience. Here we validate both HTML and plain text versions of the email, ensuring that specific elements and text are accurately represented:

assertTrue(msg.html().body().contains("January 19, 2028")); 
assertTrue(msg.html().body().contains("between 09:30 and 11:30")); 

// Note that the lines above check the HTML of an email.
// If you're testing an SMS message (or the plain text content of an email) instead:
assertTrue(msg.text().body().contains("January 19, 2028")); 
assertTrue(msg.text().body().contains("between 09:30 and 11:30")); 

Validatng and testing links

In most cases, your email will contain one or more links to critical functionality, such as an order tracking website or delivery preference center. Mailosaur automatically extracts any links found in the content of an email or SMS message, allowing you to navigate to them using Selenium, just like any other URL:

// You can use msg.text() instead of msg.html() if working with an SMS message 
Link link = msg.html().links().get(0); 

assertEquals("Update my delivery preferences", link.text()); 

// Navigate to the preferences screen using Selenium 
WebDriver driver = new ChromeDriver(); 
driver.get(link.href()); 

Verify recipient and sender information

Of course, you'll want to make sure that each notification is sent to the right person. You can

Customers are usually expect to see their order/invoice number, or perhaps their name as a way of significantly improving the email’s validity to them. With this simple assertion we that the subject line contains the order number we expect:

// Check the recipient is correct
assertEquals("Jill Smith", msg.to[0].name);
assertEquals("jill@example.com", msg.to[0].email);

// Check the sender is correct
assertEquals("Customer Support", msg.from[0].name);
assertEquals("support@example.com", msg.from[0].email);

Testing any attachments

Finally, if you typically include attachments with your emails, such as an accompanying receipt or invoice, you can ensure they are included and accessible to customers:

Attachment attachment = msg.attachments().get(0); 

// Check attachment attributes 
assertEquals("receipt-643241.pdf", attachment.fileName());
assertEquals("application/pdf", attachment.contentType());

// Save attachment to disk for further testing
byte[] file = m.files().getAttachment(attachment.id()); 
Files.write(Paths.get(attachment.fileName()), file); 

By following these steps, you’ll ensure the entire order confirmation email process is thoroughly tested, covering both content and functionality.

Try it for yourself

As a Selenium user, there’s so much more you can do with Mailosaur than just order confirmation notification tests. Try out MFA testing, password reset testing, and much more with a 14-day free trial.

Or if you’d like to learn more about what Mailosaur can do in Selenium, you can speak to our sales team, or try out our documents for Selenium.