Mailosaur logo
Mailosaur logo

How to test SMS in Playwright

Learn how to create SMS tests in Playwright, using Mailosaur.

What is Playwright?
A hashtag icon

Playwright is a Node.js library that lets you automate modern web browsers. Playwright is made by Microsoft and is used as part of the Quality Assurance testing of Microsoft’s own service.

At this stage, we assume that you already know how to test with Playwright, however if you’re just getting started, check out the official Playwright guide, then come back here to start testing email and SMS!

What is Mailosaur?
A hashtag icon

Mailosaur is a service that captures email and SMS messages and lets you test them, both via it’s web dashboard and via the Mailosaur testing API.

If you don’t already have one, create a free account trial now.

Create a Playwright project
A hashtag icon

It only takes a few minutes to create a simple Playwright project to work with, but if you have an existing project you can skip this step.

First, initialise a new project with:

npm init playwright@latest

When prompted, select the following options:

Getting started with writing end-to-end tests with Playwright:
Initializing project in '.'
✔ Do you want to use TypeScript or JavaScript? · TypeScript
✔ Where to put your end-to-end tests? · tests
✔ Add a GitHub Actions workflow? (y/N) · false
✔ Install Playwright browsers (can be done manually via 'npx playwright install')? (Y/n) · true

A simple Playwright project is created for you, with some example tests included within the file tests/example.spec.ts:

import { test, expect } from "@playwright/test";

test("has title", async ({ page }) => {
  await page.goto("");

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);

test("get started link", async ({ page }) => {
  await page.goto("");

  // Click the get started link.
  await page.getByRole("link", { name: "Get started" }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(
    page.getByRole("heading", { name: "Installation" })

You can now run these tests using:

$ npx playwright test

The output looking something like this, with all tests passing:

Running 6 tests using 5 workers
  6 passed (5.1s)

Install the Mailosaur API client
A hashtag icon

To allow you to easily test SMS messages in Playwright, Mailosaur provides an official Node.js library with everything you need to easily automate SMS testing.

Install the Mailosaur Node.js library via npm
A hashtag icon

npm install mailosaur

Import Mailosaur into your project
A hashtag icon

In order to connect to Mailosaur, you need an API key. You access your API key via in the account settings area.

In your tests, simply import the Mailosaur library, using your API key, at the top of your spec file:

import { test, expect } from "@playwright/test";
import MailosaurClient from "mailosaur";

const mailosaur = new MailosaurClient("YOUR_API_KEY_HERE");

Basic usage
A hashtag icon

The messages.get method automatically waits for the first SMS message to arrive that matches the search criteria you provide.

test("SMS notifications", async ({ page }) => {
  const serverId = "YOUR_SERVER_ID_HERE";
  const testPhoneNumber = "15551234444";

  // TODO Do something here that will trigger an SMS message to `testPhoneNumber`

  // Connect to Mailosaur, and wait for that SMS message to arrive
  const sms = await mailosaur.messages.get(serverId, {
    sentTo: testPhoneNumber,

  expect(sms.text.body).toBe("Your order is on the way!");

Testing SMS with Playwright
A hashtag icon

Playwright users should use the official Node.js library for Mailosaur. Head over to our SMS testing guide for Node.js for examples on everyhing you could possibly want to test with Playwright, including: