How do I use Playwright's test generator to create tests programmatically from external data?


You can dynamically generate and run tests with Playwright without creating .spec files. To do this, use the built-in test generator with the codegen command. Here's how:

  1. Run the test generator: Execute the codegen command followed by the URL of the website you want to generate tests for (e.g., https://ray.run/). The URL is optional, and you can add it directly into the browser window later.
npx playwright codegen https://ray.run/
  1. Record a test: Perform actions in your browser, and Playwright will generate code for your interactions. It will recommend locators based on the rendered page, prioritizing role, text, and test ID locators.

  2. Generate locators: After recording, click the 'Record' button to stop, and the 'Pick Locator' button will appear. Click it, then hover over elements in your browser window to see the locator highlighted. Click on an element to choose a locator, and copy or fine-tune its code.

  3. Emulate specific conditions: Generate tests using emulation to target specific viewports, devices, color schemes, geolocations, languages, or timezones while preserving the authenticated state if needed.

With Playwright, you can create tests interactively and programmatically from external data using the test generator, making it easier for developers to generate tests without manually creating .spec files.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.