Writing Your First End-to-End Test in Playwright
In today’s fast-paced development cycles, ensuring your web application works correctly across browsers is crucial. Playwright, an open-source end-to-end (E2E) testing framework by Microsoft, makes this task easier and faster. It allows you to test your web apps across Chromium, Firefox, and WebKit with a single API. In this blog, we’ll walk through the steps to write your first end-to-end test in Playwright.
What is Playwright?
Playwright is a powerful automation tool for testing modern web applications. It supports multiple browsers, headless or headful execution, and is ideal for simulating real user interactions like clicking, typing, and navigating across pages.
Step 1: Installing Playwright
To begin, ensure you have Node.js installed. Then run the following command:
bash
Copy
Edit
npm init playwright@latest
This initializes a new project and installs Playwright with default settings. It also sets up sample tests and configuration files.
Step 2: Create Your First Test
Create a new file called example.spec.js (or .ts if using TypeScript). Here's a simple example:
javascript
Copy
Edit
const { test, expect } = require('@playwright/test');
test('Verify Google search works', async ({ page }) => {
await page.goto('https://www.google.com');
await page.locator('input[name="q"]').fill('Playwright testing');
await page.keyboard.press('Enter');
await page.waitForTimeout(2000);
await expect(page).toHaveTitle(/Playwright testing/i);
});
This test launches a browser, navigates to Google, searches for “Playwright testing,” and verifies the title contains the search term.
Step 3: Run the Test
To execute your test, run:
bash
Copy
Edit
npx playwright test
By default, Playwright runs tests in headless mode. You can add --headed to watch the test in action.
Step 4: View Results
After the test completes, Playwright provides a summary in the terminal. To view detailed results, including screenshots and traces:
bash
Copy
Edit
npx playwright show-report
This opens an interactive HTML report showing the test steps, timings, and assertions.
Conclusion
Playwright offers a modern, reliable, and developer-friendly approach to end-to-end testing. With support for multiple browsers and platforms, easy setup, and rich features like auto-waiting and built-in reporting, it's an excellent tool for automating your UI tests. Writing your first Playwright test is just the beginning—explore its advanced features to take your testing to the next level.
Learn Playwright Training in Hyderabad
Read More:
How Playwright Supports Multiple Browsers
Visit our IHub Talent Training Institute
Comments
Post a Comment