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

# Click an icon

> Log in to a demo store, click the cart icon, and assert that the cart is empty.

This test logs in to a demo store, clicks the shopping cart icon, and asserts that the cart is empty.

## Watch it run

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

## The test

```javascript title="click-icon.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 Image Test", () => {
  it("should click on shopping cart icon and verify empty cart", 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 the shopping cart icon
    await testdriver.focusApplication("Google Chrome");
    const cartIcon = await testdriver.find(
      "shopping cart icon next to the Cart text in the top right corner",
    );

    await cartIcon.click();

    // Assert that you see an empty shopping cart
    const result = await testdriver.assert("Your cart is empty");
    expect(result).toBeTruthy();
  });
});
```

## How it works

* `performLogin()` is a plain function that takes the TestDriver instance, so any test can reuse it.
* The demo store shows its password on screen, so `extract()` reads it.
* `type(password, { secret: true })` keeps the password out of logs.
* `focusApplication('Google Chrome')` brings Chrome to the front before the test uses it.
* The `find()` description names the icon and its position ("next to the Cart text in the top right corner"). This helps the AI pick the right one.

## 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/click-icon.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)
* [extract()](/extract)
* [Reuse code across tests](/reusable-code)
* [Secrets](/secrets)


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