To dynamically generate and run tests without creating .spec files, use Playwright's test generator feature. It's an excellent way to start testing quickly and efficiently.
Run the codegen
command with the URL of the website you want to test (optional). If you don't provide the URL, you can add it directly in the browser window later.
npx playwright codegen https://ray.run/
Interact with the website in the browser window that opens.
Playwright Inspector will record your actions and generate test code.
The test generator prioritizes role, text, and test id locators when examining rendered pages. If multiple elements match a locator, Playwright improves the locator to uniquely identify the target element(s). This reduces test failures due to locator issues.
Here's a TypeScript example of a generated test using @playwright/test:
import { test, expect } from '@playwright/test';
test('Generated test example', async ({ page }) => {
await page.goto('https://ray.run/');
await page.click('text=Sign in');
await page.fill('input[name="email"]', '[email protected]');
await page.fill('input[name="password"]', 'password123');
await page.click('button:has-text("Sign in")');
const welcomeText = await page.innerText('h1');
expect(welcomeText).toBe('Welcome, User!');
});
In summary, Playwright's test generator allows you to dynamically create automated UI tests without manually writing .spec files.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].