# Selenium email and SMS plugin

Use email and SMS in Selenium tests using MailSlurp. Integrate easily with existing Selenium projects. Simple add the [NodeJS](/docs/js/), [Java](/docs/java/), [CSharp](/docs/csharp/) or [Python](/docs/python/) MailSlurp package to your test environment then you can create email addresses, send and receive emails, and send and receive TXT messages.

## Examples

<div data-component="DocsExamplesList" class="docs-card-grid docs-examples-list" data-example-terms="selenium" data-example-count="7">
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/csharp-dotnet-core2-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Csharp Dotnet Core2 Selenium</span><span class="docs-card-description">csharp example repository: <code>csharp-dotnet-core2-selenium</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/csharp-specflow-mstest-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Csharp Specflow Mstest Selenium</span><span class="docs-card-description">csharp example repository: <code>csharp-specflow-mstest-selenium</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/java-maven-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Java Maven Selenium</span><span class="docs-card-description">java example repository: <code>java-maven-selenium</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/java-testng-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Java Testng Selenium</span><span class="docs-card-description">java example repository: <code>java-testng-selenium</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/ruby-capybara-cucumber-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Ruby Capybara Cucumber Selenium</span><span class="docs-card-description">ruby example repository: <code>ruby-capybara-cucumber-selenium</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/rust-selenium-email-testing" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Rust Selenium Email Testing</span><span class="docs-card-description">rust example repository: <code>rust-selenium-email-testing</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/totp-mfa-auth0-selenium" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Totp Mfa Auth0 Selenium</span><span class="docs-card-description">totp example repository: <code>totp-mfa-auth0-selenium</code></span></a>
</div>

## Resources
- [CSharp Selenium example](https://github.com/mailslurp/examples/tree/master/csharp-specflow-mstest-selenium)
- [Java Selenium example](https://github.com/mailslurp/examples/tree/master/java-maven-selenium)
- [Ruby Capybara](https://github.com/mailslurp/examples/tree/master/ruby-capybara-cucumber-selenium)
- [Get free API KEY](https://app.mailslurp.com/)

## Tutorial

<figure data-component="DocsVideoEmbed" class="docs-video-embed">
<iframe src="https://www.youtube-nocookie.com/embed/19w81QhX3YQ?modestbranding=1&rel=0&controls=1" title="MailSlurp documentation video" loading="lazy" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
<figcaption><a href="https://www.youtube.com/watch?v=19w81QhX3YQ">Open video on YouTube</a></figcaption>
</figure>

## Setup
Install the MailSlurp package for your language and then configure it with your API Key. See the [SDKs page](/docs/sdks/) for more information.

### Java
Add the [maven central package](https://search.maven.org/artifact/com.mailslurp/mailslurp-client-java).
```xml
<dependency>
    <groupId>com.mailslurp</groupId>
    <artifactId>mailslurp-client-java</artifactId>
</dependency>
```

### CSharp
Use the [Nuget package](https://www.nuget.org/packages/mailslurp/):

```sh
dotnet add package mailslurp
```

### NodeJS
Add the [NPM package](https://npmjs.org/mailslurp-client).

```sh
npm install --save mailslurp-client
```

### Python
Use the [PyPi package](https://pypi.org/project/mailslurp-client/).
```sh
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.

```java
// 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).execute();
```

## 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:

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

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

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

### Search for an inbox
Use search methods to search for an inbox:

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

### Waiting for messages
MailSlurp [wait for methods](/docs/wait-for/) allow you to wait for an expected email or SMS to arrive during testing.

```java
WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient);
List 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")
        )

).execute();
```

### Searching for messages
You can search for messages too.

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

> **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.

```java
Email email = emailController.getEmail(emailId).execute();
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.

```typescript
// 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)
```

### Get links
You can also extract links from messages.

```typescript
// 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.

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

