Skip to content

Instantly share code, notes, and snippets.

@jimmont
Created August 6, 2024 12:26
Show Gist options
  • Save jimmont/d70845b0fc7e673b1676fb08dd70d025 to your computer and use it in GitHub Desktop.
Save jimmont/d70845b0fc7e673b1676fb08dd70d025 to your computer and use it in GitHub Desktop.
browser ui testing, scraping, etc
// astral alternative to puppeteer and playwright, similar API
// run: deno run -A ./uitest.js
// more at https://github.com/lino-levan/astral
import { launch } from "jsr:@astral/astral";
const browser = await launch({
headless: true,
// note will attempt to auto-install... to use local options use the path value
// macos
// path: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
// debian: sudo apt install chromium-browser chromium-codecs-ffmpeg
// path: '/usr/lib/chromium-browser/chromium-browser'
});
const page = await browser.newPage("https://deno.com");
const screenshot = await page.screenshot();
Deno.writeFileSync("screenshot.png", screenshot);
await browser.close();
@jimmont
Copy link
Author

jimmont commented May 11, 2025

for Raspberry Pi ARM Linux lino-levan/astral#51 (comment)

$ sudo apt install chromium-browser chromium-codecs-ffmpeg
$ which chromium
/usr/bin/chromium
...
const browser = await launch({
  // debian/raspberry pi os specific path option
  path: '/usr/bin/chromium',
  args: ['--remote-debugging-port=9222', '--headless=new', '--no-first-run', '--password-store=basic', '--use-mock-keychain', '--hide-scrollbars', '--no-sandbox']
 });
...

@jimmont
Copy link
Author

jimmont commented May 11, 2025

https://github.com/lino-levan/astral
https://jsr.io/@astral/astral

/* basic working example for a UI test with macOS specific path
  * deno test -A ./page-content-test.js
  * see also https://docs.deno.com/runtime/fundamentals/testing/
  * */
 import { launch } from "jsr:@astral/astral";
 import { assert } from "jsr:@std/assert";

 const browser = await launch({
   // macOS specific path option
   path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
   args: ['--remote-debugging-port=9222', '--headless=new', '--no-first-run', '--password-store=basic', '--use-mock-keychain', '--hide-scrollbars', '--no-sandbox']
 });

 const page = await browser.newPage("https://deno.com");

 // Run code in the context of the browser
 const text = await page.evaluate(() => {
   return document.body.querySelector('main h1')?.textContent ?? '';
 });

 assert(text.includes('JavaScript'), 'has JavaScript');

 await browser.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment