> ## 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.

# Add an item to a cart

> Log in to a demo store, add a product to the cart, and assert that it's there.

This test logs in to a demo store, adds a product to the cart, and asserts that the cart has an item.

## Watch it run

[https://api-test.testdriver.ai/api/v1/testdriver/testcase/6ad6b5f0022b6043aeec2beb/replay](https://api-test.testdriver.ai/api/v1/testdriver/testcase/6ad6b5f0022b6043aeec2beb/replay)

## The test

```javascript title="add-to-cart.test.mjs" theme={null}
import { describe, expect, it } from "vitest";
import { TestDriver } from "testdriverai/vitest/hooks";

/**
 * Log in to the TestDriver sandbox demo store.
 * @param {object} client - TestDriver instance
 * @param {string} username - Username (default: 'standard_user')
 */
async function performLogin(client, username = "standard_user") {
  await client.focusApplication("Google Chrome");
  const password = await client.extract("the password");
  const usernameField = await client.find("username input");
  await usernameField.click();
  await client.type(username);
  await client.pressKeys(["tab"]);
  await client.type(password, { secret: true });
  await client.pressKeys(["tab"]);
  await client.pressKeys(["enter"]);
}

describe("Hover Text With Description Test", () => {
  it("should add TestDriver Hat to cart and verify", async (context) => {
    const testdriver = TestDriver(context);
    await testdriver.provision.chrome({
      url: "http://testdriver-sandbox.vercel.app/login",
    });

    // Perform login first
    await performLogin(testdriver);

    // Click on "Add to Cart" under TestDriver Hat
    const addToCartButton = await testdriver.find(
      "Add to Cart, add to cart button under TestDriver Hat",
    );
    await addToCartButton.click();

    // Click on the cart
    const cartButton = await testdriver.find(
      "Cart, cart button in the top right corner",
    );
    await cartButton.click();

    // Assert the TestDriver Hat is in the cart
    const result = await testdriver.assert("There is an item in the cart");
    expect(result).toBeTruthy();
  });
});
```

## How it works

* Each `find()` description starts with the visible label, then adds context ("Add to Cart, add to cart button under TestDriver Hat").
* The context tells the AI which of several identical buttons to pick.
* `performLogin()` is the reusable helper from [Click an icon](/examples/click-icon). It reads the on-screen password with `extract()` and types it with `secret: true`.
* `assert()` checks the result in plain language, not by inspecting markup.

## Run it

The test is in the [TestDriver SDK repository](https://github.com/testdriverai/testdriverai/tree/main/examples). Clone it, install dependencies, and run the file:

```bash theme={null}
git clone https://github.com/testdriverai/testdriverai
cd testdriverai
npm install
npx vitest run examples/add-to-cart.test.mjs
```

The test reads your API key from `TD_API_KEY`. Create a key at [console.testdriver.ai/team/api-keys](https://console.testdriver.ai/team/api-keys). Then set it in your environment or a `.env` file.

## Related

* [find()](/find)
* [click()](/click)
* [Locate elements](/locating-elements)
* [Reuse code across tests](/reusable-code)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.