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-testidonly when no accessible name exists. - Use
toHaveURLandtoBeVisibleassertions rather than fixed timeouts — they retry automatically. - Configure
baseURLinplaywright.config.tssopage.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/);
});