forked from sclevine/agouti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_actions_test.go
539 lines (463 loc) · 21 KB
/
selection_actions_test.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package agouti_test
import (
"errors"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/sclevine/agouti"
"github.com/sclevine/agouti/api"
"github.com/sclevine/agouti/internal/element"
. "github.com/sclevine/agouti/internal/matchers"
"github.com/sclevine/agouti/internal/mocks"
)
var _ = Describe("Selection Actions", func() {
var (
selection *MultiSelection
session *mocks.Session
elementRepository *mocks.ElementRepository
firstElement *mocks.Element
secondElement *mocks.Element
)
BeforeEach(func() {
session = &mocks.Session{}
firstElement = &mocks.Element{}
secondElement = &mocks.Element{}
elementRepository = &mocks.ElementRepository{}
selection = NewTestMultiSelection(session, elementRepository, "#selector")
elementRepository.GetAtLeastOneCall.ReturnElements = []element.Element{firstElement, secondElement}
})
Describe("#Click", func() {
It("should successfully click on all selected elements", func() {
Expect(selection.Click()).To(Succeed())
Expect(firstElement.ClickCall.Called).To(BeTrue())
Expect(secondElement.ClickCall.Called).To(BeTrue())
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Click()).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when any click fails", func() {
It("should return an error", func() {
secondElement.ClickCall.Err = errors.New("some error")
Expect(selection.Click()).To(MatchError("failed to click on selection 'CSS: #selector': some error"))
})
})
})
// TODO: extend mock to test multiple calls
Describe("#DoubleClick", func() {
var apiElement *api.Element
BeforeEach(func() {
apiElement = &api.Element{}
elementRepository.GetAtLeastOneCall.ReturnElements = []element.Element{&api.Element{}, apiElement}
})
It("should successfully move the mouse to the middle of each selected element", func() {
Expect(selection.DoubleClick()).To(Succeed())
Expect(session.MoveToCall.Element).To(ExactlyEqual(apiElement))
Expect(session.MoveToCall.Offset).To(BeNil())
})
It("should successfully double-click on each element", func() {
Expect(selection.DoubleClick()).To(Succeed())
Expect(session.DoubleClickCall.Called).To(BeTrue())
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.DoubleClick()).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when moving over any element fails", func() {
It("should retun an error", func() {
session.MoveToCall.Err = errors.New("some error")
Expect(selection.DoubleClick()).To(MatchError("failed to move mouse to selection 'CSS: #selector': some error"))
})
})
Context("when the double-clicking any element fails", func() {
It("should return an error", func() {
session.DoubleClickCall.Err = errors.New("some error")
Expect(selection.DoubleClick()).To(MatchError("failed to double-click on selection 'CSS: #selector': some error"))
})
})
})
Describe("#Fill", func() {
It("should successfully clear each element", func() {
Expect(selection.Fill("some text")).To(Succeed())
Expect(firstElement.ClearCall.Called).To(BeTrue())
Expect(secondElement.ClearCall.Called).To(BeTrue())
})
It("should successfully fill each element with the provided text", func() {
Expect(selection.Fill("some text")).To(Succeed())
Expect(firstElement.ValueCall.Text).To(Equal("some text"))
Expect(secondElement.ValueCall.Text).To(Equal("some text"))
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Fill("some text")).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when clearing any element fails", func() {
It("should return an error", func() {
secondElement.ClearCall.Err = errors.New("some error")
Expect(selection.Fill("some text")).To(MatchError("failed to clear selection 'CSS: #selector': some error"))
})
})
Context("when entering text into any element fails", func() {
It("should return an error", func() {
secondElement.ValueCall.Err = errors.New("some error")
Expect(selection.Fill("some text")).To(MatchError("failed to enter text into selection 'CSS: #selector': some error"))
})
})
})
Describe("#Clear", func() {
It("should successfully clear each element", func() {
Expect(selection.Clear()).To(Succeed())
Expect(firstElement.ClearCall.Called).To(BeTrue())
Expect(secondElement.ClearCall.Called).To(BeTrue())
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Clear()).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when clearing any element fails", func() {
It("should return an error", func() {
secondElement.ClearCall.Err = errors.New("some error")
Expect(selection.Clear()).To(MatchError("failed to clear selection 'CSS: #selector': some error"))
})
})
})
Describe("#UploadFile", func() {
BeforeEach(func() {
firstElement.GetAttributeCall.ReturnValue = "file"
firstElement.GetNameCall.ReturnName = "input"
secondElement.GetAttributeCall.ReturnValue = "file"
secondElement.GetNameCall.ReturnName = "input"
})
It("should successfully enter the absolute file path into each element", func() {
Expect(selection.UploadFile("some-file")).To(Succeed())
Expect(firstElement.ValueCall.Text).To(HaveSuffix(filepath.Join("agouti", "some-file")))
Expect(secondElement.ValueCall.Text).To(HaveSuffix(filepath.Join("agouti", "some-file")))
})
It("should request the 'type' attribute for each element", func() {
Expect(selection.UploadFile("some-file")).To(Succeed())
Expect(firstElement.GetAttributeCall.Attribute).To(Equal("type"))
Expect(secondElement.GetAttributeCall.Attribute).To(Equal("type"))
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.UploadFile("/some/file")).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when any element has a tag name other than 'input'", func() {
It("should return an error", func() {
secondElement.GetNameCall.ReturnName = "notinput"
err := selection.UploadFile("some-file")
Expect(err).To(MatchError("element for selection 'CSS: #selector' is not an input element"))
})
})
Context("when the tag name of any element is not retrievable", func() {
It("should return an error", func() {
secondElement.GetNameCall.Err = errors.New("some error")
err := selection.UploadFile("some-file")
Expect(err).To(MatchError("failed to determine tag name of selection 'CSS: #selector': some error"))
})
})
Context("when any element has a type attribute other than 'file'", func() {
It("should return an error", func() {
secondElement.GetAttributeCall.ReturnValue = "notfile"
err := selection.UploadFile("some-file")
Expect(err).To(MatchError("element for selection 'CSS: #selector' is not a file uploader"))
})
})
Context("when the type attribute of any element is not retrievable", func() {
It("should return an error", func() {
secondElement.GetAttributeCall.Err = errors.New("some error")
err := selection.UploadFile("some-file")
Expect(err).To(MatchError("failed to determine type attribute of selection 'CSS: #selector': some error"))
})
})
Context("when entering text into any element fails", func() {
It("should return an error", func() {
secondElement.ValueCall.Err = errors.New("some error")
Expect(selection.UploadFile("/some/file")).To(MatchError("failed to enter text into selection 'CSS: #selector': some error"))
})
})
})
Describe("#Check", func() {
It("should successfully check the type of each checkbox", func() {
firstElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.GetAttributeCall.ReturnValue = "checkbox"
Expect(selection.Check()).To(Succeed())
Expect(firstElement.GetAttributeCall.Attribute).To(Equal("type"))
Expect(secondElement.GetAttributeCall.Attribute).To(Equal("type"))
})
Context("when all elements are checkboxes", func() {
BeforeEach(func() {
firstElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.GetAttributeCall.ReturnValue = "checkbox"
})
It("should not click on the checked checkbox successfully", func() {
firstElement.IsSelectedCall.ReturnSelected = true
Expect(selection.Check()).To(Succeed())
Expect(firstElement.ClickCall.Called).To(BeFalse())
})
It("should click on the unchecked checkboxes successfully", func() {
secondElement.IsSelectedCall.ReturnSelected = false
Expect(selection.Check()).To(Succeed())
Expect(secondElement.ClickCall.Called).To(BeTrue())
})
Context("when the determining the selected status of any element fails", func() {
It("should return an error", func() {
secondElement.IsSelectedCall.Err = errors.New("some error")
Expect(selection.Check()).To(MatchError("failed to retrieve state of selection 'CSS: #selector': some error"))
})
})
Context("when clicking on the checkbox fails", func() {
It("should return an error", func() {
secondElement.ClickCall.Err = errors.New("some error")
Expect(selection.Check()).To(MatchError("failed to click on selection 'CSS: #selector': some error"))
})
})
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Check()).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when any element fails to retrieve the 'type' attribute", func() {
It("should return an error", func() {
firstElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.GetAttributeCall.Err = errors.New("some error")
Expect(selection.Check()).To(MatchError("failed to retrieve type attribute of selection 'CSS: #selector': some error"))
})
})
Context("when any element is not a checkbox", func() {
It("should return an error", func() {
firstElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.GetAttributeCall.ReturnValue = "banana"
Expect(selection.Check()).To(MatchError("selection 'CSS: #selector' does not refer to a checkbox"))
})
})
})
Describe("#Uncheck", func() {
It("should successfully click on a checked checkbox", func() {
firstElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.GetAttributeCall.ReturnValue = "checkbox"
secondElement.IsSelectedCall.ReturnSelected = true
Expect(selection.Uncheck()).To(Succeed())
Expect(firstElement.ClickCall.Called).To(BeFalse())
Expect(secondElement.ClickCall.Called).To(BeTrue())
})
})
Describe("#Select", func() {
var (
firstOptionBuses []*mocks.Bus
secondOptionBuses []*mocks.Bus
firstOptions []*api.Element
secondOptions []*api.Element
)
BeforeEach(func() {
firstOptionBuses = []*mocks.Bus{{}, {}}
secondOptionBuses = []*mocks.Bus{{}, {}}
firstOptions = []*api.Element{
{ID: "one", Session: &api.Session{Bus: firstOptionBuses[0]}},
{ID: "two", Session: &api.Session{Bus: firstOptionBuses[1]}},
}
secondOptions = []*api.Element{
{ID: "three", Session: &api.Session{Bus: secondOptionBuses[0]}},
{ID: "four", Session: &api.Session{Bus: secondOptionBuses[1]}},
}
firstElement.GetElementsCall.ReturnElements = []*api.Element{firstOptions[0], firstOptions[1]}
secondElement.GetElementsCall.ReturnElements = []*api.Element{secondOptions[0], secondOptions[1]}
})
It("should successfully retrieve the options with matching text for each selected element", func() {
Expect(selection.Select("some text")).To(Succeed())
Expect(firstElement.GetElementsCall.Selector.Using).To(Equal("xpath"))
Expect(firstElement.GetElementsCall.Selector.Value).To(Equal(`./option[normalize-space()="some text"]`))
Expect(secondElement.GetElementsCall.Selector.Using).To(Equal("xpath"))
Expect(secondElement.GetElementsCall.Selector.Value).To(Equal(`./option[normalize-space()="some text"]`))
})
It("should successfully click on all options with matching text", func() {
Expect(selection.Select("some text")).To(Succeed())
Expect(firstOptionBuses[0].SendCall.Endpoint).To(Equal("element/one/click"))
Expect(firstOptionBuses[1].SendCall.Endpoint).To(Equal("element/two/click"))
Expect(secondOptionBuses[0].SendCall.Endpoint).To(Equal("element/three/click"))
Expect(secondOptionBuses[1].SendCall.Endpoint).To(Equal("element/four/click"))
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Select("some text")).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when we fail to retrieve any option", func() {
It("should return an error", func() {
secondElement.GetElementsCall.Err = errors.New("some error")
Expect(selection.Select("some text")).To(MatchError("failed to select specified option for selection 'CSS: #selector': some error"))
})
})
Context("when any of the elements has no options with matching text", func() {
It("should return an error", func() {
secondElement.GetElementsCall.ReturnElements = []*api.Element{}
Expect(selection.Select("some text")).To(MatchError(`no options with text "some text" found for selection 'CSS: #selector'`))
})
})
Context("when the click fails for any of the options", func() {
It("should return an error", func() {
secondOptionBuses[1].SendCall.Err = errors.New("some error")
Expect(selection.Select("some text")).To(MatchError(`failed to click on option with text "some text" for selection 'CSS: #selector': some error`))
})
})
})
Describe("#Submit", func() {
It("should successfully submit all selected elements", func() {
Expect(selection.Submit()).To(Succeed())
Expect(firstElement.SubmitCall.Called).To(BeTrue())
Expect(secondElement.SubmitCall.Called).To(BeTrue())
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Submit()).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when any submit fails", func() {
It("should return an error", func() {
secondElement.SubmitCall.Err = errors.New("some error")
Expect(selection.Submit()).To(MatchError("failed to submit selection 'CSS: #selector': some error"))
})
})
})
// TODO: implement call tracking in mocks
Describe("#Tap", func() {
var (
firstElement *api.Element
secondElement *api.Element
)
BeforeEach(func() {
firstElement = &api.Element{}
secondElement = &api.Element{}
elementRepository.GetAtLeastOneCall.ReturnElements = []element.Element{firstElement, secondElement}
})
It("should successfully tap on all selected elements for each event type", func() {
Expect(selection.Tap(SingleTap)).To(Succeed())
Expect(session.TouchClickCall.Element).To(ExactlyEqual(secondElement))
Expect(selection.Tap(DoubleTap)).To(Succeed())
Expect(session.TouchDoubleClickCall.Element).To(ExactlyEqual(secondElement))
Expect(selection.Tap(LongTap)).To(Succeed())
Expect(session.TouchLongClickCall.Element).To(ExactlyEqual(secondElement))
})
Context("when the tap event is invalid", func() {
It("should return an error", func() {
err := selection.Tap(-1)
Expect(err).To(MatchError("failed to perform tap on selection 'CSS: #selector': invalid tap event"))
})
})
Context("when zero elements are returned", func() {
It("should return an error", func() {
elementRepository.GetAtLeastOneCall.Err = errors.New("some error")
Expect(selection.Tap(SingleTap)).To(MatchError("failed to select elements from selection 'CSS: #selector': some error"))
})
})
Context("when any tap fails", func() {
It("should return an error", func() {
session.TouchClickCall.Err = errors.New("some error")
Expect(selection.Tap(SingleTap)).To(MatchError("failed to tap on selection 'CSS: #selector': some error"))
})
})
})
// TODO: implement call tracking in mocks
Describe("#Touch", func() {
It("should successfully instruct the session to touch using the provided offset for each event type", func() {
firstElement.GetLocationCall.ReturnX = 100
firstElement.GetLocationCall.ReturnY = 200
secondElement.GetLocationCall.ReturnX = 300
secondElement.GetLocationCall.ReturnY = 400
Expect(selection.Touch(HoldFinger)).To(Succeed())
Expect(session.TouchDownCall.X).To(Equal(300))
Expect(session.TouchDownCall.Y).To(Equal(400))
Expect(selection.Touch(ReleaseFinger)).To(Succeed())
Expect(session.TouchUpCall.X).To(Equal(300))
Expect(session.TouchUpCall.Y).To(Equal(400))
Expect(selection.Touch(MoveFinger)).To(Succeed())
Expect(session.TouchMoveCall.X).To(Equal(300))
Expect(session.TouchMoveCall.Y).To(Equal(400))
})
Context("when retrieving an element's location fails", func() {
It("should return an error", func() {
secondElement.GetLocationCall.Err = errors.New("some error")
Expect(selection.Touch(HoldFinger)).To(MatchError("failed to retrieve location of selection 'CSS: #selector': some error"))
})
})
Context("when the touch event fails", func() {
It("should return an error of each event type", func() {
session.TouchDownCall.Err = errors.New("some touch down error")
Expect(selection.Touch(HoldFinger)).To(MatchError("failed to flick finger on selection 'CSS: #selector': some touch down error"))
session.TouchUpCall.Err = errors.New("some touch up error")
Expect(selection.Touch(ReleaseFinger)).To(MatchError("failed to flick finger on selection 'CSS: #selector': some touch up error"))
session.TouchMoveCall.Err = errors.New("some touch move error")
Expect(selection.Touch(MoveFinger)).To(MatchError("failed to flick finger on selection 'CSS: #selector': some touch move error"))
})
})
Context("when the touch event is invalid", func() {
It("should return an error", func() {
err := selection.Touch(-1)
Expect(err).To(MatchError("failed to perform touch on selection 'CSS: #selector': invalid touch event"))
})
})
})
Describe("#FlickFinger", func() {
var firstElement *api.Element
BeforeEach(func() {
firstElement = &api.Element{}
elementRepository.GetExactlyOneCall.ReturnElement = firstElement
})
It("should successfully flick on the selected element", func() {
Expect(selection.FlickFinger(100, 200, 300)).To(Succeed())
Expect(session.TouchFlickCall.Element).To(ExactlyEqual(firstElement))
Expect(session.TouchFlickCall.Offset).To(Equal(api.XYOffset{X: 100, Y: 200}))
Expect(session.TouchFlickCall.Speed).To(Equal(api.ScalarSpeed(300)))
})
Context("when exactly one element is not returned", func() {
It("should return an error", func() {
elementRepository.GetExactlyOneCall.Err = errors.New("some error")
Expect(selection.FlickFinger(100, 200, 300)).To(MatchError("failed to select element from selection 'CSS: #selector': some error"))
})
})
Context("when the flick fails", func() {
It("should return an error", func() {
session.TouchFlickCall.Err = errors.New("some error")
Expect(selection.FlickFinger(100, 200, 300)).To(MatchError("failed to flick finger on selection 'CSS: #selector': some error"))
})
})
})
Describe("#ScrollFinger", func() {
var firstElement *api.Element
BeforeEach(func() {
firstElement = &api.Element{}
elementRepository.GetExactlyOneCall.ReturnElement = firstElement
})
It("should successfully scroll on the selected element", func() {
Expect(selection.ScrollFinger(100, 200)).To(Succeed())
Expect(session.TouchScrollCall.Element).To(ExactlyEqual(firstElement))
Expect(session.TouchScrollCall.Offset).To(Equal(api.XYOffset{X: 100, Y: 200}))
})
Context("when exactly one element is not returned", func() {
It("should return an error", func() {
elementRepository.GetExactlyOneCall.Err = errors.New("some error")
Expect(selection.ScrollFinger(100, 200)).To(MatchError("failed to select element from selection 'CSS: #selector': some error"))
})
})
Context("when the scroll fails", func() {
It("should return an error", func() {
session.TouchScrollCall.Err = errors.New("some error")
Expect(selection.ScrollFinger(100, 200)).To(MatchError("failed to scroll finger on selection 'CSS: #selector': some error"))
})
})
})
})