-
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.
feat: allow passing args to evaluate
- Loading branch information
1 parent
6bc80c3
commit dfb1f2a
Showing
4 changed files
with
92 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: 2.3 - Evaluate | ||
description: A small example on how to do complex evaluation in Astral | ||
index: 1 | ||
--- | ||
|
||
## Running | ||
|
||
```bash | ||
deno run -A https://deno.land/x/astral/examples/evaluate.ts | ||
``` | ||
|
||
## Code | ||
|
||
```ts | ||
// Import Astral | ||
import { launch } from "https://deno.land/x/astral/mod.ts"; | ||
|
||
// Launch the browser | ||
const browser = await launch(); | ||
|
||
// Open a new page | ||
const page = await browser.newPage("https://deno.land"); | ||
|
||
// Run code in the context of the browser | ||
const value = await page.evaluate(() => { | ||
return document.body.innerHTML; | ||
}); | ||
console.log(value); | ||
|
||
// Run code with args | ||
const result = await page.evaluate((x, y) => { | ||
return `The result of adding ${x}+${y} = ${x + y}`; | ||
}, { | ||
args: [10, 15], | ||
}); | ||
console.log(result); | ||
|
||
// Close the browser | ||
await browser.close(); | ||
``` |
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
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,21 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; | ||
import { launch } from "../mod.ts"; | ||
|
||
Deno.test("Testing evaluate", async () => { | ||
// Launch browser | ||
const browser = await launch(); | ||
|
||
// Open the webpage | ||
const page = await browser.newPage("http://example.com"); | ||
|
||
// passing arguments to evaluate | ||
const result = await page.evaluate((x, y) => { | ||
return x + y; | ||
}, { | ||
args: ["string", "concat"] as const, | ||
}); | ||
assertEquals(result, "stringconcat"); | ||
|
||
// Close browser | ||
await browser.close(); | ||
}); |