Demo Test Run
Watch this test execute in a real sandbox environment:Source Code
match-image.test.mjs
Copy
/**
* TestDriver SDK - Match Image Test
*/
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import { describe, expect, it } from "vitest";
import { TestDriver } from "../lib/vitest/hooks.mjs";
/**
* Perform login flow for SauceLabs demo app
* @param {TestDriver} client - TestDriver client
* @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"]);
}
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
describe("Match Image Test", () => {
it.skip("should match shopping cart image and verify empty cart", async (context) => {
const testdriver = TestDriver(context, { ip: context.ip || process.env.TD_IP, headless: true });
await testdriver.provision.chrome({ url: 'http://testdriver-sandbox.vercel.app/login' });
//
// Perform login first
await performLogin(testdriver);
// Match and click the shopping cart icon
const cartImagePath = path.resolve(
__dirname,
"../../_testdriver/acceptance/screenshots/cart.png",
);
await testdriver.matchImage(cartImagePath, "click");
// Assert that you see an empty shopping cart
const result = await testdriver.assert("Your cart is empty");
expect(result).toBeTruthy();
});
});
Running This Example
Copy
# 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/match-image.test.mjs
Make sure you have
TD_API_KEY set in your environment. Get one at testdriver.ai.
