Skip to content

Commit

Permalink
Merge pull request #9 from lino-levan/feat-innertext
Browse files Browse the repository at this point in the history
feat: ElementHandle.innerText/HTML
  • Loading branch information
lino-levan authored Aug 23, 2023
2 parents 65b3f80 + 9de13b4 commit 2e9b5be
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/elementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,62 @@ export class ElementHandle {
);
}

/**
* Returns the `element.innerHTML`
*/
async innerHTML(): Promise<string> {
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.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/__snapshots__/evaluate_test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const snapshot = {};

snapshot[`Testing evaluate 1`] = `
'
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
'
`;

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..."
`;
8 changes: 7 additions & 1 deletion tests/evaluate_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { assertSnapshot } from "https://deno.land/[email protected]/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();

Expand All @@ -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();
});

0 comments on commit 2e9b5be

Please sign in to comment.