Skip to content

Commit

Permalink
feat: locator.fill (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
lino-levan authored Jul 25, 2024
1 parent d2cb05c commit 9122439
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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();
});

0 comments on commit 9122439

Please sign in to comment.