# Playwright MailSlurp testing

MailSlurp integrates easily with existing Playwright projects. Simple add the [NodeJS](/docs/js/), [Java](/docs/java/), [CSharp](/docs/csharp/) or [Python](/docs/python/) MailSlurp package to your test environment and 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="playwright" data-example-count="6">
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/csharp-playwright-nunit-sms-otp" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Csharp Playwright Nunit Sms Otp</span><span class="docs-card-description">csharp example repository: <code>csharp-playwright-nunit-sms-otp</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/playwright-codegen-recorder-nocode" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Playwright Codegen Recorder Nocode</span><span class="docs-card-description">playwright example repository: <code>playwright-codegen-recorder-nocode</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/playwright-email-testing" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Playwright Email Testing</span><span class="docs-card-description">playwright example repository: <code>playwright-email-testing</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/playwright-lowcode-testing" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Playwright Lowcode Testing</span><span class="docs-card-description">playwright example repository: <code>playwright-lowcode-testing</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/playwright-sms-testing" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Playwright Sms Testing</span><span class="docs-card-description">playwright example repository: <code>playwright-sms-testing</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/python3-django-playwright" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Python3 Django Playwright</span><span class="docs-card-description">python3 example repository: <code>python3-django-playwright</code></span></a>
</div>

## Resources
- [Test SMS OTP](https://www.mailslurp.com/guides/test-sms-otp-mfa-using-playwright/#main-post-header-title)
- [Email magic links](https://www.mailslurp.com/guides/test-next-auth-magic-links/#main-post-header-title)
- [Email testing example project](https://github.com/mailslurp/examples/tree/master/playwright-email-testing)
- [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/fsN_PXh9riM?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=fsN_PXh9riM">Open video on YouTube</a></figcaption>
</figure>

## Get started
In order to use MailSlurp in your Playwright tests you need to do two things:

- Obtain and configure an [API KEY](https://app.mailslurp.com/)
- Install the MailSlurp client library for your language

### Javascript

```bash
npm install --save mailslurp-client
```

### CSharp

```bash
dotnet add package mailslurp
```

### Java

For Maven add:

```xml
<!-- place inside the <dependencies> block -->
<dependency>
  <groupId>com.mailslurp</groupId>
  <artifactId>mailslurp-client-java</artifactId>
  <version>LATEST</version>
  <type>pom</type>
</dependency>
```

For gradle add:

```kotlin
dependencies {
    implementation("com.mailslurp:mailslurp-client-java")
}
```

### Python

```bash
pip install mailslurp-client
```

## Use MailSlurp within your tests
You can now use MailSlurp to create email addresses, send and receive emails, and send and receive TXT messages. Here is an example of how to use MailSlurp in a Playwright test:

```typescript
import { test, expect, Page } from '@playwright/test';
import MailSlurp from "mailslurp-client";

test.describe('test email login with playwright', () => {
  test('can login and verify email address with mailslurp', async ({ page }) => {
    const apiKey = process.env.API_KEY;
    expect(apiKey).toBeDefined();

    // load playground app
    await page.goto("https://playground.mailslurp.com");
    await page.click('[data-test="sign-in-create-account-link"]');

    // create a new inbox
    const mailslurp = new MailSlurp({ apiKey })
    const password = "test-password"
    const { id, emailAddress } = await mailslurp.createInbox()

    // fill sign up form
    await page.fill('input[name=email]', emailAddress);
    await page.fill('input[name=password]', password);
    await page.click('[data-test="sign-up-create-account-button"]');

    // wait for verification code
    const email = await mailslurp.waitForLatestEmail(id)

    // extract the confirmation code (so we can confirm the user)
    const code = /([0-9]{6})$/.exec(email.body)[1];

    // enter confirmation code
    await page.fill('[data-test="confirm-sign-up-confirmation-code-input"]', code);
    await page.click('[data-test="confirm-sign-up-confirm-button"]');

    // fill out username (email) and password
    await page.fill('[data-test="username-input"]', emailAddress);
    await page.fill('[data-test="sign-in-password-input"]', password);
    // submit
    await page.click('[data-test="sign-in-sign-in-button"]');
    await page.waitForSelector("[data-test='greetings-nav']")
  });

});
```

## Example projects
For more examples see the [MailSlurp examples repository] or the following:

- [Playwright JS email example](https://github.com/mailslurp/examples/tree/master/playwright-email-testing)
- [Playwright JS sms testing](https://github.com/mailslurp/examples/tree/master/playwright-sms-testing)
