-
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.
Merge pull request #31 from lowlighter/feat-support-existing-ws-endpoint
Feat support existing ws endpoint
- Loading branch information
Showing
5 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,45 @@ | ||
--- | ||
title: Connect to existing browser | ||
description: How to connect an existing browser process with astral | ||
index: 3 | ||
--- | ||
|
||
If you already have a browser process running somewhere else or you're using a | ||
service that provides remote browsers for automation (such as | ||
[browserless.io](https://www.browserless.io/)), it is possible to directly | ||
connect to its endpoint rather than spawning a new process. | ||
|
||
## Code | ||
|
||
```ts | ||
// Import Astral | ||
import { launch } from "https://deno.land/x/astral/mod.ts"; | ||
|
||
// Connect to remote endpoint | ||
const browser = await launch({ | ||
wsEndpoint: "wss://remote-browser-endpoint.example.com", | ||
}); | ||
|
||
// Do stuff | ||
const page = await browser.newPage("http://example.com"); | ||
console.log(await page.evaluate(() => document.title)); | ||
|
||
// Close connection | ||
await browser.close(); | ||
``` | ||
|
||
## Reusing a browser spawned by astral | ||
|
||
A browser instance expose its WebSocket endpoint through `browser.wsEndpoint()`. | ||
|
||
```ts | ||
// Spawn a browser process | ||
const browser = await launch(); | ||
|
||
// Connect to first browser instead | ||
const anotherBrowser = await launch({ wsEndpoint: browser.wsEndpoint() }); | ||
``` | ||
|
||
This is especially useful in unit testing as you can setup a shared browser | ||
instance before all your tests, while also properly closing resources to avoid | ||
operations leaks. |
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,35 @@ | ||
import { launch } from "../mod.ts"; | ||
import { assertThrows } from "https://deno.land/[email protected]/assert/assert_throws.ts"; | ||
import { assert } from "https://deno.land/[email protected]/assert/assert.ts"; | ||
|
||
Deno.test("Test existing ws endpoint", async () => { | ||
// Spawn one browser instance and spawn another one connecting to the first one | ||
const a = await launch(); | ||
const b = await launch({ wsEndpoint: a.wsEndpoint() }); | ||
|
||
// Test that second instance works without any process attached | ||
const page = await b.newPage("http://example.com"); | ||
await page.waitForSelector("h1"); | ||
await page.close(); | ||
assert(!b.pages.includes(page)); | ||
|
||
// Close first instance and ensure that b instance is inactive too | ||
await a.close(); | ||
assert(a.closed); | ||
assert(b.closed); | ||
}); | ||
|
||
Deno.test("Ensure pages are properly closed when closing existing endpoint", async () => { | ||
// Spawn one browser instance and spawn another one connecting to the first one | ||
const a = await launch(); | ||
const b = await launch({ wsEndpoint: a.wsEndpoint() }); | ||
|
||
// Ensure closing existing endpoint properly clean resources | ||
await b.newPage("http://example.com"); | ||
await b.newPage("http://example.com"); | ||
await b.close(); | ||
assertThrows(() => b.pages[0].close(), "Page has already been closed"); | ||
assertThrows(() => b.pages[1].close(), "Page has already been closed"); | ||
assert(b.closed); | ||
await a.close(); | ||
}); |