-
Notifications
You must be signed in to change notification settings - Fork 1
/
webdriver.go
117 lines (93 loc) · 2.04 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
package webdriver
import (
"encoding/json"
"fmt"
"net"
"time"
)
type WebDriver interface {
// Start webDriver service
Start() error
// Stop webDriver service
Stop() error
// Query the server status
Status() (*Status, error)
// 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 {
client *RestClient
}
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
}
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
}
fmt.Println(string(data))
session := &Session{}
if err := json.Unmarshal(data, session); err != nil {
return nil, err
}
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
}