-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9816187
commit 414f92d
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { deadline } from "https://deno.land/[email protected]/async/deadline.ts"; | ||
|
||
import { Celestial } from "../bindings/celestial.ts"; | ||
import { KeyboardTypeOptions } from "./keyboard.ts"; | ||
import { Page, ScreenshotOptions } from "./page.ts"; | ||
|
@@ -228,4 +230,28 @@ export class ElementHandle { | |
await this.focus(); | ||
await this.#page.keyboard.type(text, opts); | ||
} | ||
|
||
/** | ||
* Wait for an element matching the given selector to appear in the current element. | ||
*/ | ||
async waitForSelector(selector: string) { | ||
// TODO(lino-levan): Make this easier to read, it's a little scuffed | ||
return await deadline<ElementHandle>( | ||
(async () => { | ||
while (true) { | ||
const result = await this.#celestial.DOM.querySelector({ | ||
nodeId: this.#id, | ||
selector, | ||
}); | ||
|
||
if (!result) { | ||
continue; | ||
} | ||
|
||
return new ElementHandle(result.nodeId, this.#celestial, this.#page); | ||
} | ||
})(), | ||
this.#page.timeout, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { launch } from "../mod.ts"; | ||
|
||
Deno.test("Wait for selector", async () => { | ||
// Launch browser | ||
const browser = await launch(); | ||
|
||
// Open the webpage | ||
const page = await browser.newPage("http://deno.land"); | ||
|
||
// Wait for selector | ||
const selected = await page.waitForSelector(".font-bold"); | ||
console.log(selected); | ||
|
||
// Close browser | ||
await browser.close(); | ||
}); |