Skip to main content

Chromedp

chromedp is a golang package which allows you to automate a Chrome browser with Golang using Puppeteer syntax.

To find out more about this package, please see the chromedp documentation.

Installation

The current chromedp release requires Go 1.26+. Create a module and add chromedp:

go mod init example.com/tbtest
go get github.com/chromedp/chromedp

Your first Test

To get started, please see this simple example:

package main

import (
  "context"
  "flag"
  "log"
  "strings"

  "github.com/chromedp/chromedp"
  "github.com/chromedp/chromedp/remote"
)

func main() {
  var devToolWsUrl string
  flag.StringVar(&devToolWsUrl, "devtools-ws-url", "wss://cloud.testingbot.com?key=API_KEY&secret=API_SECRET&browserName=chrome&browserVersion=latest", "DevTools Websocket URL")
  flag.Parse()

  // NoModifyURL: use the TestingBot URL as-is instead of looking up /json/version
  actxt, cancelActxt := remote.NewAllocator(context.Background(), devToolWsUrl, remote.NoModifyURL)
  defer cancelActxt()

  ctxt, cancelCtxt := chromedp.NewContext(actxt) // create new tab
  defer cancelCtxt()                             // close tab afterwards

  if err := chromedp.Do(ctxt, chromedp.Navigate("https://testingbot.com")); err != nil {
    log.Fatalf("Failed navigating to testingbot.com: %v", err)
  }
  title, err := chromedp.Run(ctxt, chromedp.Title())
  if err != nil {
    log.Fatalf("Failed getting title of testingbot.com: %v", err)
  }
  if !strings.Contains(title, "TestingBot") {
    log.Fatalf("Unexpected title: %q", title)
  }

  log.Println("Got title of:", title)
}

This will connect with a Chrome browser running in the TestingBot cloud, go to the TestingBot homepage, check its title and print it.

Specifying browser and version

To specify on which browser and version your Chromedp test should run, you can include both a browserName and browserVersion in the browserWSEndpoint URL.

Was this page helpful?
Last updated