-
Notifications
You must be signed in to change notification settings - Fork 33
/
contract.go
393 lines (348 loc) · 9.85 KB
/
contract.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package webdriver
import (
"fmt"
"github.com/viant/endly/internal/util"
"github.com/viant/endly/model/criteria/eval"
"github.com/viant/endly/model/location"
"github.com/viant/endly/service/testing/validator"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"strings"
)
const defaultTarget = "/opt/local/webdriver"
const defaultExitWaitTimeMs = 1000
type PathKind int
const (
PathKindUndefined = PathKind(iota)
PathKindSimple
PathKindComposite
)
// StartRequest represents a selenium server start request
type StartRequest struct {
Target *location.Resource
Driver string
Server string
Sdk string
Capabilities []string
Port int
}
func (r *StartRequest) Init() error {
if r.Port == 0 {
r.Port = 4444
}
if r.Driver == "" {
r.Driver = ChromeDriver
}
if r.Target == nil {
r.Target = location.NewResource(defaultTarget)
}
return nil
}
func (r *StartRequest) Validate() error {
return nil
}
// NewStartRequestFromURL creates a new start request from URL
func NewStartRequestFromURL(URL string) (*StartRequest, error) {
var result = &StartRequest{}
var resource = location.NewResource(URL)
err := resource.Decode(result)
return result, err
}
// StartResponse represents a selenium server stop request
type StartResponse struct {
Pid int
ServerPath string
DriverPath string
SessionID string
}
// StopRequest represents server stop request
type StopRequest struct {
Target *location.Resource
Port int
}
func (r *StopRequest) Init() error {
if r.Port == 0 {
r.Port = 4444
}
if r.Target == nil {
r.Target = location.NewResource(defaultTarget)
}
return nil
}
// NewStopRequestFromURL creates a new start request from URL
func NewStopRequestFromURL(URL string) (*StopRequest, error) {
var result = &StopRequest{}
var resource = location.NewResource(URL)
err := resource.Decode(result)
return result, err
}
// StopResponse represents a selenium stop request
type StopResponse struct {
}
// OpenSessionResponse represents open session response.
type OpenSessionResponse struct {
SessionID string
}
// CloseSessionRequest represents close session request.
type CloseSessionRequest struct {
SessionID string
}
// NewCloseSessionRequestFromURL creates a new close session request from URL
func NewCloseSessionRequestFromURL(URL string) (*CloseSessionRequest, error) {
var result = &CloseSessionRequest{}
var resource = location.NewResource(URL)
err := resource.Decode(result)
return result, err
}
// CloseSessionResponse represents close session response.
type CloseSessionResponse struct {
SessionID string
}
// WebDriverCallRequest represents selenium call driver request
type WebDriverCallRequest struct {
SessionID string
Key string
PathKind PathKind
Call *MethodCall
}
// ServiceCallResponse represents selenium call response
type ServiceCallResponse struct {
Result []interface{}
Data data.Map
}
// WebElementSelector represents a web element selector
type WebElementSelector struct {
By string //selector type
Value string //selector value
Key string //optional result key
}
// WebElementCallRequest represents a web element call reqesut
type WebElementCallRequest struct {
SessionID string
Selector *WebElementSelector
Call *MethodCall
PathKind PathKind
}
// WebElementCallResponse represents seleniun web element response
type WebElementCallResponse struct {
Result []interface{}
LookupError string
Data map[string]interface{}
}
// RunRequest represents group of selenium web elements calls
type RunRequest struct {
SessionID string
Browser string
RemoteSelenium string //remote selenium resource
Actions []*Action
ActionDelaysMs int `description:"slows down action with specified delay"`
Commands []interface{} `description:"list of selenium command: {web element selector}.WebElementMethod(params), or WebDriverMethod(params), or wait map "`
Expect interface{} `description:"If specified it will validated response as actual"`
}
func (r *RunRequest) asWaitAction(parser *parser, candidate interface{}) (*Action, error) {
if aMap, err := util.NormalizeMap(candidate, true); err == nil {
command, ok := aMap["command"]
if !ok {
return nil, fmt.Errorf("command was missing: %v", candidate)
}
action, err := parser.Parse(toolbox.AsString(command))
if err != nil {
return nil, err
}
if action.PathKind == PathKindUndefined {
action.PathKind = PathKindSimple
}
err = toolbox.DefaultConverter.AssignConverted(&action.Calls[0].Wait, aMap)
return action, err
}
return nil, fmt.Errorf("sunupported command: %T", candidate)
}
func (r *RunRequest) Init() error {
if r.SessionID == "" {
r.SessionID = "localhost:4444"
}
if len(r.Actions) > 0 {
for _, action := range r.Actions {
if action.Selector != nil {
_ = action.Selector.Init()
if action.Selector.Key == "" {
action.Selector.Key = action.Key
}
if action.Selector.Key == "" {
action.Selector.Key = action.Selector.Value
}
}
}
return nil
}
if len(r.Commands) == 0 {
return nil
}
expectMap := r.expectMap()
r.Actions = make([]*Action, 0)
var previousAction *Action
parser := &parser{}
for _, candidate := range r.Commands {
command, ok := candidate.(string)
if !ok {
action, err := r.asWaitAction(parser, candidate)
if err != nil {
return err
}
r.setWaitExitIfNeeded(action.Calls[0], expectMap, action)
r.Actions = append(r.Actions, action)
continue
}
action, err := parser.Parse(command)
if err != nil {
return fmt.Errorf("invalid command: %v, %v", command, err)
}
if previousAction != nil {
if previousAction.Selector != nil && action.Selector != nil && previousAction.Selector.Value == action.Selector.Value {
if action.Key == "" && isReadMethod(action.Calls[0].Method) && isReadMethod(previousAction.Calls[0].Method) {
previousAction.Calls = append(previousAction.Calls, action.Calls[0])
previousAction.PathKind = PathKindComposite
continue
}
}
}
r.Actions = append(r.Actions, action)
previousAction = action
call := action.Calls[0]
r.setWaitExitIfNeeded(call, expectMap, action)
}
return nil
}
func (r *RunRequest) setWaitExitIfNeeded(call *MethodCall, expectMap map[string]interface{}, action *Action) {
if call.Exit == "" {
if expectValue, ok := expectMap[action.Key]; ok && expectValue != nil {
switch actual := expectValue.(type) {
case string:
call.Exit = "$" + action.Key + " contains " + actual
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
call.Exit = "$" + action.Key + " = " + toolbox.AsString(expectValue)
case float64:
call.Exit = "$" + action.Key + " = " + toolbox.AsString(expectValue)
}
if call.Exit != "" {
if call.WaitTimeMs == 0 {
call.WaitTimeMs = defaultExitWaitTimeMs
}
call.IgnoreTimeout = true
}
}
}
}
func isReadMethod(method string) bool {
return strings.HasPrefix(method, "Get") || strings.HasPrefix(method, "Text")
}
// NewRunRequest creates a new run request
func NewRunRequest(sessionID, browser string, remote string, actions ...*Action) *RunRequest {
return &RunRequest{
SessionID: sessionID,
Browser: browser,
RemoteSelenium: remote,
Actions: actions,
}
}
// NewRunRequestFromURL creates a new request from URL
func NewRunRequestFromURL(URL string) (*RunRequest, error) {
resource := location.NewResource(URL)
var result = &RunRequest{}
return result, resource.Decode(result)
}
// RunResponse represents selenium call response
type RunResponse struct {
SessionID string
Data map[string]interface{}
LookupErrors []string
Assert *validator.AssertResponse
}
// MethodCall represents selenium call.
type MethodCall struct {
Wait
Method string
Parameters []interface{}
}
type Wait struct {
WaitTimeMs int
ThinkTimeMs int
IgnoreTimeout bool
Exit string
criteria eval.Compute
}
// Action represents various calls on web element
type Action struct {
Key string //optional result key
PathKind
Selector *WebElementSelector
Calls []*MethodCall
}
// NewAction creates a new action
func NewAction(key, selector string, method string, params ...interface{}) *Action {
var result = &Action{
Key: key,
Calls: []*MethodCall{
{
Method: method,
Parameters: params,
},
},
}
if selector != "" {
var webSelector = WebSelector(selector)
result.Selector = &WebElementSelector{}
result.Selector.By, result.Selector.Value = webSelector.ByAndValue()
result.Selector.Key = result.Key
}
return result
}
// Validate validates run request.
func (r *RunRequest) Validate() error {
if r.SessionID == "" {
if r.Browser == "" {
return fmt.Errorf("both SessionID and Browser were empty")
}
}
if len(r.Actions) == 0 {
return fmt.Errorf("both actions/commands were empty")
}
for i, action := range r.Actions {
if len(action.Calls) == 0 {
return fmt.Errorf("actions[%d].Calls were empty", i)
}
}
return nil
}
// OpenSessionRequest represents open session request
type OpenSessionRequest struct {
Browser string
Capabilities []string
Remote string `description:"webdriver server endpoint"`
SessionID string `description:"if specified this SessionID will be used for a sessionID"`
}
// Init initializes request
func (r *OpenSessionRequest) Init() error {
if r.SessionID == "" {
r.SessionID = "localhost:4444"
}
host, port := pair(r.SessionID)
r.Remote = fmt.Sprintf("http://%v:%v/wd/hub", host, port)
return nil
}
// NewOpenSessionRequest creates a new open session request
func NewOpenSessionRequest(browser string, remote string) *OpenSessionRequest {
return &OpenSessionRequest{
Browser: browser,
Remote: remote,
}
}
func (r *RunRequest) expectMap() map[string]interface{} {
var expectMap = map[string]interface{}{}
if r.Expect != nil {
if value, ok := r.Expect.(map[string]interface{}); ok {
expectMap = value
}
}
return expectMap
}