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
|
|
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.
|
|
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
|
|
Then run dep ensure to update Agouti version.
Run the code
|
|
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
|
|
We could still get the title from the Agouti website without starting the 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 and Gomega.
- I also tried another Go package called chromedp but it doesn’t work well.
- Complete example is available at gitlab.com