Your first Playwright test with TypeScript
Set up a small test, assert user-visible behavior, and make failures easy to debug.
1. Install and initialize
In a Node.js project run: npm init playwright@latest. Select TypeScript and install browsers.
2. Write a focused scenario
Prefer accessible locators like getByRole and getByLabel. Assert visible outcomes, not implementation details.
3. Run and inspect
Use npx playwright test and npx playwright show-report. Investigate traces before adding waits.
4. Make it maintainable
Avoid hard-coded sleeps, keep tests independent, and use fixtures for shared setup.
Example test
import { test, expect } from '@playwright/test';
test('homepage has a title', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle('Example Domain');
});For a real app, replace the URL and assertion with a meaningful user outcome.
Key takeaway
Code example below demonstrates a simple public page title assertion.