Playwright & playwright-go
Playwright enables reliable end-to-end testing for modern web apps.
- playwright.dev (docs)
- GitHub: microsoft/playwright
- Twitter: @playwrightweb
playwright-go is a Go library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
- GitHub: mxschmitt/playwright-go
- Twitter: @maxibanki
Try Playwright: Getting started with node.js
Visit the playwright getting started documentation to try it out with JavaScript. Here is what your first script might look like:
const { webkit } = require('playwright');
(async () => {
const browser = await webkit.launch();
const page = await browser.newPage();
await page.goto('http://whatsmyuseragent.org/');
await page.screenshot({ path: `example.png` });
await browser.close();
})();
Try playwright-go
playwright-go has some excellent examples that include: Downloading files, End-To-End testing a website, Executing JavaScript in the browser, Emulate mobile and geolocation, Parallel scraping using a WaitGroup, Rendering a PDF of a website, Scraping HackerNews, Take a screenshot, and Record a video.
This is the first example, Scraping HackerNews:
package main
import (
"fmt"
"log"
"github.com/mxschmitt/playwright-go"
)
func main() {
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
browser, err := pw.Chromium.Launch()
if err != nil {
log.Fatalf("could not launch browser: %v", err)
}
page, err := browser.NewPage()
if err != nil {
log.Fatalf("could not create page: %v", err)
}
if _, err = page.Goto("https://news.ycombinator.com"); err != nil {
log.Fatalf("could not goto: %v", err)
}
entries, err := page.QuerySelectorAll(".athing")
if err != nil {
log.Fatalf("could not get entries: %v", err)
}
for i, entry := range entries {
titleElement, err := entry.QuerySelector("td.title > a")
if err != nil {
log.Fatalf("could not get title element: %v", err)
}
title, err := titleElement.TextContent()
if err != nil {
log.Fatalf("could not get text content: %v", err)
}
fmt.Printf("%d: %s\n", i+1, title)
}
if err = browser.Close(); err != nil {
log.Fatalf("could not close browser: %v", err)
}
if err = pw.Stop(); err != nil {
log.Fatalf("could not stop Playwright: %v", err)
}
}