How to Test Form Validation Messages with Playwright
On sign-up and contact forms, you can use Playwright tests to confirm that input validation errors appear correctly. The bubble that the browser shows for required or type="email" is not an element on the page, so getByText() cannot find it. Instead, check the validationMessage property of the input with toHaveJSProperty(). For an error message that you display on the page yourself, you can use toHaveAccessibleErrorMessage() to confirm that the message is linked to the input.
I tested on Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. I used only headless Chromium. I did not test Firefox, WebKit, Google Chrome, or headed mode (showing the browser window). I also did not test the message language on macOS or Windows.
The example form
I made a sign-up form. The email field uses type="email" and required, and the nickname field uses minlength="3". The confirmation password field uses setCustomValidity() to show パスワードが一致しません (Japanese for "Passwords do not match") when it does not match the password above.
<form id="native" action="/done">
<label for="email">メールアドレス</label>
<input id="email" name="email" type="email" required>
<label for="nickname">ニックネーム(3文字以上)</label>
<input id="nickname" name="nickname" minlength="3">
<label for="password">パスワード</label>
<input id="password" name="password" type="password" required>
<label for="confirm">パスワード(確認)</label>
<input id="confirm" name="confirm" type="password" required>
<button>登録する</button>
</form>
<script>
const password = document.getElementById('password');
const confirm = document.getElementById('confirm');
const check = () => {
confirm.setCustomValidity(confirm.value === password.value ? '' : 'パスワードが一致しません');
};
password.addEventListener('input', check);
confirm.addEventListener('input', check);
</script>
If you press the 登録する (Register) button without entering anything, the browser shows a bubble under the email field and stops the submission.

