-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebdriver.go
165 lines (130 loc) · 2.8 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
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
package webdriver
import (
"encoding/json"
"fmt"
"net"
"time"
"github.com/hupe1980/gowebdriver/bidi"
)
type WebDriver interface {
// Start webDriver service
Start() error
// Stop webDriver service
Stop() error
// Query the server status
Status() (*Status, error)
Port() int
// Create a new session
NewSession(optFns ...func(o *SessionOptions)) (*Session, error)
// Delete a session
DeleteSession(id string) error
}
type Options struct {
Port int
BootTimeout time.Duration
}
type webDriver struct {
port int
service *Service
timeout time.Duration
client *RestClient
}
func (w *webDriver) Start() error {
if err := w.service.Start(); err != nil {
return err
}
if err := w.service.WaitForBoot(w.timeout, func() bool {
status, err := w.Status()
if err != nil {
return false
}
return status.Ready
}); err != nil {
_ = w.service.Stop()
return err
}
return nil
}
func (w *webDriver) Stop() error {
if err := w.service.Stop(); err != nil {
return err
}
return nil
}
type Status struct {
Build struct {
// Version of driver
Version string `json:"version"`
} `json:"build"`
Message string `json:"message"`
OS struct {
// Operating system architecture
Arch string `json:"arch"`
// Name of operating system
Name string `json:"name"`
// Version of operating system
Version string `json:"version"`
} `json:"os"`
Ready bool `json:"ready"`
}
func (w *webDriver) Status() (*Status, error) {
data, err := w.client.Get("/status")
if err != nil {
return nil, err
}
status := &Status{}
if err := json.Unmarshal(data, status); err != nil {
return nil, err
}
return status, nil
}
func (w *webDriver) DeleteSession(id string) error {
_, err := w.client.Delete(fmt.Sprintf("/session/%s", id))
return err
}
func (w *webDriver) Port() int {
return w.port
}
type SessionOptions struct {
AlwaysMatch Capabilities
FirstMatch []Capabilities
}
func (w *webDriver) newSession(opts SessionOptions) (*Session, error) {
params := Params{
"alwaysMatch": opts.AlwaysMatch,
}
if opts.FirstMatch != nil {
params["firstMatch"] = opts.FirstMatch
}
data, err := w.client.Post("/session", &Params{
"capabilities": params,
})
if err != nil {
return nil, err
}
session := &Session{}
if err := json.Unmarshal(data, session); err != nil {
return nil, err
}
session.client = w.client
if session.IsBiDiSession() {
biDiSession, err := bidi.New(session.Capabilities.WebSocketURL(), nil)
if err != nil {
return nil, err
}
session.biDiSession = biDiSession
}
return session, nil
}
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}