-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphantomjs.go
221 lines (190 loc) · 5.01 KB
/
phantomjs.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package phantomjs
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
"time"
)
type PhantomJS struct {
name string
execPath string
addr string
pid string
logFile *os.File
process *os.Process
options *Options
}
func (phantomJS *PhantomJS) Name() string {
return phantomJS.name
}
func (phantomJS *PhantomJS) NewSession(cap *Capabilities) (*Session, error) {
if cap == nil {
cap = DesiredCapabilities()
}
args := map[string]interface{}{
"requiredCapabilities": map[string]interface{}{},
"desiredCapabilities": cap,
}
res, err := Commands.NewSession.Execute(phantomJS.addr, args, phantomJS.options)
if err != nil {
return nil, err
}
if res.Code != ErrorCode_Success {
return nil, fmt.Errorf("%s", res.Data)
}
var ret struct {
Response
Cap *Capabilities `json:"value"`
}
err = json.Unmarshal(res.Data, &ret)
if err != nil {
return nil, err
}
return NewSession(phantomJS.addr, ret.SessionId, ret.Cap, phantomJS.options), nil
}
func (phantomJS *PhantomJS) GetAllSessions() ([]*Session, error) {
res, err := Commands.GetAllSessions.Execute(phantomJS.addr, nil, phantomJS.options)
if err != nil {
return nil, err
}
if res.Code != ErrorCode_Success {
return nil, errors.New(string(res.Data))
}
var ret struct {
Response
Value []struct {
Id string `json:"id"`
Cap *Capabilities `json:"capabilities"`
} `json:"value"`
}
err = json.Unmarshal(res.Data, &ret)
if err != nil {
return nil, err
}
if ret.Status != 0 {
return nil, fmt.Errorf("%s", res.Data)
}
sessions := []*Session{}
for _, item := range ret.Value {
sessions = append(sessions, NewSession(phantomJS.addr, item.Id, item.Cap, phantomJS.options))
}
return sessions, nil
}
func (phantomJS *PhantomJS) start() error {
var err error
attr := os.ProcAttr{}
attr.Dir = phantomJS.options.WorkDir
attr.Env = os.Environ()
attr.Files = []*os.File{nil, phantomJS.logFile, phantomJS.logFile}
attr.Sys = &syscall.SysProcAttr{Setpgid: true}
args := phantomJS.options.Args()
phantomJS.process, err = os.StartProcess(phantomJS.execPath, args, &attr)
if err != nil {
return errors.New("start phantomjs service failed")
}
for i := 0; ; i++ {
select {
case <-time.After(1 * time.Second):
if IsConnectAble(phantomJS.addr) {
return nil
}
}
if i == 30 {
phantomJS.Quit()
return fmt.Errorf("CAN NOT connect to service %s", phantomJS.addr)
}
}
}
func (phantomJS *PhantomJS) quit() error {
defer func() {
if phantomJS.logFile != nil {
phantomJS.logFile.Close()
os.RemoveAll(phantomJS.options.LogFilePath)
}
os.RemoveAll(phantomJS.options.CookiesFile)
os.RemoveAll(phantomJS.options.WorkDir)
}()
if phantomJS.process != nil {
err := phantomJS.process.Kill()
if err != nil {
return err
}
_, err = phantomJS.process.Wait()
return err
}
return nil
}
func (phantomJS *PhantomJS) Quit() error {
return phantomJS.quit()
}
func NewPhantomJS(port int, options *Options) (*PhantomJS, error) {
cmd := "phantomjs"
execPath, err := exec.LookPath(cmd)
if err != nil {
return nil, fmt.Errorf("please install phantomjs first")
}
return NewPhantomJSWithExecutePath(execPath, port, options)
}
func NewPhantomJSWithExecutePath(execPath string, port int, options *Options) (*PhantomJS, error) {
var err error
phantomJS := PhantomJS{}
phantomJS.execPath = execPath
phantomJS.options = &Options{}
*phantomJS.options = *options
if phantomJS.options.WorkDir == "" {
phantomJS.options.WorkDir, err = ioutil.TempDir("/tmp", "phantomjs")
if err != nil {
return nil, fmt.Errorf("create workspace failed")
}
}
_, err = os.Stat(phantomJS.options.WorkDir)
if err == os.ErrNotExist {
err = os.MkdirAll(phantomJS.options.WorkDir, 0755)
if err == os.ErrPermission {
return nil, fmt.Errorf("have no permission to create %s", phantomJS.options.WorkDir)
} else {
return nil, err
}
} else if err != nil {
return nil, err
}
if phantomJS.options.LogFilePath == "" {
phantomJS.logFile, err = ioutil.TempFile(phantomJS.options.WorkDir, "log")
if err != nil {
return nil, errors.New("open temp log file failed")
}
phantomJS.options.LogFilePath = phantomJS.logFile.Name()
} else {
phantomJS.logFile, err = os.OpenFile(phantomJS.options.LogFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, errors.New("open log file failed")
}
}
if phantomJS.options.CookiesFile == "" {
cookieFile, err := ioutil.TempFile(phantomJS.options.WorkDir, "cookie")
if err != nil {
return nil, errors.New("open temp cookie file failed")
}
phantomJS.options.CookiesFile = cookieFile.Name()
cookieFile.Close()
} else {
info, err := os.Stat(phantomJS.options.CookiesFile)
if err != nil && info.IsDir() {
return nil, fmt.Errorf("can not access cookie file %s", phantomJS.options.CookiesFile)
}
}
if port == 0 {
port = FindFreePort()
}
phantomJS.addr = fmt.Sprintf("localhost:%d", port)
phantomJS.options.Webdriver = phantomJS.addr
err = phantomJS.start()
if err != nil {
return nil, err
}
return &phantomJS, nil
}