getByText cannot find the bubble text
The bubble appears in screenshots, but it is not in the page's HTML. So a test that looks for the text with getByText() fails. Every test in this article calls page.goto('/') in beforeEach to open the page before it starts.
test('ブラウザの吹き出しを getByText で探す', async ({ page }) => {
await page.getByRole('button', { name: '登録する' }).click();
await expect(page.getByText('Please fill out this field.')).toBeVisible({ timeout: 2000 });
});
Error: expect(locator).toBeVisible() failed
Locator: getByText('Please fill out this field.')
Expected: visible
Timeout: 2000ms
Error: element(s) not found
You can read the text shown in the bubble from the input's validationMessage property. In Playwright, toHaveJSProperty() checks the value of an element's property.
Check validationMessage and validity
The following test confirms that when you try to submit with the form empty, the page does not change, the email field has focus, and the error is "no value entered".
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test('メールアドレスが空のままでは送信されない', async ({ page }) => {
const email = page.getByLabel('メールアドレス');
await page.getByRole('button', { name: '登録する' }).click();
await expect(page).toHaveURL('/');
await expect(email).toBeFocused();
expect(await email.evaluate((el: HTMLInputElement) => el.validity.valueMissing)).toBe(true);
await expect(email).toHaveJSProperty('validationMessage', 'Please fill out this field.');
});
test('@のないメールアドレスは形式の誤りになる', async ({ page }) => {
const email = page.getByLabel('メールアドレス');
await email.fill('neko');
expect(await email.evaluate((el: HTMLInputElement) => el.validity.typeMismatch)).toBe(true);
await expect(email).toHaveJSProperty(
'validationMessage',
"Please include an '@' in the email address. 'neko' is missing an '@'.",
);
await expect(page.locator('#email:invalid')).toHaveCount(1);
});
validity is an object that holds true or false for each condition the input may fail. If required is set and the field is empty, valueMissing is true. If the value does not match the type="email" format, typeMismatch is true. I read these values with evaluate() and compared them. A locator using the CSS :invalid pseudo-class, such as #email:invalid, can also count the fields that fail their constraints.
page.goto('/') works because playwright.config.ts sets use.baseURL to the URL of the server I ran for testing. Both tests passed.
✓ 1 tests/validation.spec.ts:7:1 › メールアドレスが空のままでは送信されない (174ms)
✓ 2 tests/validation.spec.ts:17:1 › @のないメールアドレスは形式の誤りになる (92ms)
I read validationMessage for other inputs and got the following messages. You can read validationMessage before pressing the submit button. Some messages include the entered value, such as 'neko@'.
| Input | validationMessage |
|---|---|
| Email is empty | Please fill out this field. |
Email is neko@ |
Please enter a part following '@'. 'neko@' is incomplete. |
Nickname is ab (under 3 characters) |
Please lengthen this text to 3 characters or more (you are currently using 2 characters). |
For the nickname, I got the same message whether I entered the text with fill() or typed it one character at a time with pressSequentially().
Setting locale to ja-JP does not change the message from English
Even with locale: 'ja-JP' set in playwright.config.ts, the messages stayed in English, as the table above shows. navigator.language was ja-JP, so the language the page sees did change. But the language of the message did not change.
The MDN explanation says that validationMessage is localized (translated to match the language). So I changed the Chromium variant and the language setting, and compared the validationMessage of an empty email field. "headless shell" is the headless-only Chromium that Playwright uses when you do not specify channel. "Regular Chromium" is the new headless mode you get with channel: 'chromium'.
| Chromium variant | Language setting | validationMessage |
|---|---|---|
| headless shell | Environment variable LANG=C.UTF-8 |
Please fill out this field. |
| headless shell | Environment variable LANG=ja_JP.UTF-8 |
Please fill out this field. |
| headless shell | Launch argument --lang=ja |
Please fill out this field. |
| Regular Chromium | Environment variable LANG=C.UTF-8 |
Please fill out this field. |
| Regular Chromium | Launch argument --lang=ja |
Please fill out this field. |
| Regular Chromium | Environment variable LANG=ja_JP.UTF-8 |
このフィールドを入力してください。 |
Of these six combinations, only regular Chromium launched with the environment variable LANG=ja_JP.UTF-8 returned a Japanese message. In every combination, validity.valueMissing was true.
In a config file, you set the environment variables passed to the browser with launchOptions.env. I saved the following as ja.config.ts and ran it with npx playwright test --config=ja.config.ts. ja.spec.ts is a test that reads the validationMessage of the email and nickname fields.
import { defineConfig } from '@playwright/test';
import base from './playwright.config';
export default defineConfig({
...base,
testMatch: 'ja.spec.ts',
use: {
...base.use,
channel: 'chromium',
launchOptions: { env: { ...process.env, LANG: 'ja_JP.UTF-8' } },
},
});
With this setup, the other messages also came back in Japanese.
メール アドレスに「@」を挿入してください。「neko」内に「@」がありません。
このテキストは 3 文字以上で指定してください(現在は 2 文字です)。
The messages that the browser creates change with the browser variant and how it is launched. If you want a test that compares the exact message text, fix the browser and language settings first. If you only want to confirm which condition failed, compare the values in validity.
Messages set with setCustomValidity can be compared as they are
A message set with setCustomValidity() goes into validationMessage unchanged. In all six settings in the table above, the confirmation password field returned パスワードが一致しません. Setting the message back to an empty string clears the error.
test('確認用のパスワードが違うと、自前のメッセージが出る', async ({ page }) => {
await page.getByLabel('パスワード', { exact: true }).fill('nekonoTe1');
const confirm = page.getByLabel('パスワード(確認)');
await confirm.fill('nekonoTe2');
await expect(confirm).toHaveJSProperty('validationMessage', 'パスワードが一致しません');
expect(await confirm.evaluate((el: HTMLInputElement) => el.validity.customError)).toBe(true);
await confirm.fill('nekonoTe1');
await expect(confirm).toHaveJSProperty('validationMessage', '');
});
For a field where you set a message with setCustomValidity(), validity.customError is true. This test passed in headless shell, which returns English messages.
Check errors you show yourself with toHaveAccessibleErrorMessage
Instead of the browser bubble, you can use a form with novalidate and show the error text on the page. As an example, I made a form that shows a pink error message under the input when the postal code is not seven digits.
<form id="custom" action="/done" novalidate>
<label for="zip">郵便番号(7桁の数字)</label>
<input id="zip" name="zip" inputmode="numeric" aria-describedby="zip-hint" aria-errormessage="zip-error">
<p id="zip-hint">ハイフンなしで入力します</p>
<p id="zip-error" class="error" hidden></p>
<button>送信する</button>
</form>
On submit, if the value is not valid, the submit handler stops the submission, sets aria-invalid="true" on the input, puts the message into #zip-error, and shows it.
<script>
const form = document.getElementById('custom');
const zip = document.getElementById('zip');
const zipError = document.getElementById('zip-error');
form.addEventListener('submit', (e) => {
const message = /^\d{7}$/.test(zip.value) ? '' : '郵便番号は7桁の数字で入力してください';
if (message) {
e.preventDefault();
zip.setAttribute('aria-invalid', 'true');
zipError.textContent = message;
zipError.hidden = false;
zip.focus();
} else {
zip.removeAttribute('aria-invalid');
zipError.hidden = true;
}
});
</script>

Use toHaveAccessibleErrorMessage() to check the message linked to the input with aria-errormessage. This confirms that the error text is the error message of that input. To check that the message is visible on the screen, use getByText() and toBeVisible() separately. To check the hint text (aria-describedby), use toHaveAccessibleDescription().
test('自前で表示したエラーを確かめる', async ({ page }) => {
const zip = page.getByLabel('郵便番号');
await zip.fill('123-4567');
await page.getByRole('button', { name: '送信する' }).click();
await expect(zip).toHaveAttribute('aria-invalid', 'true');
await expect(zip).toHaveAccessibleErrorMessage('郵便番号は7桁の数字で入力してください');
await expect(page.getByText('郵便番号は7桁の数字で入力してください')).toBeVisible();
await expect(zip).toHaveAccessibleDescription('ハイフンなしで入力します');
});
This test passed. However, when I showed the error text without aria-invalid="true" on the input, toHaveAccessibleErrorMessage() received an empty string and failed.
Error: expect(locator).toHaveAccessibleErrorMessage(expected) failed
Locator: getByLabel('郵便番号')
Expected: "郵便番号は7桁の数字で入力してください"
Received: ""
Timeout: 2000ms
MDN's explanation of aria-errormessage also says that aria-errormessage is meant to be used when aria-invalid is true. If this test fails, check whether you forgot to set aria-invalid in the page.





