---
title: "Clicking Image Example"
sidebarTitle: "Clicking on an Image"
description: "Example test that finds and clicks on an image element using AI-powered visual recognition."
icon: "hand-pointer"
mode: "wide"
---

## Demo Test Run

Watch this test execute in a real sandbox environment:

{/* hover-image.test.mjs output */}
<iframe 
  src="https://api-test.testdriver.ai/api/v1/testdriver/testcase/6a2b5a35adce118d11db7451/replay" 
  width="100%" 
  height="390" 
  style={{ border: "1px solid #333", borderRadius: "8px" }}
  allow="fullscreen"
/>

## Source Code

```javascript title="hover-image.test.mjs" {40-42,47}
/**
 * TestDriver SDK - Hover Image Test (Vitest)
 * Converted from: testdriver/acceptance/hover-image.yaml
 */

import { describe, expect, it } from "vitest";
import { TestDriver } from "testdriverai/vitest/hooks";

/**
 * Perform login flow for SauceLabs demo app
 * @param {import('../../sdk.js').default} client - TestDriver client 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, { ip: context.ip || process.env.TD_IP});
    
    // provision.chrome() automatically calls ready() and starts dashcam
    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();
  });
});
```

## Running This Example

```bash
# Clone the TestDriver repository
git clone https://github.com/testdriverai/testdriverai

# Install dependencies
cd testdriverai
npm install

# Run this specific example
npx vitest run examples/hover-image.test.mjs
```

<Note>
  Make sure you have `TD_API_KEY` set in your environment. Get one at [testdriver.ai](https://testdriver.ai).
</Note>
