Fill and submit a form

Fill fields by their label, check options, submit, and assert the result the user sees.

Forms are label-first in Playwright PHP. You find each control the way a user does, by its visible label or role, then act on the returned locator. Filling and clicking are locator actions, not page actions.

Fill fields by their label

getByLabel() resolves the input from its <label>, so the test reads like the form. fill() clears the field, then types the value.

php
$page->goto('https://app.example.test/signup');

$page->getByLabel('Full name')->fill('Ada Lovelace');
$page->getByLabel('Email')->fill('ada@example.test');
$page->getByLabel('Password')->fill('correct horse battery');

There is no $page->fill(). Filling lives on the locator, so the element is re-resolved and waited for on every call.

Check boxes and choose radios

check() waits until the control is actionable, then checks it. It is idempotent: a control that is already checked stays checked. Use uncheck() to clear one.

php
$page->getByLabel('Subscribe to the newsletter')->check();
$page->getByRole('radio', ['name' => 'Annual billing'])->check();

Role options go in an array. There are no named arguments here.

Submit and assert the result

Click the submit control by its accessible name, then assert the state the user would see. Do not assert that the click happened; assert what it produced.

php
$page->getByRole('button', ['name' => 'Create account'])->click();

expect($page->getByText('Welcome, Ada'))->toBeVisible();

expect() polls until the page reaches the expected state, so you do not need a fixed wait after submit.

Pitfalls

  • A label that matches two inputs makes getByLabel() fail on purpose. Locators are strict: narrow the label text or scope with a container locator.
  • Asserting a redirect URL alone can pass before the page renders. Assert a visible element as well.

Expected result

The form reaches the submitted state the user sees: a success message, new page heading, validation error, or saved value. Do not stop at "the button was clicked".

Go next

← All recipes