Parametrization in PlayWright
Test Uptime Status of Multiple Sites
Yesterday, I showed how to use Parametrization in Pytest. Here's an example of how you would run that same code in PlayWright with TypeScript:
This code checks to make sure the four websites are up and running. This is just a quick sanity test, it doesn't do any critical path testing.
import { test, expect } from '@playwright/test';
// List of websites to test
const WEBSITES = [
"https://www.company.com",
"https://qa1.company.com",
"https://qa2.company.com",
"https://stage.company.com",
];
// Configure Playwright to run in headless mode globally
test.use({ headless: true });
test(`Check Websites Status`, async ({ page }) => {
// Iterate over websites to create a test for each
for (const website of WEBSITES) {
test(`Check if ${website} is up and running`, async ({ page }) => {
try {
// Attempt to load the website
await page.goto(website, { waitUntil: 'domcontentloaded' });
// Check if page title exists and is not empty
const title = await page.title();
expect(title).not.toBe('');
// Check if body element exists
const body = page.locator('body');
await expect(body).toBeVisible();
// Log success
console.log(`? ${website} is up and running (Title: ${title})`);
} catch (error) {
// Oh the Horror: Fail the test with a detailed message
throw new Error(`Website ${website} failed: ${error.message}`);
}
});
}
})