GyaaniGuy

- self made 💻 😎

Automation with playwright - Part 1 Setup and Basics

Install Playwright

Simple example

Playwright has 2 ‘modes’

  • regular mode: where you have fine grained control over the browser.
  • tests: Useful for running test case scenarios.

Regular mode

import { chromium } from '@playwright/test';

browser = await chromium.launch({ headless });
context = await browser.newContext();
page = await context.newPage();

// Can create multiple pages
page2 = await context.newPage();

// 5. Use each page independently
await page.goto('https://example.com');
await page2.goto('https://playwright.dev');
  
await browser.close();

wait for elements to exist

These are vital in javascript pages or after ajax requests.

// wait using selector to exist for 30 seconds
await page.waitForSelector('//li');
// 5 seconds
await page.waitForSelector('//li', { timeout: 5000 }); 

wait for time

// wait using selector to exist for 3 seconds
await page.waitForTimeout(3000);

Please see rest of the series to know how to select and interact with page elements


dated April 2025