In this section, we will delve into the core concepts of Playwright, a powerful tool for automating web applications. Understanding these concepts is crucial for writing effective and efficient tests. We will cover the following key areas:

  1. Playwright Architecture
  2. Browser Contexts
  3. Pages
  4. Selectors
  5. Actions and Assertions

  1. Playwright Architecture

Playwright is designed to automate browsers using a high-level API. It supports multiple browsers, including Chromium, Firefox, and WebKit. Here's a breakdown of its architecture:

  • Playwright Server: Manages browser instances and communicates with them.
  • Playwright Client: Your test scripts that interact with the Playwright server to perform actions on the browser.

Code Example: Basic Playwright Script

import { chromium } from 'playwright';

(async () => {
  // Launch a browser instance
  const browser = await chromium.launch();
  // Create a new browser context
  const context = await browser.newContext();
  // Open a new page
  const page = await context.newPage();
  // Navigate to a website
  await page.goto('https://example.com');
  // Close the browser
  await browser.close();
})();

Explanation:

  • We import the chromium module from Playwright.
  • We launch a new browser instance.
  • We create a new browser context, which is an isolated environment for pages.
  • We open a new page and navigate to a URL.
  • Finally, we close the browser.

  1. Browser Contexts

Browser contexts allow you to create isolated environments within a single browser instance. This is useful for running tests in parallel without interference.

  • Isolation: Each context has its own cookies, cache, and storage.
  • Efficiency: Multiple contexts can run in a single browser instance, reducing resource usage.

  1. Pages

Pages represent individual tabs or windows in a browser. They are the primary interface for interacting with web content.

  • Navigation: Use page.goto(url) to navigate to a specific URL.
  • Interaction: Perform actions like clicking, typing, and scrolling.

  1. Selectors

Selectors are used to identify elements on a web page. Playwright supports various selector types:

  • CSS Selectors: Standard CSS selectors.
  • Text Selectors: Select elements based on their text content.
  • XPath Selectors: Use XPath expressions to locate elements.

Code Example: Using Selectors

await page.click('text="More information"');
await page.fill('input[name="username"]', 'myUsername');

Explanation:

  • We use a text selector to click a link with the text "More information".
  • We use a CSS selector to fill an input field with the name "username".

  1. Actions and Assertions

Actions are operations performed on web elements, such as clicking or typing. Assertions are used to verify the state of the application.

  • Actions: page.click(), page.fill(), page.hover(), etc.
  • Assertions: Use Playwright's built-in assertions to verify conditions.

Code Example: Actions and Assertions

await page.click('button#submit');
await expect(page).toHaveURL('https://example.com/dashboard');

Explanation:

  • We click a button with the ID "submit".
  • We assert that the page URL changes to the expected dashboard URL.

Practical Exercise

Task: Write a Playwright script that navigates to a website, fills out a form, and submits it. Verify that the submission was successful by checking the URL or a success message.

Solution

import { chromium, expect } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  
  await page.goto('https://example.com/form');
  await page.fill('input[name="firstName"]', 'John');
  await page.fill('input[name="lastName"]', 'Doe');
  await page.click('button[type="submit"]');
  
  await expect(page).toHaveURL('https://example.com/form-success');
  
  await browser.close();
})();

Explanation:

  • We navigate to a form page and fill out the first and last name fields.
  • We submit the form and assert that the URL changes to indicate a successful submission.

Common Mistakes:

  • Forgetting to await asynchronous operations.
  • Using incorrect selectors that do not match any elements.

Conclusion

In this section, we explored the core concepts of Playwright, including its architecture, browser contexts, pages, selectors, and actions/assertions. Understanding these concepts is essential for writing robust and efficient tests. In the next section, we will learn how to run Playwright tests effectively.

© Copyright 2024. All rights reserved