Basic Playwright Test Structure
Here’s the clean setup using JavaScript with @playwright/test:
// Import Playwright test
const { test, expect } = require('@playwright/test');
// Define a test
test('First Playwright test', async ({ page }) => {
// Step 1: Navigate to a website
await page.goto('https://playwright.dev/');
// Step 2: Check the page title
await expect(page).toHaveTitle(/Playwright/);
// Step 3: Interact with the page (click link)
await page.click('text=Get Started');
// Step 4: Validate navigation
await expect(page).toHaveURL(/.*docs/);
});
Breakdown
- test → Defines a test block.
- page → Represents a single browser tab.
- goto(url) → Opens the given URL.
- expect → Assertions for validation.
click(selector) → Simulates a user click.
Isolated page for this test run.
test(‘Browser Context Playwright test’, async ({page}) =>
{
await page.goto(“http://url”);
});
What is browser context and page fixture in playwright?
General terminology for fixtures is nothing but a global varibale which is available across your project.
- A Page is basically a tab inside a Browser Context.
- When you create a new context, you usually open a new page (tab) inside it
- You can open multiple pages inside the same context if needed.
Browser context declaration
test(‘Browser Context Playwright test’, async ({browser}) =>{
const context= await browser.newContext();
const page= await context.newPage();
await page.goto(“http://url”);
});
Example:
// js
test('Multiple users in same browser instance', async ({ browser }) => {
// User A
const contextA = await browser.newContext();
const pageA = await contextA.newPage();
await pageA.goto('https://example.com');
await pageA.fill('#username', 'UserA');
await pageA.fill('#password', 'PassA');
await pageA.click('button#login');
// User B
const contextB = await browser.newContext();
const pageB = await contextB.newPage();
await pageB.goto('https://example.com');
await pageB.fill('#username', 'UserB');
await pageB.fill('#password', 'PassB');
await pageB.click('button#login');
// Both sessions run in parallel but don’t share cookies/sessions
});
Playwright Essentials: A Step-by-Step Journey into Test Automation

Part 01: Master Playwright Testing – Framework from Scratch Guide

Part 02: How to install Playwright

Part 03: Basic Playwright Test Structure
