From 9de13b43a11702946724f71bec68554da1157157 Mon Sep 17 00:00:00 2001 From: lino-levan <11367844+lino-levan@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:19:24 -0700 Subject: [PATCH] feat: ElementHandle.innerText/HTML --- src/elementHandle.ts | 56 +++++++++++++++++++++++ tests/__snapshots__/evaluate_test.ts.snap | 18 ++++++++ tests/evaluate_test.ts | 8 +++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/__snapshots__/evaluate_test.ts.snap diff --git a/src/elementHandle.ts b/src/elementHandle.ts index f05ea4b..b4bc7f8 100644 --- a/src/elementHandle.ts +++ b/src/elementHandle.ts @@ -194,6 +194,62 @@ export class ElementHandle { ); } + /** + * Returns the `element.innerHTML` + */ + async innerHTML(): Promise { + return await retryDeadline( + (async () => { + const { object } = await this.#celestial.DOM.resolveNode({ + nodeId: this.#id, + }); + + const result = await this.#celestial.Runtime.callFunctionOn({ + functionDeclaration: "(element)=>element.innerHTML", + objectId: object.objectId, + arguments: [ + { + objectId: object.objectId, + }, + ], + awaitPromise: true, + returnByValue: true, + }); + + return result.result.value; + })(), + this.#page.timeout, + ); + } + + /** + * Returns the `element.innerText` + */ + async innerText() { + return await retryDeadline( + (async () => { + const { object } = await this.#celestial.DOM.resolveNode({ + nodeId: this.#id, + }); + + const result = await this.#celestial.Runtime.callFunctionOn({ + functionDeclaration: "(element)=>element.innerText", + objectId: object.objectId, + arguments: [ + { + objectId: object.objectId, + }, + ], + awaitPromise: true, + returnByValue: true, + }); + + return result.result.value; + })(), + this.#page.timeout, + ); + } + /** * This method scrolls element into view if needed, and then uses `Page.screenshot()` to take a screenshot of the element. */ diff --git a/tests/__snapshots__/evaluate_test.ts.snap b/tests/__snapshots__/evaluate_test.ts.snap new file mode 100644 index 0000000..870a784 --- /dev/null +++ b/tests/__snapshots__/evaluate_test.ts.snap @@ -0,0 +1,18 @@ +export const snapshot = {}; + +snapshot[`Testing evaluate 1`] = ` +' +

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+' +`; + +snapshot[`Testing evaluate 2`] = ` +"Example Domain + +This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission. + +More information..." +`; diff --git a/tests/evaluate_test.ts b/tests/evaluate_test.ts index 0ea572c..7f9e451 100644 --- a/tests/evaluate_test.ts +++ b/tests/evaluate_test.ts @@ -1,8 +1,9 @@ import { assertEquals } from "https://deno.land/std@0.198.0/assert/assert_equals.ts"; +import { assertSnapshot } from "https://deno.land/std@0.198.0/testing/snapshot.ts"; import { launch } from "../mod.ts"; -Deno.test("Testing evaluate", async () => { +Deno.test("Testing evaluate", async (t) => { // Launch browser const browser = await launch(); @@ -17,6 +18,11 @@ Deno.test("Testing evaluate", async () => { }); assertEquals(result, "stringconcat"); + // innerHTML / innerText + const element = (await page.$("div"))!; + assertSnapshot(t, await element.innerHTML()); + assertSnapshot(t, await element.innerText()); + // Close browser await browser.close(); });