Skip to content

Commit

Permalink
Merge pull request #17 from lowlighter/feat-page-set-content
Browse files Browse the repository at this point in the history
feat: add `page.setContent`
  • Loading branch information
lino-levan authored Oct 9, 2023
2 parents 8abb9af + 676a04f commit 3a1ec6a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ export class Page extends EventTarget {
return result.value;
}

/**
* Set page content
*/
async setContent(content: string): Promise<void> {
await this.evaluate(
(html) => {
const { document } = globalThis as unknown as {
document: {
open: () => void;
write: (html: string) => void;
close: () => void;
};
};
document.open();
document.write(html);
document.close();
},
{ args: [content] },
);
}

/**
* If no URLs are specified, this method returns cookies for the current page URL. If URLs are specified, only cookies for those URLs are returned.
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/set_content_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { assertStrictEquals } from "https://deno.land/[email protected]/assert/assert_strict_equals.ts";
import { launch } from "../mod.ts";

Deno.test("Set content", async () => {
// Launch browser
const browser = await launch();
const content = `
<!DOCTYPE html>
<html>
<head>
<title>Astral</title>
</head>
<body>
<span>Hello world</span>
</body>
</html>`;

// Open the webpage and set content
const page = await browser.newPage();
await page.setContent(content);

// Wait for selector
assertStrictEquals(
content.replace(/\s/g, ""),
`${await page.content()}`.replace(/\s/g, ""),
);
const selected = await page.waitForSelector("span");
assertStrictEquals(await selected.innerText(), "Hello world");

// Close browser
await browser.close();
});

0 comments on commit 3a1ec6a

Please sign in to comment.