Playwright
Course Index
Index

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/' }
});