Skip to contentPlayForge

How do I check the value of an input field in Playwright?

Written by Jatin Thacker, Automation Architect · Updated

Use await expect(locator).toHaveValue(...), which retries until the input's current value matches a string or a regular expression. Do not use toHaveText() for this: the text of an input is empty, and what the user typed lives in its value.

This answer is one piece of Chapter 11: Assertions.

  • Web-First Assertions Cookbook
  • Retry & Polling Patterns
  • Visual Regression Testing
  • Making a Failure Explain Itself

An <input> does not hold its contents as text inside the element. What the user typed, or what a script filled in, is the input's value. That is why toHaveText() on a text field disappoints, and why Playwright has a separate assertion, toHaveValue().

Like every web-first assertion, toHaveValue() retries. That matters when a value is filled in by script after the page loads.

The example uses two inputs:

  • On the dashboard, a script fills a Report reference field with RPT-0093. The test finds it by its label and checks the exact value. A regular expression, /^RPT-\d{4}$/, checks the shape instead, which is the better choice when the number changes between environments.
  • On the login page, the test fills Work email itself and then checks that the value stuck. This catches fields that reformat or reject input.

Checking a value you read yourself, as in expect(await email.inputValue()).toBe(...), is a non-retrying assertion: it checks once. Use toHaveValue() so the check waits.

value.spec.js(https://practice.learnplayforge.com)
import { test, expect } from '@playwright/test'; test('reading what an input holds', async ({ page }) => { await page.goto('/index.html?mock=static&seed=42'); await expect(page.getByLabel('Report reference')).toHaveValue('RPT-0093'); await expect(page.getByLabel('Report reference')).toHaveValue(/^RPT-\d{4}$/); await page.goto('/login.html?mock=static&seed=42'); const email = page.getByLabel('Work email'); await email.fill('ops@meridian.test'); await expect(email).toHaveValue('ops@meridian.test'); });
Output
1 passed (1.5s)
Test yourself
After await email.fill('ops@meridian.test'), which assertion confirms what the field holds?

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.