In this guide, you’ll setup your TestDriver account, create a new Playwright project, and leverage TestDriver’s AI to convert tests to natural language.
// Afterimport { test } from "@testdriver.ai/playwright";test.describe("get started link", () => { test.beforeEach(async ({ page }) => page.goto("https://playwright.dev/")); test.agent(` - Click the 'Get started' link - Verify the 'Installation' heading is visible `);});
✔ Do you want to use TypeScript or JavaScript?> TypeScript✔ Where to put your end-to-end tests?> tests✔ Add a GitHub Actions workflow? (y/N)> N✔ Install Playwright browsers (can be done manually via 'npx playwright install')? (Y/n)> Y
@testdriver.ai/playwright as a backwards-compatible wrapper around @playwright/test:
npm
yarn
pnpm
Copy
npm install @testdriver.ai/playwright
Copy
yarn add @testdriver.ai/playwright
Copy
pnpm add @testdriver.ai/playwright
2
Run Playwright
Before we start using TestDriver in our tests, run Playwright in UI Mode:
npm
yarn
pnpm
Copy
npx playwright test --ui
Copy
yarn playwright test --ui
Copy
pnpm exec playwright test --ui
Clicking the ▶️ button should successfully run the tests in the UI,
just as they did before with playwright test in the CLI.
3
Import TestDriver
For the sake of simplicity, we’ll be working with one test file for now.Open tests/example.spec.ts in your editor & rename the @playwright/test
import to @testdriver.ai/playwright:
tests/example.spec.ts
Copy
import { test, expect } from '@playwright/test'; import { test, expect } from '@testdriver.ai/playwright';
Click the button to run the test and verify everything still works.
Click the button to automatically re-run tests on save.
Because TestDriver uses AI vision instead of selectors, we can use natural language for
assertions,
locating,
performing actions,
or even having an agent test for you!
Replace toBeVisible with toMatchPrompt to assert that the element is visible on the screen:
tests/example.spec.ts
Copy
// Expects page to have a heading with the name of Installation.await expect(page.getByRole("heading", { name: "Installation" })).toBeVisible();await expect(page).toMatchPrompt("'Installation' heading is visible");
Before, the test needed code comments to describe what the assertion is actually checking.With toMatchPrompt, natural language acts as a description, selector, and assertion in one
TestDriver can reduce the amount of complexity in your tests, but you can
still “opt-in” to Playwright assertions and selectors if you need to (e.g.
validating accessibility with page.getByRole).
TestDriver can replace data-testids, getByRole, and CSS selectors with natural language.First, update your test to get access to the testdriver fixture:
tests/example.spec.ts
Copy
test('get started link', async ({ page, }) => {test('get started link', async ({ page, testdriver }) => {
Then, replace getByRole with testdriver.locate:
tests/example.spec.ts
Copy
test('get started link', async ({ page, testdriver }) => { await page.goto('https://playwright.dev/'); // Click the get started link. await page.getByRole("link", { name: "Get started" }).click(); const link = await testdriver(page).locate("Get started link"); await link.click(); await expect(page).toMatchPrompt("'Installation' heading is visible");
Now, our test uses natural language to both describe & locate the element.
In the example above, you can still use Playwright to assert that the element is indeed a link for accessibility:
tests/example.spec.ts
Copy
const link = await testdriver(page).locate("Get started link");expect(link).toHaveRole("link");await link.click();
This way you can write user-centric tests and validate the implementation.
TestDriver can automatically perform the entire test for you with an AI agent:
tests/example.spec.ts
Copy
test("get started link", async ({ page, testdriver }) => { await page.goto("https://playwright.dev/"); await testdriver(page).act("Click the 'Get started' link"); await expect(page).toMatchPrompt("'Installation' heading is visible");});test.describe("get started link", () => { test.beforeEach(async ({ page }) => page.goto("https://playwright.dev/")); test.agent(` - Click the 'Get started' link - Verify the 'Installation' heading is visible `);});
Instead of writing the test implementation, we’ve used test.describe to describe the test still,
but replaced the test itself with test.agent.
Use test.beforeEach to prepare the page for the agent (e.g.
page.goto, calling
an API to create a user). Use
test.afterEach
to clean up after the agent (e.g. page.close) or perform additional logic
(e.g. clearing the session).
With @testdriver.ai/playwright, you can use as much or as little of Playwright’s or TestDriver’s API as you need to validate correctness. It’s up to you!