×

Introdcution

In the web technology field ,Web automation has become highly essential for developers and testers. Playwright is the go-to choice for modern web automation needs. This article guides you to understand the basics of Playwright and how to get started with it.

Playwright

Playwright is a powerful open-source Microsoft automation framework which is used to automate browsers using a single API. It supports end-to-end testing and major browsers, including:

  • Google Chrome
  • Microsoft Edge
  • Safari
  • Firefox

Top Reasons To Choose Playwright

Playwright is an ideal choice for web automation across multiple browsers with a single codebase. It runs tests in different modes, like headless mode for speed and in headful mode to observe interactions.

Playwright has a rich ecosystem that offers built-in test runners, tracing tools, and easy integration. It simulates devices, geolocations, and network conditions.

The Powerful selectors ensure built-in support for XPath, CSS selectors, and text-based locators. Playwright follows elements to wait to be actionable before performing interactions automatically.

Installation of Playwright

To install Playwright using npm or yarn:

npm init playwright@latest

This command initializes a new Playwright project with examples, configuration files, and Playwright Test Runner.

Writing the First Playwright Script

Step 1: Creating a New Script

Create a file named example.js and open it in the editor.

Step 2: Writing the JavaScript to automate a search on Google:

const { chromium } = require(‘playwright’); (async () => { const browser = await chromium.launch({ headless: false }); const context = await browser.newContext(); const page = await context.newPage(); await page.goto(‘https://www. google.com’); await page.fill(‘input[name=”q”]’, ‘Playwright automation’); await page.press(‘input[name=”q”]’, ‘Enter’); await page.waitForTimeout(2000); await page.screenshot({ path: ‘screenshot.png’ }); await browser.close(); })();

Step 3: Run the Script

Run the script using Node.js:

node example.js

The script will open Google, perform a search, and take a screenshot.

Advanced Features of Playwright

The advanced level features of Playwright are:

  • Parallel Testing
  • Tracing
  • Network Interception

Parallel Testing

Playwright supports parallel test execution that allows you to speed up the test cycles by running multiple instances simultaneously.

npx playwright test –workers=4

Tracing

Playwright provides a tracing feature to debug flaky tests. To enable tracing, follow the below code,

<pre> await context.tracing.start({ screenshots: true, snapshots: true }); await context.tracing.stop({ path: 'trace.zip' }); </pre>

Network Interception

Intercept and mock API requests and responses for testing.

<pre> await page.route('**/api/**', (route) => { route.fulfill({ body: JSON.stringify({ success: true }) }); }); </pre>

Best Practices of Playwright

Leverage the built-in Playwright Test Runner for better structure and reporting. Modularize your code by breaking the tests into reusable functions for maintainability. Add explicit assertions to validate test outcomes. Use –debug mode or add await page.pause() to debug issues interactively.

Conclusion

Finally, Playwright is a powerful and versatile framework for web automation. It offers unparalleled browser compatibility and developer-friendly features.

Playwright provides a solid foundation to automate functional tests and to streamline repetitive tasks. Credo Systemz Playwright course offers a deep dive into Playwright and to explore advanced topics like CI/CD integration, API testing, and more.

Join Credo Systemz Software Courses in Chennai at Credo Systemz OMR, Credo Systemz Velachery to kick-start or uplift your career path.