Driving web browser in Go
Tags: go, agouti
Published: 2018-03-09 02:02:45

---
title: Driving web browser in Go
published: true
description: "Introduction to Agoutil."
tags: [Golang, Agouti]
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xzoyanqsw83rk2apyf96.png
---

_Originally posted on [Boatswain Blog](https://blog.boatswain.io/post/driving-web-browser-in-go/)._

---

This article is about driving web browser in [Golang][golang].

## Agouti

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

The following example only make use of [Agouti][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_

```go
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][agouti] supports web drivers such as [PhantomJS][phantomjs], [Selenium][selenium] and [Chrome][chrome].

### Download the Go dependency

Let's use [dep][dep] to manage the [Go][golang] dependency. Simply run the _dep init_ command.

```shell
[ykyuen@camus 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][agouti] by [dep][dep] is too old. Let's update it to the latest master manually.

_Gopkg.toml_

```toml
[[constraint]]
  name = "github.com/sclevine/agouti"
  branch = "master"
```

Then run _dep ensure_ to update [Agouti][agouti] version.

### Run the code

```shell
[ykyuen@camus 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][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][phantomjs]. [Chrome][chrome] also supports headless mode. Let's update the code as follow.

_main.go_

```go
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][agouti] website without starting the [Chrome][chrome] window.

## Summary

- This example is not about writing test case but just showing how to drive a web browser.
- For testing, we probably need [Ginkgo][ginkgo] and [Gomega][gomega].
- I also tried another [Go][golang] package called [chromedp][chromedp] but it doesn't work well.
  - [chromedp used as a functional test tool?](https://github.com/chromedp/chromedp/issues/115)
  - [Saving screenshot with headless chrome throws "invalid image size" error](https://github.com/chromedp/chromedp/issues/167)
  - [Click does not work in a specific environment](https://github.com/chromedp/chromedp/issues/70)
- Complete example is available at [gitlab.com](https://gitlab.com/ykyuen/driving-web-browser-in-go-example)





[golang]: https://golang.org/
[agouti]: https://agouti.org/
[ginkgo]: http://onsi.github.io/ginkgo/
[gomega]: http://onsi.github.io/gomega/
[phantomjs]: http://phantomjs.org/
[selenium]: http://www.seleniumhq.org/
[chrome]: https://www.google.com/chrome/
[dep]: https://github.com/golang/dep
[chromedp]: https://github.com/chromedp/chromedp