Driving web browser in Go

This article is about driving web browser in Golang.

Agouti

Agouti is an acceptance and testing framework. It could be used together with Ginkgo which is a BDD testing framework and Gomega matcher/assertion library if you are looking for a complete testing solution for your Go project.

The following example only make use of Agouti to drive a browser.

Create the project inside $GOPATH

Create the main.go under $GOPATH/src/gitlab.com/ykyuen/driving-web-browser-in-go-example.

main.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
  "log"

  "github.com/sclevine/agouti"
)

func main() {
  // driver := agouti.PhantomJS()
  driver := agouti.ChromeDriver()
  // driver := agouti.ChromeDriver(
  //   agouti.ChromeOptions("args", []string{"--headless", "--disable-gpu", "--no-sandbox"}),
  // )

  if err := driver.Start(); err != nil {
    log.Fatal("Failed to start driver:", err)
  }

  page, err := driver.NewPage()
  if err != nil {
    log.Fatal("Failed to open page:", err)
  }

  if err := page.Navigate("https://agouti.org/"); err != nil {
    log.Fatal("Failed to navigate:", err)
  }

  sectionTitle, err := page.FindByID(`getting-agouti`).Text()
  log.Println(sectionTitle)

  if err := driver.Stop(); err != nil {
    log.Fatal("Failed to close pages and stop WebDriver:", err)
  }
}

Agouti supports web drivers such as PhantomJS, Selenium and Chrome.

Download the Go dependency

Let’s use dep to manage the Go dependency. Simply run the dep init command.

1
2
3
[[email protected] driving-web-browser-in-go-example]$ dep init
  Using ^2.0.0 as constraint for direct dep github.com/sclevine/agouti
  Locking in v2.0 (b920a9c) for direct dep github.com/sclevine/agouti

At the moment when i am writing this article. The default downloaded version of Agouti by dep is too old. Let’s update it to the latest master manually.

Gopkg.toml

1
2
3
[[constraint]]
  name = "github.com/sclevine/agouti"
  branch = "master"

Then run dep ensure to update Agouti version.

Run the code

1
2
[[email protected] driving-web-browser-in-go-example]$ go run main.go
2018/01/21 19:02:42 Getting Agouti

If you are using ChromeDriver, you should be able to see the Chrome browser will be started automatically and execute the tasks as stated in main.go.

Running in headless browser

We could also run the code in headless browsers like PhantomJS. Chrome also supports headless mode. Let’s update the code as follow.

main.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main

import (
  "log"

  "github.com/sclevine/agouti"
)

func main() {
  // driver := agouti.PhantomJS()
  // driver := agouti.ChromeDriver()
  driver := agouti.ChromeDriver(
    agouti.ChromeOptions("args", []string{"--headless", "--disable-gpu", "--no-sandbox"}),
  )
  ...

We could still get the title from the Agouti website without starting the Chrome window.

Summary