> For the complete documentation index, see [llms.txt](https://chaoslabz.gitbook.io/viewgraph/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chaoslabz.gitbook.io/viewgraph/tutorials/generate-tests.md).

# Generate Playwright Tests

Create a complete Playwright test file from a single page capture.

## Prerequisites

* ViewGraph extension installed and connected
* A page captured (click ViewGraph icon, then Send to Agent)

## Step 1: Capture the page

Open the page you want to test in Chrome. Click the ViewGraph icon, then **Send to Agent**. This creates a capture with every interactive element, its selectors, and accessibility attributes.

## Step 2: Generate tests

Tell your agent:

```
@vg-tests
```

The agent reads the capture and generates a Playwright test file.

![Kiro generating Playwright tests from a ViewGraph capture](/files/PY9sbN14Ay62eYKC49zT)

## Step 3: Review the generated file

The agent creates a single `.spec.ts` file in your `tests/` directory. Review it - the locators come directly from the capture data, not from guessing.

![Generated Playwright test file with correct locators](/files/jom8e5ZzamZpN3mwqKjk)

Example output:

```typescript
test('login button is visible and clickable', async ({ page }) => {
  const btn = page.getByTestId('login-btn');
  await expect(btn).toBeVisible();
  await expect(btn).toBeEnabled();
});

test('email input accepts text', async ({ page }) => {
  const email = page.getByLabel('Email address');
  await expect(email).toBeVisible();
  await email.fill('test@example.com');
});
```

## Step 4: Run the tests

```bash
npx playwright test tests/your-page.spec.ts
```

## Capture during tests

For ongoing regression detection, add ViewGraph captures to your existing tests:

```typescript
import { test } from '@viewgraph/playwright/fixture';

test('checkout flow', async ({ page, viewgraph }) => {
  await page.goto('/checkout');
  await viewgraph.capture('checkout-page');
  // ... your test assertions
});
```

Captures from test runs land in `.viewgraph/captures/` where the agent can diff them between runs.

## What the agent uses

From the capture, the agent calls:

* `get_interactive_elements` - all buttons, links, inputs with ranked locators
* `get_page_summary` - page title, element counts, structure
* `get_capture` - full details when needed for complex assertions


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://chaoslabz.gitbook.io/viewgraph/tutorials/generate-tests.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
