Playwright Basics
Installation
# Install Playwright
npm init playwright@latest
# Install Playwright in existing project
npm install @playwright/test
# Install Browsers
npx playwright install
Folder SructureStructure
π your-playwright-project/
βββ π tests/ # Main folder for test cases
β βββ π e2e/ # End-to-End (UI) tests
β β βββ login.spec.js # Example test case
β β βββ dashboard.spec.js
β β βββ checkout.spec.js
β βββ π api/ # API testing scripts
β β βββ apiTest.spec.js
β βββ π components/ # Reusable component tests (if applicable)
β β βββ modal.spec.js
β βββ π helpers/ # Utility functions (e.g., custom locators)
β βββ utils.js
βββ π fixtures/ # Shared test setup (e.g., user auth, config)
β βββ auth.js
βββ π reports/ # Test reports (after execution)
β βββ playwright-report/ # Auto-generated HTML reports
βββ π screenshots/ # Screenshots for debugging
β βββ error-screenshot.png
βββ π videos/ # Video recordings (if enabled)
β βββ test-video.mp4
βββ .github/ # GitHub Actions (CI/CD configuration)
β βββ workflows/
β βββ playwright.yml
βββ package.json # Node.js dependencies & scripts
βββ playwright.config.js # Main Playwright configuration
βββ README.md # Project documentation
Run Commands
Run all tests
npx playwright test
View test report
npx playwright show-report
Run tests in a specific folder
npx playwright test tests/e2e/
Launch Browser
Javascript
Copy
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://example.com'); await browser.close(); })();
Browser Contexts
Javascript
Copy
const context = await browser.newContext({ viewport: { width: 1280, height: 720 }, locale: 'en-US', geolocation: { latitude: 37.7749, longitude: -122.4194 }, permissions: ['geolocation'] });
Page Actions
javascript
Copy
await page.goto('https://example.com'); await page.fill('#username', 'myUser'); await page.click('button#submit'); await page.screenshot({ path: 'screenshot.png' });
Selectors
Javascript
Copy
await page.locator('text="Sign In"').click(); await page.locator('button >> nth=1').click(); await page.locator('#inputId').fill('value');
Common Selector Types:
- CSS: 'button.submit'
- Text: text='Submit'
- XPath: xpath=//button[@id="submit"]
- Role: role=button[name="Submit"]
Assertions
Javascript
Copy
const { expect } = require('@playwright/test'); await expect(page).toHaveTitle('Example Domain'); await expect(page.locator('#success')).toBeVisible(); await expect(page.locator('#output')).toHaveText('Hello');
Waiting Mechanisms
javascript
Copy
await page.waitForSelector('#element', { timeout: 5000 }); await page.waitForLoadState('networkidle'); await page.waitForTimeout(3000); // Static wait
Handling Frames
javascript
Copy
const frame = page.frame({ name: 'iframeName' }); await frame.click('button.submit');
File Upload & Download
javascript
Copy
// Upload a file await page.setInputFiles('input[type="file"]', 'path/to/file.txt'); // Download a file const [download] = await Promise.all([ page.waitForEvent('download'), page.click('#download') ]); await download.saveAs('downloadedFile.txt');
Multi-Tab (New Window)
javascript
Copy
const [newPage] = await Promise.all([ context.waitForEvent('page'), page.click('a[target="_blank"]') ]); await newPage.waitForLoadState();
Screenshots & Videos
Javascript
Copy
await page.screenshot({ path: 'screenshot.png', fullPage: true }); // Record Video (Set up context) const context = await browser.newContext({ recordVideo: { dir: 'videos/' } });