Go Testing with chromedp
chromedp is a fast and simple way to drive browsers supporting the Chrome DevTools Protocol in Go.
Installing chromedp
To install chromedp, please run the following command:
go get -u github.com/chromedp/chromedp
Running your first chromedp test
To run your first test, please use this example:
package main
import (
"context"
"flag"
"github.com/chromedp/chromedp"
"log"
)
func main() {
var devToolWsUrl string
var title string
flag.StringVar(&devToolWsUrl, "devtools-ws-url", "wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]", "DevTools Websocket URL")
flag.Parse()
actxt, cancelActxt := chromedp.NewRemoteAllocator(context.Background(), devToolWsUrl)
defer cancelActxt()
ctxt, cancelCtxt := chromedp.NewContext(actxt) // create new tab
defer cancelCtxt() // close tab afterwards
if err := chromedp.Run(ctxt,
chromedp.Navigate("https://headlesstesting.com"),
chromedp.Title(&title),
); err != nil {
log.Fatalf("Failed getting body of headlesstesting.com: %v", err)
}
log.Println("Got title of:", title)
}
This example will start a Chrome Headless Browser, navigate to HeadlessTesting.com and print the title of the page.