-
Notifications
You must be signed in to change notification settings - Fork 1
/
element.go
244 lines (193 loc) · 7.24 KB
/
element.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package webdriver
import (
"encoding/base64"
"encoding/json"
"fmt"
)
type Element struct {
ID string `json:"element-6066-11e4-a52e-4f735466cecf"`
SessionID string `json:"-"`
client *RestClient `json:"-"`
}
/****************************************************************************************************************
* ELEMENTS *
* https://www.w3.org/TR/webdriver/#elements *
****************************************************************************************************************/
// GetShadowRoot returns a shadow root of the element if there is one or an error.
func (e *Element) GetShadowRoot() (*ShadowRoot, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/shadow", e.SessionID, e.ID))
if err != nil {
return nil, err
}
shadowRoot := ShadowRoot{}
if err = json.Unmarshal(data, &shadowRoot); err != nil {
return nil, err
}
shadowRoot.SessionID = e.SessionID
shadowRoot.client = e.client
return &shadowRoot, err
}
// FindElement searches for an element on the page, starting from the referenced web element.
func (e *Element) FindElement(strategy LocatorStrategy, selector string) (*Element, error) {
data, err := e.client.Post(fmt.Sprintf("/session/%s/element/%s", e.SessionID, e.ID), &Params{
"using": strategy,
"value": selector,
})
if err != nil {
return nil, err
}
element := Element{}
if err := json.Unmarshal(data, &element); err != nil {
return nil, err
}
element.SessionID = e.SessionID
element.client = e.client
return &element, nil
}
// FindElements searches for multiple elements on the page, starting from the referenced web element. The located
// elements will be returned as a WebElement JSON objects. The table below lists the locator
// strategies that each server should support. Elements should be returned in the order located
// in the DOM.
func (e *Element) FindElements(strategy LocatorStrategy, selector string) ([]Element, error) {
data, err := e.client.Post(fmt.Sprintf("/session/%s/elements/%s", e.SessionID, e.ID), &Params{
"using": strategy,
"value": selector,
})
if err != nil {
return nil, err
}
elements := []Element{}
if err := json.Unmarshal(data, &elements); err != nil {
return nil, err
}
for index := range elements {
elements[index].SessionID = e.SessionID
elements[index].client = e.client
}
return elements, nil
}
// IsSelected determines if the referenced element is selected or not.
// This operation only makes sense on input elements of the Checkbox- and Radio Button states, or on option elements.
func (e *Element) IsSelected() (bool, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/selected", e.SessionID, e.ID))
if err != nil {
return false, err
}
var selected bool
err = json.Unmarshal(data, &selected)
return selected, err
}
// GetAttribute returns the attribute value of the referenced web element.
func (e *Element) GetAttribute(name string) (string, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/attribute/%s", e.SessionID, e.ID, name))
if err != nil {
return "", err
}
var value string
err = json.Unmarshal(data, &value)
return value, err
}
// GetProperty returns the property of the referenced web element.
func (e *Element) GetProperty(name string) (string, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/property/%s", e.SessionID, e.ID, name))
if err != nil {
return "", err
}
var value string
err = json.Unmarshal(data, &value)
return value, err
}
// GetCSSValue returns the computed value of the given CSS property for the element.
func (e *Element) GetCSSValue(name string) (string, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/css/%s", e.SessionID, e.ID, name))
if err != nil {
return "", err
}
var value string
err = json.Unmarshal(data, &value)
return value, err
}
// GetText returns the visible text for the element.
func (e *Element) GetText() (string, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/text", e.SessionID, e.ID))
if err != nil {
return "", err
}
var text string
err = json.Unmarshal(data, &text)
return text, err
}
// GetTagName returns the tagName of an element
func (e *Element) GetTagName() (string, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/name", e.SessionID, e.ID))
if err != nil {
return "", err
}
var name string
err = json.Unmarshal(data, &name)
return name, err
}
// ElementRect defines the Element Rect.
type ElementRect struct {
// X axis position of the top-left corner of the element relative to the current browsing context's document element in CSS pixels
X int `json:"x"`
// Y axis position of the top-left corner of the element relative to the current browsing context's document element in CSS pixels
Y int `json:"y"`
// Height of the element's bounding rectangle in CSS pixels
Width int `json:"width"`
// Width of the web element's bounding rectangle in CSS pixels
Height int `json:"height"`
}
// Returns the dimensions and coordinates of the referenced element
func (e *Element) GetRect() (*ElementRect, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/rect", e.SessionID, e.ID))
if err != nil {
return nil, err
}
elementRect := &ElementRect{}
err = json.Unmarshal(data, &elementRect)
return elementRect, err
}
// IsEnabled determines if the referenced element is enabled or not.
func (e *Element) IsEnabled() (bool, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/enabled", e.SessionID, e.ID))
if err != nil {
return false, err
}
var enabled bool
err = json.Unmarshal(data, &enabled)
return enabled, err
}
// Click clicks on an element.
func (e *Element) Click() error {
_, err := e.client.Post(fmt.Sprintf("/session/%s/element/%s/click", e.SessionID, e.ID), nil)
return err
}
// Clear clears content of an element.
func (e *Element) Clear() error {
_, err := e.client.Post(fmt.Sprintf("/session/%s/element/%s/clear", e.SessionID, e.ID), nil)
return err
}
// SendKeys sends a sequence of key strokes to an element.
func (e *Element) SendKeys(text string) error {
_, err := e.client.Post(fmt.Sprintf("/session/%s/element/%s/value", e.SessionID, e.ID), &Params{
"text": text,
})
return err
}
/****************************************************************************************************************
* SCREEN CAPTURE *
* https://www.w3.org/TR/webdriver/#screen-capture *
****************************************************************************************************************/
// TakeScreenshot takes a screenshot of the visible region encompassed by the bounding rectangle of an element.
func (e *Element) TakeScreenshot() ([]byte, error) {
data, err := e.client.Get(fmt.Sprintf("/session/%s/element/%s/screenshot", e.SessionID, e.ID))
if err != nil {
return nil, err
}
var screenshot string
if err := json.Unmarshal(data, &screenshot); err != nil {
return nil, err
}
return base64.StdEncoding.DecodeString(screenshot)
}