This repository has been archived by the owner on May 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webdriver.go
68 lines (59 loc) · 2.66 KB
/
webdriver.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package agouti
import (
"fmt"
"github.com/sclevine/agouti/api"
)
// A WebDriver controls a Selenium, PhantomJS, or ChromeDriver process.
// This struct embeds api.WebDriver, which provides Start and Stop methods
// for starting and stopping the process.
type WebDriver struct {
*api.WebDriver
}
// NewWebDriver returns an instance of a WebDriver specified by
// a templated URL and command. The URL should be the location of the
// WebDriver Wire Protocol web service brought up by the command. The
// command should be provided as a list of arguments (which are each
// templated). The optional timeout specifies how long to wait for the
// web service to become available. Default timeout is 5 seconds.
//
// Valid template parameters are:
// {{.Host}} - local address to bind to (usually 127.0.0.1)
// {{.Port}} - arbitrary free port on the local address
// {{.Address}} - {{.Host}}:{{.Port}}
//
// Selenium JAR example:
// command := []string{"java", "-jar", "selenium-server.jar", "-port", "{{.Port}}"}
// agouti.NewWebDriver("http://{{.Address}}/wd/hub", command)
func NewWebDriver(url string, command []string, options ...Option) *WebDriver {
apiWebDriver := api.NewWebDriver(url, command)
defaultConfig := &config{timeout: apiWebDriver.Timeout}
apiWebDriver.Timeout = defaultConfig.apply(options).timeout
return &WebDriver{apiWebDriver}
}
// ChromeDriver returns an instance of a ChromeDriver WebDriver.
func ChromeDriver() *WebDriver {
return NewWebDriver("http://{{.Address}}", []string{"chromedriver", "--silent", "--port={{.Port}}"})
}
// PhantomJS returns an instance of a PhantomJS WebDriver.
// The return error is deprecated and will always be nil.
func PhantomJS() *WebDriver {
return NewWebDriver("http://{{.Address}}", []string{"phantomjs", "--webdriver={{.Address}}"})
}
// Selenium returns an instance of a Selenium WebDriver.
// The return error is deprecated and will always be nil.
func Selenium() *WebDriver {
return NewWebDriver("http://{{.Address}}/wd/hub", []string{"selenium-server", "-port", "{{.Port}}"})
}
// NewPage returns a *Page that corresponds to a new WebDriver session.
// Any provided options configure the page. For instance:
// driver.NewPage(agouti.Desired(agouti.NewCapabilities().Without("javascriptEnabled")))
// For Selenium, this argument must include a browser. For instance:
// seleniumDriver.NewPage(agouti.Desired(agouti.NewCapabilities().Browser("safari")))
func (w *WebDriver) NewPage(options ...Option) (*Page, error) {
desiredCapabilities := getOptions(options).desired
session, err := w.Open(desiredCapabilities)
if err != nil {
return nil, fmt.Errorf("failed to open session: %s", err)
}
return newPage(session), nil
}