---
title: "Scroll Until Text Test"
sidebarTitle: "Scroll Until Text"
description: "Example test demonstrating how to scroll a page until specific text becomes visible."
icon: "scroll"
mode: "wide"
---

## Demo Test Run

Watch this test execute in a real sandbox environment:

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

## Source Code

```javascript title="scroll-until-text.test.mjs" {41-51}
/**
 * TestDriver SDK - Scroll Until Text Test
 */

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

/**
 * 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"]);
}

describe("Scroll Until Text Test", () => {
  it('should scroll until "testdriver socks" appears', 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);

    // Scroll until text appears
    await testdriver.focusApplication("Google Chrome");

    await testdriver.find('TestDriver.ai Sandbox heading').click();

    // Scroll until text appears
    let found = false;
    let scrollCount = 0;
    const maxScrolls = 10;
    
    while (!found && scrollCount < maxScrolls) {
      const findResult = await testdriver.find("testdriver socks product text is fully visible");

      if (findResult.found()) {
        found = true;
      } else {
        await testdriver.scroll();
        scrollCount++;
      }
    }
    
    if (!found) {
      throw new Error(`Failed to find "testdriver socks" after ${maxScrolls} scrolls`);
    }

    // Assert testdriver socks appears on screen
    await testdriver.focusApplication("Google Chrome");
    const result = await testdriver.assert("TestDriver Socks appears on screen");
    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/scroll-until-text.test.mjs
```

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