Skip to content

Commit 9d85948

Browse files
committed
fork webdriver and add phantomjs
1 parent 6f14c44 commit 9d85948

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is a pure go library and doesn't require a running Selenium driver. It curr
88
Tests are partial and have been run only on Linux (with firefox webdriver 2.32.0 and chromedriver 2.1).
99

1010
**Install:**
11-
$ go get github.com/fedesog/webdriver
11+
$ go get github.com/bborbe/webdriver
1212

1313
**Requires:**
1414
* chromedriver (for chrome):

phantomjs.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2013 Federico Sogaro. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package webdriver
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
"io"
11+
"os"
12+
"os/exec"
13+
"strconv"
14+
"time"
15+
)
16+
17+
type PhantomJsSwitches map[string]interface{}
18+
19+
type PhantomJsDriver struct {
20+
WebDriverCore
21+
//The port that PhantomJsDriver listens on. Default: 9515
22+
Port int
23+
//The URL path prefix to use for all incoming WebDriver REST requests. Default: ""
24+
BaseUrl string
25+
//The number of threads to use for handling HTTP requests. Default: 4
26+
Threads int
27+
//The path to use for the PhantomJsDriver server log. Default: ./phantomJsdriver.log
28+
LogPath string
29+
// Log file to dump phantomJsdriver stdout/stderr. If "" send to terminal. Default: ""
30+
LogFile string
31+
// Start method fails if PhantomJsdriver doesn't start in less than StartTimeout. Default 20s.
32+
StartTimeout time.Duration
33+
34+
path string
35+
cmd *exec.Cmd
36+
logFile *os.File
37+
}
38+
39+
//create a new service using phantomJsdriver.
40+
//function returns an error if not supported switches are passed. Actual content
41+
//of valid-named switches is not validate and is passed as it is.
42+
//switch silent is removed (output is needed to check if phantomJsdriver started correctly)
43+
func NewPhantomJsDriver(path string) *PhantomJsDriver {
44+
d := &PhantomJsDriver{}
45+
d.path = path
46+
d.Port = 9515
47+
d.BaseUrl = ""
48+
d.Threads = 4
49+
d.LogPath = "phantomJsdriver.log"
50+
d.StartTimeout = 20 * time.Second
51+
return d
52+
}
53+
54+
func (d *PhantomJsDriver) Start() error {
55+
csferr := "phantomJsdriver start failed: "
56+
if d.cmd != nil {
57+
return errors.New(csferr + "phantomJsdriver already running")
58+
}
59+
60+
if d.LogPath != "" {
61+
//check if log-path is writable
62+
file, err := os.OpenFile(d.LogPath, os.O_WRONLY | os.O_CREATE, 0664)
63+
if err != nil {
64+
return errors.New(csferr + "unable to write in log path: " + err.Error())
65+
}
66+
file.Close()
67+
}
68+
69+
d.url = fmt.Sprintf("http://127.0.0.1:%d%s", d.Port, d.BaseUrl)
70+
var switches []string
71+
switches = append(switches, "--webdriver=" + strconv.Itoa(d.Port))
72+
switches = append(switches, "--webdriver-logfile=" + d.LogPath)
73+
switches = append(switches, "--webdriver-loglevel=DEBUG")
74+
75+
d.cmd = exec.Command(d.path, switches...)
76+
stdout, err := d.cmd.StdoutPipe()
77+
if err != nil {
78+
return errors.New(csferr + err.Error())
79+
}
80+
stderr, err := d.cmd.StderrPipe()
81+
if err != nil {
82+
return errors.New(csferr + err.Error())
83+
}
84+
if err := d.cmd.Start(); err != nil {
85+
return errors.New(csferr + err.Error())
86+
}
87+
if d.LogFile != "" {
88+
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
89+
d.logFile, err = os.OpenFile(d.LogFile, flags, 0640)
90+
if err != nil {
91+
return err
92+
}
93+
go io.Copy(d.logFile, stdout)
94+
go io.Copy(d.logFile, stderr)
95+
} else {
96+
go io.Copy(os.Stdout, stdout)
97+
go io.Copy(os.Stderr, stderr)
98+
}
99+
if err = probePort(d.Port, d.StartTimeout); err != nil {
100+
return err
101+
}
102+
return nil
103+
}
104+
105+
func (d *PhantomJsDriver) Stop() error {
106+
if d.cmd == nil {
107+
return errors.New("stop failed: phantomJsdriver not running")
108+
}
109+
defer func() {
110+
d.cmd = nil
111+
}()
112+
d.cmd.Process.Signal(os.Interrupt)
113+
if d.logFile != nil {
114+
d.logFile.Close()
115+
}
116+
return nil
117+
}
118+
119+
func (d *PhantomJsDriver) NewSession(desired, required Capabilities) (*Session, error) {
120+
//id, capabs, err := d.newSession(desired, required)
121+
//return &Session{id, capabs, d}, err
122+
session, err := d.newSession(desired, required)
123+
if err != nil {
124+
return nil, err
125+
}
126+
session.wd = d
127+
return session, nil
128+
}
129+
130+
func (d *PhantomJsDriver) Sessions() ([]Session, error) {
131+
sessions, err := d.sessions()
132+
if err != nil {
133+
return nil, err
134+
}
135+
for i := range sessions {
136+
sessions[i].wd = d
137+
}
138+
return sessions, nil
139+
}

0 commit comments

Comments
 (0)