> ## Documentation Index
> Fetch the complete documentation index at: https://docs.testdriver.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating Tests

> Use the TestDriver agent and MCP to create tests through natural language

With GitHub Copilot and TestDriver's MCP server, you can create tests by chatting with an AI agent. The agent spawns a virtual machine, executes actions, and writes test code for you.

## Start a Conversation

Open GitHub Copilot Chat in VS Code. If your project has no other agents configured, the TestDriver agent is used by default. Otherwise, select **testdriver** from the agent dropdown in the chat panel.

Describe what you want to test:

```
Create a test that logs into my app at https://myapp.com
```

The agent will:

1. Start a new session and spawn a Linux virtual machine
2. Launch Chrome and navigate to your URL
3. Execute actions based on your instructions
4. Write the test code to a `.test.mjs` file

<Note>
  The TestDriver agent appears in the agent selection dropdown if you have other agents configured (like `copilot-instructions.md` or other `.agent.md` files). Make sure to select **testdriver** to use TestDriver's MCP tools.
</Note>

## Live Preview Panel

When the agent starts a session, a **live preview panel** opens in VS Code. This lets you:

* **Watch tests execute** in real-time
* **Interact with the VM** — click, type, and navigate manually
* **Debug issues** — see exactly what the AI sees

<Note>
  The live preview panel requires the TestDriver VS Code extension. Set `TD_PREVIEW=ide` in your MCP configuration to enable it. See the [device configuration docs](/v7/device-config) for all preview options.
</Note>

## Interactive Workflow

The recommended workflow for creating tests:

<Steps>
  <Step title="Describe Your Test">
    Tell the agent what you want to test:

    ```
    Test the checkout flow on https://shop.example.com
    ```
  </Step>

  <Step title="Watch and Guide">
    The agent will start executing. Watch the preview panel and provide guidance if needed:

    ```
    The login modal appeared. Use test@example.com and password123
    ```
  </Step>

  <Step title="Add Assertions">
    Ask the agent to verify specific conditions:

    ```
    Assert that the order confirmation page is shown with the order number
    ```
  </Step>

  <Step title="Review the Code">
    The agent writes test code as it works. Review the generated `.test.mjs` file and make any adjustments.
  </Step>
</Steps>

## Example Conversation

Here's a complete example of creating a login test:

```
You: Create a test for the login flow at https://console.testdriver.ai

Agent: I'll create a test for the login flow. Starting a session...
       [Screenshot: Login page loaded]

       I can see the login page. Let me find the email input.
       [Screenshot: Email input highlighted]

       Now I'll type the email and find the password field...

You: Use test@example.com for the email

Agent: Got it. Typing test@example.com...
       [Screenshot: Email entered]

       Now entering the password and clicking Sign In...
       [Screenshot: Dashboard loaded]

       ✅ Login successful! I've written the test to tests/login.test.mjs
```

## Generated Test Code

The agent generates standard Vitest test files:

```javascript tests/login.test.mjs theme={null}
import { describe, expect, it } from "vitest";
import { TestDriver } from "testdriverai/vitest/hooks";

describe("Login Flow", () => {
  it("should log in successfully", async (context) => {
    const testdriver = TestDriver(context);

    await testdriver.provision.chrome({
      url: "https://console.testdriver.ai"
    });

    const emailInput = await testdriver.find("email input field");
    await emailInput.click();
    await testdriver.type("test@example.com");

    const passwordInput = await testdriver.find("password input field");
    await passwordInput.click();
    await testdriver.type("password123");

    const signInButton = await testdriver.find("Sign In button");
    await signInButton.click();

    const result = await testdriver.assert("dashboard is visible");
    expect(result).toBeTruthy();
  });
});
```

## Tips for Better Tests

<AccordionGroup>
  <Accordion title="Be specific with element descriptions">
    Instead of "click the button", say "click the blue Sign In button in the header". More context helps the AI find the right element.
  </Accordion>

  <Accordion title="Add waits for dynamic content">
    If your app has animations or loading states, tell the agent to wait:

    ```
    Wait for the loading spinner to disappear before continuing
    ```
  </Accordion>

  <Accordion title="Use assertions liberally">
    Add assertions after each major action to catch regressions early:

    ```
    Assert that the product was added to the cart
    ```
  </Accordion>

  <Accordion title="Break complex flows into steps">
    For long workflows, create the test incrementally and verify each step works before moving on.
  </Accordion>
</AccordionGroup>
