-
Notifications
You must be signed in to change notification settings - Fork 1
/
element.go
213 lines (168 loc) · 6.26 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
package webdriver
import (
"encoding/json"
"fmt"
)
type webElement struct {
ID string `json:"element-6066-11e4-a52e-4f735466cecf"`
}
type Element struct {
ID string
SessionID string
client *RestClient
}
/****************************************************************************************************************
* ELEMENTS *
* https://www.w3.org/TR/webdriver/#elements *
****************************************************************************************************************/
// Search 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
}
webElement := &webElement{}
if err := json.Unmarshal(data, webElement); err != nil {
return nil, err
}
return &Element{ID: webElement.ID, SessionID: e.SessionID, client: e.client}, nil
}
// Search 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
}
webElements := &[]webElement{}
if err := json.Unmarshal(data, webElements); err != nil {
return nil, err
}
elements := []*Element{}
for _, we := range *webElements {
elements = append(elements, &Element{ID: we.ID, SessionID: e.SessionID, client: e.client})
}
return elements, nil
}
// 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
}
// 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/attribure/%s", e.SessionID, e.ID, name))
if err != nil {
return "", err
}
var value string
err = json.Unmarshal(data, &value)
return value, err
}
// 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
}
// 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
}
// 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
}
// Returns the tagName of a 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
}
// 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 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 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
}
// Send 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 *
****************************************************************************************************************/
//TODO