Playwright Automation Testing Notes
Playwright Automation Testing Notes
Playwright Automation Testing Notes
Automation testing uses tools and scripts to execute test cases. Unlike manual testing, where a
person interacts with the software, automation testing relies on predefined instructions to
perform the same actions. This saves time, ensures consistency, and is especially useful for
repetitive or complex scenarios.
Why Playwright?
Playwright is a modern end-to-end testing tool that stands out for its features:
2. Setting Up Playwright
Prerequisites
● Node.js: Playwright requires Node.js to run. Install it from the official Node.js website.
● Basic Knowledge of JavaScript/TypeScript: Familiarity with these programming
languages is essential.
Installation Steps
Initialize a Node.js Project: This creates a package.json file to manage dependencies.
npm init -y
1.
Install Playwright: This downloads the Playwright library and necessary browser binaries.
npm install @playwright/test
2.
Run the Test Generator: Playwright provides a code generator to create test scripts
interactively.
npx playwright codegen
3.
Project Structure
project-folder/
|-- tests/
| |-- example.test.js # Contains test scripts
|-- playwright.config.js # Global configurations
|-- package.json # Dependency manager
|-- node_modules/ # Installed modules
3. Playwright Basics
Key Concepts
1. Browser: Represents the browser application (e.g., Chrome, Firefox). It launches and
manages browser instances.
2. Context: An isolated environment within a browser. Each context has its own cookies,
cache, and session storage, making it useful for testing independent sessions.
3. Page: Represents a single tab or window in a browser. Most user interactions are
performed on a Page.
● CSS Selectors: Locate elements by class, ID, or attributes (e.g., .class, #id,
[name="value"]).
● Text Selectors: Match elements containing specific text (e.g., text='Example').
● Custom Selectors: Use locator for more advanced targeting (e.g.,
page.locator('css=selector')).
Actions
Click:
await page.click('button#submit');
●
Type:
await page.type('input#username', 'TestUser');
●
Hover:
await page.hover('div#menu');
●
Assertions
●
Text Content:
await expect(page.locator('h1')).toHaveText('Welcome');
●
Visibility:
await expect(page.locator('button#login')).toBeVisible();
●
await browser.newContext({
viewport: { width: 375, height: 812 },
userAgent: 'Mobile User Agent'
});
Network Interception
Handling Pop-ups
Dialog Boxes: Handle alert, confirm, or prompt dialogs.
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.accept();
});
●
●
● Advantages:
○ Reusability: Shared code between tests.
○ Maintainability: Centralized element updates.
Example Implementation:
class LoginPage {
constructor(page) {
this.page = page;
this.usernameInput = page.locator('#username');
this.passwordInput = page.locator('#password');
this.loginButton = page.locator('#login');
}
module.exports = {
timeout: 30000, // Test timeout
retries: 2, // Retry on failure
use: {
headless: true, // Run tests in headless mode
baseURL: 'https://example.com',
},
};
Parallel Execution
Cloud Testing
Playwright integrates with services like BrowserStack or LambdaTest for cloud-based browser
testing. Update configurations accordingly.
●
●
Reports
GitHub Actions
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- run: npm install
- run: npx playwright install
- run: npx playwright test
This detailed guide ensures a deep understanding of Playwright automation, with clear
explanations for all terms and concepts to prepare for industry-level challenges.