First Playwright Program
Let’s write our very first Playwright test case to launch an application.
Code Example
// Import Playwright test library
import { test } from '@playwright/test';
// Define a test case with title
test('Launch Application', async ({ page }) => {
// Step 1: Navigate to the application URL
await page.goto('https://vinothqaacademy.com/demo-site/');
// Step 2: Wait for 3 seconds (just to see the page launch during demo)
await page.waitForTimeout(3000);
// Step 3: Close the page (browser context auto-closes after test)
});
Explanation
- import { test } from '@playwright/test'
→ imports Playwright’s built-in test runner.
– test('Launch Application', async ({ page }) => { ... })
→ defines a test case with the name Launch Application.
– page.goto(URL)
→ opens the browser and navigates to the given application URL.
– page.waitForTimeout(3000)
→ makes the browser wait for 3 seconds (just for demo purposes).
– Browser context and page auto-close after the test run, no need to close manually.
Behind the Scenes
– Playwright automatically launches a browser context for each test.
– All Playwright functions are asynchronous, which is why we use async/await
to run steps in order.
– waitForTimeout()
is used here only for demo. In real automation, prefer auto-waiting or waitForSelector()
for stability.
What You’ll See
– A browser window opens.
– The demo site loads.
– The page stays open for 3 seconds.
– The browser closes automatically.
Try It Yourself
– Change the URL to https://playwright.dev/
and rerun the test.
– Modify the timeout value (e.g., 1000
ms = 1 second).
– Remove waitForTimeout()
and notice how quickly the test finishes