Skip to content

Commit

Permalink
Merge branch 'topic/headless-browser'
Browse files Browse the repository at this point in the history
  • Loading branch information
minusnine committed Aug 5, 2019
2 parents e6fd2ac + 620f263 commit 004f2b0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 53 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ go:
- 1.12.x

jdk:
# The Java 8 SDK is a requirement for Selenium.
- openjdk8
# The Java JRE is a requirement for Selenium and HTMLUnit.
- openjdk11

before_script:
# Start an X frame buffer in which to run the web browsers.
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
# give xvfb some time to start
- sleep 3
# Download all of the binary dependencies needed to run the tests.
- cd vendor && go run init.go --alsologtostderr --download_browsers --download_latest && cd ..

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ No longer supported.
3. Firefox via Geckodriver doesn't handle sending control characters
[without appending a terminating null key](https://github.com/mozilla/geckodriver/issues/665).

### Chromedriver

1. [Headless Chrome does not support running extensions](https://crbug.com/706008).

## Breaking Changes

There are a number of upcoming changes that break backward compatibility in an
Expand Down
106 changes: 64 additions & 42 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
"time"

"github.com/armon/go-socks5"
socks5 "github.com/armon/go-socks5"
"github.com/blang/semver"
"github.com/golang/glog"
"github.com/google/go-cmp/cmp"
Expand All @@ -44,7 +44,8 @@ var (
useDocker = flag.Bool("docker", false, "If set, run the tests in a Docker container.")
runningUnderDocker = flag.Bool("running_under_docker", false, "This is set by the Docker test harness and should not be needed otherwise.")

startFrameBuffer = flag.Bool("start_frame_buffer", true, "If true, start an Xvfb subprocess and run the browsers in that X server.")
startFrameBuffer = flag.Bool("start_frame_buffer", false, "If true, start an Xvfb subprocess and run the browsers in that X server.")
headless = flag.Bool("headless", true, "If true, run Chrome and Firefox in headless mode, not requiring a frame buffer.")

serverURL string
)
Expand Down Expand Up @@ -146,6 +147,23 @@ func TestChrome(t *testing.T) {
t.Skipf("Skipping Chrome tests because ChromeDriver not found at path %q", *chromeDriverPath)
}

t.Run("Chromedriver", func(t *testing.T) {
runChromeTests(t, config{
path: *chromeBinary,
})
})

t.Run("Selenium3", func(t *testing.T) {
runChromeTests(t, config{
path: *chromeBinary,
seleniumVersion: semver.MustParse("3.0.0"),
})
})
}

func runChromeTests(t *testing.T, c config) {
c.browser = "chrome"

var opts []ServiceOption
if *startFrameBuffer {
opts = append(opts, StartFrameBuffer())
Expand All @@ -159,15 +177,17 @@ func TestChrome(t *testing.T) {
if err != nil {
t.Fatalf("pickUnusedPort() returned error: %v", err)
}
c.addr = fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port)

s, err := NewChromeDriverService(*chromeDriverPath, port, opts...)
if err != nil {
t.Fatalf("Error starting the ChromeDriver server: %v", err)
var s *Service
if c.seleniumVersion.Major == 3 {
c.serviceOptions = append(c.serviceOptions, ChromeDriver(*chromeDriverPath))
s, err = NewSeleniumService(*selenium3Path, port, c.serviceOptions...)
} else {
s, err = NewChromeDriverService(*chromeDriverPath, port, c.serviceOptions...)
}
c := config{
addr: fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port),
browser: "chrome",
path: *chromeBinary,
if err != nil {
t.Fatalf("Error starting the server: %v", err)
}

runTests(t, c)
Expand Down Expand Up @@ -195,6 +215,11 @@ func testChromeExtension(t *testing.T, c config) {
}
defer wd.Quit()

if *headless {
// https://crbug.com/706008
t.Skip("Chrome does not support extensions in headless mode.")
}

if err := wd.Get(serverURL); err != nil {
t.Fatalf("wd.Get(%q) returned error: %v", serverURL, err)
}
Expand All @@ -215,34 +240,33 @@ func testChromeExtension(t *testing.T, c config) {
}
}

func TestFirefoxSelenium3(t *testing.T) {
func TestFirefox(t *testing.T) {
if *useDocker {
t.Skip("Skipping tests because they will be run under a Docker container")
}
if _, err := os.Stat(*selenium3Path); err != nil {
t.Skipf("Skipping Firefox tests using Selenium 3 because Selenium WebDriver JAR not found at path %q", *selenium3Path)
}
if _, err := os.Stat(*geckoDriverPath); err != nil {
t.Skipf("Skipping Firefox tests on Selenium 3 because geckodriver binary %q not found", *geckoDriverPath)
}

runFirefoxTests(t, *selenium3Path, config{
seleniumVersion: semver.MustParse("3.0.0"),
serviceOptions: []ServiceOption{GeckoDriver(*geckoDriverPath)},
path: *firefoxBinarySelenium3,
})
}

func TestFirefoxGeckoDriver(t *testing.T) {
if *useDocker {
t.Skip("Skipping tests because they will be run under a Docker container")
}
if _, err := os.Stat(*geckoDriverPath); err != nil {
t.Skipf("Skipping Firefox tests on Selenium 3 because geckodriver binary %q not found", *geckoDriverPath)
if s, err := os.Stat(*firefoxBinarySelenium3); err != nil || !s.Mode().IsRegular() {
if p, err := exec.LookPath(*firefoxBinarySelenium3); err == nil {
*firefoxBinarySelenium3 = p
} else {
t.Skipf("Skipping Firefox tests because binary %q not found", *firefoxBinarySelenium3)
}
}

runFirefoxTests(t, *geckoDriverPath, config{
path: *firefoxBinarySelenium3,
t.Run("Selenium3", func(t *testing.T) {
runFirefoxTests(t, *selenium3Path, config{
seleniumVersion: semver.MustParse("3.0.0"),
serviceOptions: []ServiceOption{GeckoDriver(*geckoDriverPath)},
path: *firefoxBinarySelenium3,
})
})
t.Run("Geckodriver", func(t *testing.T) {
runFirefoxTests(t, *geckoDriverPath, config{
path: *firefoxBinarySelenium3,
})
})
}

Expand Down Expand Up @@ -287,14 +311,6 @@ func TestHTMLUnit(t *testing.T) {
func runFirefoxTests(t *testing.T, webDriverPath string, c config) {
c.browser = "firefox"

if s, err := os.Stat(c.path); err != nil || !s.Mode().IsRegular() {
if path, err := exec.LookPath(c.path); err == nil {
c.path = path
} else {
t.Skipf("Skipping Firefox tests because binary %q not found", c.path)
}
}

if *startFrameBuffer {
c.serviceOptions = append(c.serviceOptions, StartFrameBuffer())
}
Expand All @@ -313,20 +329,20 @@ func runFirefoxTests(t *testing.T, webDriverPath string, c config) {

var s *Service
if c.seleniumVersion.Major == 0 {
c.addr = fmt.Sprintf("http://127.0.0.1:%d", port)
s, err = NewGeckoDriverService(webDriverPath, port, c.serviceOptions...)
} else {
c.addr = fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port)
if _, err := os.Stat(*selenium3Path); err != nil {
t.Skipf("Skipping Firefox tests using Selenium 3 because Selenium WebDriver JAR not found at path %q", *selenium3Path)
}

s, err = NewSeleniumService(webDriverPath, port, c.serviceOptions...)
}
if err != nil {
t.Fatalf("Error starting the WebDriver server with binary %q: %v", webDriverPath, err)
}

if c.seleniumVersion.Major == 0 {
c.addr = fmt.Sprintf("http://127.0.0.1:%d", port)
} else {
c.addr = fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port)
}

runFirefoxSubTests(t, c)

if err := s.Stop(); err != nil {
Expand Down Expand Up @@ -470,6 +486,9 @@ func newTestCapabilities(t *testing.T, c config) Capabilities {
},
W3C: true,
}
if *headless {
chrCaps.Args = append(chrCaps.Args, "--headless")
}
caps.AddChrome(chrCaps)
case "firefox":
f := firefox.Capabilities{}
Expand All @@ -487,6 +506,9 @@ func newTestCapabilities(t *testing.T, c config) Capabilities {
Level: firefox.Trace,
}
}
if *headless {
f.Args = append(f.Args, "-headless")
}
caps.AddFirefox(f)
case "htmlunit":
caps["javascriptEnabled"] = true
Expand Down
3 changes: 1 addition & 2 deletions testing/travis-ci-test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
# Run tests under Travis for continuous integration.

go test -coverprofile=coverage.txt -covermode=atomic -test.v -timeout=20m \
--start_frame_buffer=false ./...
go test -coverprofile=coverage.txt -covermode=atomic -test.v -timeout=20m ./...
4 changes: 2 additions & 2 deletions vendor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ var files = []file{
// hash: "acf71b77d1b66b55db6fb0bed6d8bae2bbd481311bcbedfeff472c0d15e8f3cb",
},
{
url: "https://saucelabs.com/downloads/sc-4.5.3-linux.tar.gz",
url: "https://saucelabs.com/downloads/sc-4.5.4-linux.tar.gz",
name: "sauce-connect.tar.gz",
rename: []string{"sc-4.5.3-linux", "sauce-connect"},
rename: []string{"sc-4.5.4-linux", "sauce-connect"},
},
}

Expand Down

0 comments on commit 004f2b0

Please sign in to comment.