Skip to main content

Selenium email and SMS plugin

Use email and SMS in Selenium tests using MailSlurp. Integrate easily with existing Selenium projects. Simple add the NodeJS, Java, CSharp or Python MailSlurp package to your test environment then you can create email addresses, send and receive emails, and send and receive TXT messages.

Resources

Tutorial

Setup

Install the MailSlurp package for your language and then configure it with your API Key. See the SDKs page for more information.

Java

Add the maven central package.

<dependency>
<groupId>com.mailslurp</groupId>
<artifactId>mailslurp-client-java</artifactId>
</dependency>

CSharp

Use the Nuget package:

dotnet add package mailslurp

NodeJS

Add the NPM package.

npm install --save mailslurp-client

Python

Use the PyPi package.

pip install mailslurp-client

Usage

Once you have installed the MailSlurp package you can use it to create email addresses, send and receive emails, and send and receive SMS messages in your Selenium tests.

The following sections will give usage examples in Java and Typescript but you can use the same methods in any language.

Creating inboxes

You can create inboxes that are disposable or permanent. Use name, description, or tags to so you can find it again.

// use names, description, and tags to identify an inbox
String randomString = String.valueOf(new Random().nextLong());
String customName = "Test inbox " + randomString;
String customDescription = "My custom description " + randomString;
String customTag = "test-inbox-" + randomString;
// create inbox with options so we can find it later
CreateInboxDto options = new CreateInboxDto()
.name(customName)
.description(customDescription)
.tags(Collections.singletonList(customTag));
InboxDto inbox = inboxControllerApi.createInboxWithOptions(options);

Finding inboxes

You can either create a new inbox each test run or fetch an existing inbox. When fetching you can either get it directly by the ID or search for it. When searching you need something to search for such as an email address of the name, description, or tags used when the inbox was created.

Get inbox by address or name

Get an inbox by name or address like so:

// get inbox by id
InboxDto inboxById = inboxControllerApi.getInbox(inbox.getId());

// lookup inbox by address
InboxByEmailAddressResult inboxByAddress = inboxControllerApi.getInboxByEmailAddress(inbox.getEmailAddress());
assertEquals(inboxByAddress.getInboxId(), inbox.getId());

// lookup inbox by name
InboxByNameResult inboxByName = inboxControllerApi.getInboxByName("Non-existing inbox");
assertFalse(inboxByName.getExists());

Search for an inbox

Use search methods to search for an inbox:

PageInboxProjection inboxSearchResult = inboxControllerApi.searchInboxes(
new SearchInboxesOptions()
.search(customTag)
);
assertEquals(inboxSearchResult.getNumberOfElements(), Integer.valueOf(1));
assertEquals(inboxSearchResult.getContent().get(0).getId(), inbox.getId());

Waiting for messages

MailSlurp wait for methods allow you to wait for an expected email or SMS to arrive during testing.

WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient);
List<EmailPreview> matchingEmails = waitForControllerApi.waitFor(new WaitForConditions()
.inboxId(inbox.getId())
.timeout(120000L)
.count(1)
.countType(WaitForConditions.CountTypeEnum.ATLEAST)
.unreadOnly(true)
.addMatchesItem(new MatchOption()
.field(MatchOption.FieldEnum.SUBJECT)
.should(MatchOption.ShouldEnum.CONTAIN)
.value("Test subject")
)

);

Searching for messages

You can search for messages too.

EmailControllerApi emailController = new EmailControllerApi(mailslurpClient);
PageEmailProjection emailSearch = emailController.searchEmails(
new SearchEmailsOptions()
.searchFilter("Test subject")
);

Note: searching will not wait for matching conditions. Use the waitFor methods to ensure matches await expected emails in case they have not yet arrived.

Getting content

You can get the content of emails and SMS messages.

Email email = emailController.getEmail(emailId, false);
assertEquals(email.getSubject(), mySubject);
assertNotNull(email.getBody());
assertNotNull(email.getFrom());
assertNotNull(email.getAttachments());

Extract codes

Use RegExp patterns to extract codes from emails or SMS messages.

// query HTML for content
const { matches: [, captureGroup]} = await emailController.getEmailContentMatch({
emailId: emailId,
contentMatchOptions: {
// use regex capture groups to extract codes
pattern: 'Your verification code is: (\\d{6})',
}
});
expect(captureGroup).toEqual(sentVerificationCode)

You can also extract links from messages.

// get links from email content
const { links } = await emailController.getEmailLinks({
emailId: emailId,
});

Query HTML

You can use XPath or CSS selectors to query HTML content.

// query HTML for content
const { lines } = await emailController.getEmailHTMLQuery({
emailId: emailId,
htmlSelector: '.heading > .username'
});