October 28, 2023
1 minute read
We will get started by setting up playwright using npm
.
npx init playwright@latest
This will install the latest version of playwright and other related packages. You can choose between TypeScript or JavaScript. Select all the configuration as default.
By default, when we install playwright, a example.spec.ts
file is created under tests
folder. Its contains some example tests.
We can also add a separate test like below:
import { test, expect } from '@playwright/test';
test('has page title', async ({ page }) => {
// Navigate to webpage
await page.goto('https://www.example.com');
// Get the page title
const pageTitle = page.locator('h1');
// Assert page title
await expect(pageTitle).toContainText('Example Domain');
});
npx playwright test
This will run all the tests in all the browsers. By default, tests run in headless mode.
npx playwright show-report
This will automatically open the HTML report in our browser.
npx playwright test --headed
npx playwright test --project firefox
This will run the test in firefox
.
Available named projects are: chromium
, firefox
, webkit
.
We can also use the --project
flag multiple times to run on multiple browsers.