forked from fedesog/webdriver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchromedriver.go
175 lines (159 loc) · 4.17 KB
/
chromedriver.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2013 Federico Sogaro. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webdriver
import (
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strconv"
"time"
)
type ChromeSwitches map[string]interface{}
type ChromeDriver struct {
WebDriverCore
//The port that ChromeDriver listens on. Default: 9515
Port int
//The URL path prefix to use for all incoming WebDriver REST requests. Default: ""
BaseUrl string
//The number of threads to use for handling HTTP requests. Default: 4
Threads int
//The path to use for the ChromeDriver server log. Default: ./chromedriver.log
LogPath string
// Log file to dump chromedriver stdout/stderr. If "" send to terminal. Default: ""
LogFile string
// Start method fails if Chromedriver doesn't start in less than StartTimeout. Default 20s.
StartTimeout time.Duration
path string
cmd *exec.Cmd
logFile *os.File
}
//create a new service using chromedriver.
//function returns an error if not supported switches are passed. Actual content
//of valid-named switches is not validate and is passed as it is.
//switch silent is removed (output is needed to check if chromedriver started correctly)
func NewChromeDriver(path string) *ChromeDriver {
d := &ChromeDriver{}
d.path = path
d.Port = 0
d.BaseUrl = ""
d.Threads = 4
d.LogPath = "chromedriver.log"
d.StartTimeout = 20 * time.Second
return d
}
var switchesFormat = "-port=%d -url-base=%s -log-path=%s -http-threads=%d"
var cmdchan = make(chan error)
func (d *ChromeDriver) Start() error {
if d.Port == 0 {
var err error
d.Port, err = GetFreePort()
if err != nil {
return err
}
}
csferr := "chromedriver start failed: "
if d.cmd != nil {
return errors.New(csferr + "chromedriver already running")
}
if d.LogPath != "" {
//check if log-path is writable
file, err := os.OpenFile(d.LogPath, os.O_WRONLY|os.O_CREATE, 0664)
if err != nil {
return errors.New(csferr + "unable to write in log path: " + err.Error())
}
file.Close()
}
d.url = fmt.Sprintf("http://127.0.0.1:%d%s", d.Port, d.BaseUrl)
var switches []string
switches = append(switches, "-port="+strconv.Itoa(d.Port))
switches = append(switches, "-log-path="+d.LogPath)
switches = append(switches, "-http-threads="+strconv.Itoa(d.Threads))
if d.BaseUrl != "" {
switches = append(switches, "-url-base="+d.BaseUrl)
}
d.cmd = exec.Command(d.path, switches...)
stdout, err := d.cmd.StdoutPipe()
if err != nil {
return errors.New(csferr + err.Error())
}
stderr, err := d.cmd.StderrPipe()
if err != nil {
return errors.New(csferr + err.Error())
}
if err := d.cmd.Start(); err != nil {
return errors.New(csferr + err.Error())
}
if d.LogFile != "" {
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
d.logFile, err = os.OpenFile(d.LogFile, flags, 0640)
if err != nil {
return err
}
go func() {
if _, err := io.Copy(d.logFile, stdout); err != nil {
log.Println(err)
}
}()
go func() {
if _, err := io.Copy(d.logFile, stderr); err != nil {
log.Println(err)
}
}()
} else {
go func() {
if _, err := io.Copy(os.Stdout, stdout); err != nil {
log.Println(err)
}
}()
go func() {
if _, err := io.Copy(os.Stderr, stderr); err != nil {
log.Println(err)
}
}()
}
if err = probePort(d.Port, d.StartTimeout); err != nil {
return err
}
return nil
}
func (d *ChromeDriver) Stop() error {
if d.cmd == nil {
return errors.New("stop failed: chromedriver not running")
}
defer func() {
d.cmd = nil
}()
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
return err
}
if d.logFile != nil {
if err := d.logFile.Close(); err != nil {
return err
}
}
return nil
}
func (d *ChromeDriver) NewSession(desired, required Capabilities) (*Session, error) {
//id, capabs, err := d.newSession(desired, required)
//return &Session{id, capabs, d}, err
session, err := d.newSession(desired, required)
if err != nil {
return nil, err
}
session.wd = d
return session, nil
}
func (d *ChromeDriver) Sessions() ([]Session, error) {
sessions, err := d.sessions()
if err != nil {
return nil, err
}
for i := range sessions {
sessions[i].wd = d
}
return sessions, nil
}