Playwright Test Stub Builder

Generate a Playwright test file for browser automation and E2E testing

Build a Playwright @playwright/test spec with page.goto, getByRole locators, fill, click, and web-first expect assertions covering a login and navigation flow — ready to run with npx playwright test.

Why does Playwright prefer getByRole over CSS selectors?

Role and label locators mirror how users and assistive tech perceive the page, so they are resilient to markup changes and also nudge you toward accessible UI. The Playwright team recommends them as the default; CSS selectors are a fallback.

A runnable Playwright spec, generated

Playwright tests use locators and web-first assertions: you find an element by its role or label, act on it, and assert with retrying expectations. A first spec usually logs in and verifies a redirect, then checks navigation. This builder produces that file using the recommended locator style so it is robust out of the gate.

How it works

The tool imports { test, expect } from '@playwright/test' and writes a test('...', async ({ page }) => { ... }). Inside, it calls await page.goto(loginRoute), fills the email and password fields with either page.getByRole('textbox', { name }) or page.getByLabel(name), clicks the submit button via page.getByRole('button', { name }), then asserts the redirect with await expect(page).toHaveURL(/dashboard/). A second test checks navigation by clicking a link and asserting a heading is visible with toBeVisible(). All actions are awaited and all assertions are web-first, so no manual sleeps are needed.

Tips and example

  • Prefer role/label locators; reach for CSS or data-testid only when no accessible name exists.
  • Use toHaveURL and toBeVisible assertions rather than fixed timeouts — they retry automatically.
  • Configure baseURL in playwright.config.ts so page.goto('/login') works with a relative path.
import { test, expect } from "@playwright/test";

test("logs in and reaches the dashboard", async ({ page }) => {
  await page.goto("/login");
  await page.getByLabel("Email").fill("[email protected]");
  await page.getByLabel("Password").fill("Passw0rd!");
  await page.getByRole("button", { name: "Sign in" }).click();
  await expect(page).toHaveURL(/dashboard/);
});