Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: locator.fill #81

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ export class Locator<T> {
await handle.click();
}

async fill(text: string) {
await retry(async () => {
const p = this.#fill(text);
await deadline(p, this.#timeout);
});
}

async #fill(text: string) {
await this.#page.waitForSelector(this.#selector);
const handle = await this.#page.$(this.#selector);
if (handle === null) {
throw new Error(`Selector "${this.#selector}" not found.`);
}

await handle.type(text);
}

async wait(): Promise<ElementHandle> {
return await this.#page.waitForSelector(this.#selector);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/fill.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fill</title>
</head>
<body>
<input id="target"></input>
</body>
</html>
18 changes: 18 additions & 0 deletions tests/locator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ Deno.test("Locator - evaluate()", async () => {
await page.close();
await browser.close();
});

Deno.test("Locator - fill()", async () => {
await using server = await serveFixture("fixtures/fill.html");

const browser = await launch();
const page = await browser.newPage(server.address);
await page.waitForNetworkIdle();

await page.locator("#target").fill("hello");

const text = await page.locator<HTMLInputElement>("#target").evaluate((el) =>
el.value
);
assertEquals(text, "hello");

await page.close();
await browser.close();
});
Loading