Repository navigation
Expand file tree
/
Copy pathdom_test.go
More file actions
2598 lines (2183 loc) · 107 KB
/
Copy pathdom_test.go
File metadata and controls
2598 lines (2183 loc) · 107 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package biloba_test
import (
"strconv"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("DOM manipulators and matchers", func() {
BeforeEach(func() {
b.Navigate(fixtureServer + "/dom.html")
Eventually("#hello").Should(b.Exist())
})
Describe("exist", func() {
It("does not match when the dom element does not exist", func() {
Ω("#non-existing").ShouldNot(b.Exist())
})
It("does not match when the dom element exists", func() {
Ω("#hello").Should(b.Exist())
Ω("#hidden-parent").Should(b.Exist())
})
It("matches eventually if a dom element pops into existence", func() {
Consistently("#say-when").ShouldNot(b.Exist())
b.Run("bilobaSaysWhen()")
Eventually("#say-when").Should(b.Exist())
})
It("errors if the selector is malformed", func() {
match, err := b.Exist().Match(b.XPath("//[blarg]"))
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError(ContainSubstring("'//[blarg]' is not a valid XPath expression")))
})
})
Describe("counting elements", func() {
It("count returns the number of elements", func() {
Ω(b.Count("#non-existing")).Should(Equal(0))
Ω(b.Count("#hello")).Should(Equal(1))
Ω(b.Count(b.XPath("div").WithID("hidden-parent").Descendant())).Should(Equal(4))
Ω(b.Count("input[type='radio']")).Should(Equal(10))
})
It("HaveCount does the same, as a matcher", func() {
Ω("#non-existing").Should(b.HaveCount(0))
Ω("#hello").Should(b.HaveCount(1))
Ω(b.XPath("div").WithID("hidden-parent").Descendant()).Should(b.HaveCount(4))
Ω("input[type='radio']").Should(b.HaveCount(10))
matcher := b.HaveCount(BeNumerically("<", 8))
match, err := matcher.Match("input[type='radio']")
Ω(match).Should(BeFalse())
Ω(err).Should(BeNil())
Ω(matcher.FailureMessage("input[type='radio']")).Should(Equal("HaveCount for input[type='radio']:\nExpected\n <int>: 10\nto be <\n <int>: 8"))
})
})
Describe("HasElement()", func() {
It("return true when the element exists", func() {
Ω(b.HasElement("#hello")).Should(BeTrue())
Ω(b.HasElement(b.XPath().WithID("hello"))).Should(BeTrue())
})
It("returns true when the element is hidden", func() {
Ω(b.HasElement("#hidden-parent")).Should(BeTrue())
})
It("returns false when the element does not exist", func() {
Ω(b.HasElement("#non-existing")).Should(BeFalse())
})
It("errors when the selector is malformed", func() {
b.HasElement(b.XPath("//[blarg]"))
ExpectFailures(ContainSubstring("'//[blarg]' is not a valid XPath expression"))
})
})
Describe("BeVisible", func() {
It("matches when the element is visible", func() {
Ω("#hello").Should(b.BeVisible())
})
It("does not match when the element is hidden", func() {
Ω("#hidden-parent").ShouldNot(b.BeVisible())
})
It("does not match when a child element is hidden", func() {
Ω("#hidden-child").ShouldNot(b.BeVisible())
Ω("#fixed-hidden-child").ShouldNot(b.BeVisible())
Ω("#hidden-button").ShouldNot(b.BeVisible())
})
It("errors when the element does not exist", func() {
match, err := b.BeVisible().Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("BeEnabled", func() {
It("matches when the element is enabled", func() {
Ω("#increment").Should(b.BeEnabled())
})
It("matches when the element is enabled even if it is hidden", func() {
Ω("#hidden-button").ShouldNot(b.BeVisible())
Ω("#hidden-button").Should(b.BeEnabled())
})
It("does not match when the element is disabled", func() {
Ω("#decrement").ShouldNot(b.BeEnabled())
})
It("errors when the element does not exist", func() {
match, err := b.BeEnabled().Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("EachBeVisible", func() {
It("matches when every element is visible", func() {
Ω(".each-vis:not(.hidden)").Should(b.EachBeVisible())
})
It("does not match when any element is hidden", func() {
Ω(".each-vis").ShouldNot(b.EachBeVisible())
})
It("fails (does not pass vacuously) when no elements match the selector", func() {
m := b.EachBeVisible()
Ω(".non-existing").ShouldNot(m)
Ω(m.FailureMessage(".non-existing")).Should(ContainSubstring("Expected at least one element to match .non-existing, but none did"))
})
It("fails the spec via the immediate-failure path of a captured failure", func() {
match, err := b.EachBeVisible().Match(".each-vis")
Ω(match).Should(BeFalse())
Ω(err).Should(BeNil())
Ω(b.EachBeVisible().FailureMessage(".each-vis")).Should(ContainSubstring("each be visible"))
})
})
Describe("EachBeEnabled", func() {
It("matches when every element is enabled", func() {
Ω(".each-en:not([disabled])").Should(b.EachBeEnabled())
})
It("does not match when any element is disabled", func() {
Ω(".each-en").ShouldNot(b.EachBeEnabled())
})
It("matches enabled elements even when some are hidden", func() {
Ω(".each-vis").Should(b.EachBeEnabled())
})
It("fails (does not pass vacuously) when no elements match the selector", func() {
m := b.EachBeEnabled()
Ω(".non-existing").ShouldNot(m)
Ω(m.FailureMessage(".non-existing")).Should(ContainSubstring("Expected at least one element to match .non-existing, but none did"))
})
})
Describe("EachHaveClass", func() {
It("matches when every element has the class", func() {
Ω(".each-vis").Should(b.EachHaveClass("tagged"))
Ω(".each-vis").Should(b.EachHaveClass("each-vis"))
})
It("does not match when any element lacks the class", func() {
Ω("#each-group > *").ShouldNot(b.EachHaveClass("tagged"))
})
It("fails (does not pass vacuously) when no elements match the selector", func() {
m := b.EachHaveClass("tagged")
Ω(".non-existing").ShouldNot(m)
Ω(m.FailureMessage(".non-existing")).Should(ContainSubstring("Expected at least one element to match .non-existing, but none did"))
})
})
Describe("GetInnerText", func() {
It("returns the InnerText of the element", func() {
Ω(b.GetInnerText("#hello")).Should(Equal("Hello Biloba!"))
Ω(b.GetInnerText("#hidden-child")).Should(Equal("Can't see me!"))
Ω(b.GetInnerText("#list")).Should(Equal("First Things\nSecond Things\nThird Things"))
})
It("times out (poll-by-default) if the element never exists", func() {
b.WithTimeout(time.Millisecond * 60).GetInnerText("#non-existing")
ExpectFailures(ContainSubstring("Timed out after"))
})
It("fails fast under Immediate() if the element does not exist", func() {
Ω(b.Immediate().GetInnerText("#non-existing")).Should(Equal(""))
ExpectFailures(ContainSubstring("have property \"innerText\""))
})
})
Describe("CurrentInnerTextForEach", func() {
It("returns the InnerText of the element", func() {
Ω(b.CurrentInnerTextForEach(b.XPath().WithID("party").Descendant("optgroup").WithAttr("label", "Heros").Descendant("option"))).Should(HaveExactElements("Luke", "Leia", "Han", "Obi-Wan"))
Ω(b.CurrentInnerTextForEach("#list li")).Should(HaveExactElements("First Things", "Second Things", "Third Things"))
})
It("returns an empty slice if no elements exist", func() {
Ω(b.CurrentInnerTextForEach(".non-existing")).Should(BeEmpty())
})
})
Describe("HaveInnerText", func() {
It("matches if the element in question has the specified inner text", func() {
Ω("#hello").Should(b.HaveInnerText("Hello Biloba!"))
Ω("#hidden-child").Should(b.HaveInnerText("Can't see me!"))
Ω("#hello").ShouldNot(b.HaveInnerText("nope"))
})
It("works with matchers", func() {
Ω("#list").Should(b.HaveInnerText(ContainSubstring("Second Things")))
})
It("has a reasonable failure message", func() {
matcher := b.HaveInnerText("Hello")
match, err := matcher.Match("#hello")
Ω(match).Should(BeFalse())
Ω(err).ShouldNot(HaveOccurred())
Ω(matcher.FailureMessage("#hello")).Should(Equal("HaveProperty \"innerText\" for #hello:\nExpected\n <string>: Hello Biloba!\nto equal\n <string>: Hello"))
Ω(matcher.NegatedFailureMessage("#hello")).Should(Equal("HaveProperty \"innerText\" for #hello:\nExpected\n <string>: Hello Biloba!\nnot to equal\n <string>: Hello"))
nestedMatcher := b.HaveInnerText(ContainSubstring("Fourth Things"))
match, err = nestedMatcher.Match("#list")
Ω(match).Should(BeFalse())
Ω(err).ShouldNot(HaveOccurred())
Ω(nestedMatcher.FailureMessage("#list")).Should(Equal("HaveProperty \"innerText\" for #list:\nExpected\n <string>: First Things\n Second Things\n Third Things\nto contain substring\n <string>: Fourth Things"))
})
It("errors if the element does not exist", func() {
match, err := b.HaveInnerText("").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("EachHaveInnerText", func() {
It("matches if the elements in question have the specified inner text", func() {
Ω(b.XPath().WithID("party").Descendant("optgroup").WithAttr("label", "Heros").Descendant("option")).Should(b.EachHaveInnerText("Luke", "Leia", "Han", "Obi-Wan"))
Ω("#list li").Should(b.EachHaveInnerText(ConsistOf("Second Things", "First Things", "Third Things")))
Ω("#list li").Should(b.EachHaveInnerText(ContainElement("Second Things")))
})
It("hands the sub-matcher a []string, so Equal([]string{...}) and HaveExactElements pin order and length the way they look like they do", func() {
// this used to be impossible: the sub-matcher saw the raw []any Chrome returns, so
// Equal([]string{...}) could never match despite looking exactly right
Ω("#list li").Should(b.EachHaveInnerText(Equal([]string{"First Things", "Second Things", "Third Things"})))
Ω("#list li").Should(b.EachHaveInnerText(HaveExactElements("First Things", "Second Things", "Third Things")))
})
It("captures into a []string target, and into a *[]any target via the JSON decode path", func() {
var texts []string
Ω("#list li").Should(b.EachHaveInnerText(Equal([]string{"First Things", "Second Things", "Third Things"})).Capture(&texts))
Ω(texts).Should(Equal([]string{"First Things", "Second Things", "Third Things"}))
var anyTexts []any
Ω("#list li").Should(b.EachHaveInnerText(HaveLen(3)).Capture(&anyTexts))
Ω(anyTexts).Should(HaveExactElements("First Things", "Second Things", "Third Things"))
})
It("names itself in failure output, not the generic EachHaveProperty", func() {
m := b.EachHaveInnerText(Equal([]string{"nope"}))
Ω("#list li").ShouldNot(m)
Ω(m.FailureMessage("#list li")).Should(ContainSubstring("EachHaveInnerText for #list li:"))
Ω(m.FailureMessage("#list li")).ShouldNot(ContainSubstring("EachHaveProperty"))
})
It("fails (does not pass vacuously) when no elements match the selector", func() {
m := b.EachHaveInnerText("anything")
Ω("#non-existing").ShouldNot(m)
Ω(m.FailureMessage("#non-existing")).Should(ContainSubstring("Expected at least one element to match #non-existing, but none did"))
// to assert that nothing matches, use HaveCount(0) instead
Ω("#non-existing").Should(b.HaveCount(0))
})
})
Describe("GetTextContent", func() {
It("returns the textContent of the element - including hidden content", func() {
Ω(b.GetTextContent("#hello")).Should(Equal("Hello Biloba!"))
// textContent reads straight from the DOM tree, so it sees hidden elements just the same
Ω(b.GetTextContent("#hidden-child")).Should(Equal("Can't see me!"))
// unlike innerText, textContent does not collapse block layout into newlines - it returns
// the raw template whitespace, so #list comes back with each <li> on its own indented line
Ω(b.GetTextContent("#list")).Should(ContainSubstring("First Things"))
Ω(b.GetTextContent("#list")).Should(ContainSubstring("Second Things"))
Ω(b.GetTextContent("#list")).Should(ContainSubstring("Third Things"))
})
It("times out (poll-by-default) if the element never exists", func() {
b.WithTimeout(time.Millisecond * 60).GetTextContent("#non-existing")
ExpectFailures(ContainSubstring("Timed out after"))
})
It("fails fast under Immediate() if the element does not exist", func() {
Ω(b.Immediate().GetTextContent("#non-existing")).Should(Equal(""))
ExpectFailures(ContainSubstring("have property \"textContent\""))
})
})
Describe("CurrentTextContentForEach", func() {
It("returns the textContent of each element", func() {
Ω(b.CurrentTextContentForEach(b.XPath().WithID("party").Descendant("optgroup").WithAttr("label", "Heros").Descendant("option"))).Should(HaveExactElements("Luke", "Leia", "Han", "Obi-Wan"))
Ω(b.CurrentTextContentForEach("#list li")).Should(HaveExactElements("First Things", "Second Things", "Third Things"))
})
It("returns an empty slice if no elements exist", func() {
Ω(b.CurrentTextContentForEach(".non-existing")).Should(BeEmpty())
})
})
Describe("HaveTextContent", func() {
It("matches if the element in question has the specified text content", func() {
Ω("#hello").Should(b.HaveTextContent("Hello Biloba!"))
Ω("#hidden-child").Should(b.HaveTextContent("Can't see me!"))
Ω("#hello").ShouldNot(b.HaveTextContent("nope"))
})
It("works with matchers", func() {
Ω("#list").Should(b.HaveTextContent(ContainSubstring("Second Things")))
})
It("errors if the element does not exist", func() {
match, err := b.HaveTextContent("").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("EachHaveTextContent", func() {
It("matches if the elements in question have the specified text content", func() {
Ω(b.XPath().WithID("party").Descendant("optgroup").WithAttr("label", "Heros").Descendant("option")).Should(b.EachHaveTextContent("Luke", "Leia", "Han", "Obi-Wan"))
Ω("#list li").Should(b.EachHaveTextContent(ConsistOf("Second Things", "First Things", "Third Things")))
Ω("#list li").Should(b.EachHaveTextContent(ContainElement("Second Things")))
})
It("hands the sub-matcher a []string, so Equal([]string{...}) and HaveExactElements pin order and length the way they look like they do", func() {
// this used to be impossible: the sub-matcher saw the raw []any Chrome returns, so
// Equal([]string{...}) could never match despite looking exactly right
Ω("#list li").Should(b.EachHaveTextContent(Equal([]string{"First Things", "Second Things", "Third Things"})))
Ω("#list li").Should(b.EachHaveTextContent(HaveExactElements("First Things", "Second Things", "Third Things")))
})
It("captures into a []string target, and into a *[]any target via the JSON decode path", func() {
var texts []string
Ω("#list li").Should(b.EachHaveTextContent(Equal([]string{"First Things", "Second Things", "Third Things"})).Capture(&texts))
Ω(texts).Should(Equal([]string{"First Things", "Second Things", "Third Things"}))
var anyTexts []any
Ω("#list li").Should(b.EachHaveTextContent(HaveLen(3)).Capture(&anyTexts))
Ω(anyTexts).Should(HaveExactElements("First Things", "Second Things", "Third Things"))
})
It("names itself in failure output, not the generic EachHaveProperty", func() {
m := b.EachHaveTextContent(Equal([]string{"nope"}))
Ω("#list li").ShouldNot(m)
Ω(m.FailureMessage("#list li")).Should(ContainSubstring("EachHaveTextContent for #list li:"))
Ω(m.FailureMessage("#list li")).ShouldNot(ContainSubstring("EachHaveProperty"))
})
It("fails (does not pass vacuously) when no elements match the selector", func() {
m := b.EachHaveTextContent("anything")
Ω("#non-existing").ShouldNot(m)
Ω(m.FailureMessage("#non-existing")).Should(ContainSubstring("Expected at least one element to match #non-existing, but none did"))
// to assert that nothing matches, use HaveCount(0) instead
Ω("#non-existing").Should(b.HaveCount(0))
})
})
Describe("Working with inputs that honor value", func() {
Describe("GetValue", func() {
It("returns the value associated with the input", func() {
Ω(b.GetValue("#hidden-text-input")).Should(Equal("my-hidden-value"))
Ω(b.GetValue("#counter-input")).Should(Equal("0"))
Ω(b.GetValue("#disabled-text-input")).Should(Equal("i'm off"))
Ω(b.GetValue("#text-area")).Should(Equal("Something long"))
Ω(b.GetValue("#droid")).Should(Equal("r2d2"))
})
It("polls until the element appears, then returns its value", func() {
go func() {
defer GinkgoRecover()
<-time.After(time.Millisecond * 100)
b.Run(`document.querySelector("#text-area").id = "delayed-text-area"`)
}()
Ω(b.GetValue("#delayed-text-area")).Should(Equal("Something long"))
})
It("treats an empty value as valid (does not wait for it to be non-empty)", func() {
b.SetValue("#text-input", "")
Ω(b.GetValue("#text-input")).Should(Equal(""))
})
It("times out (poll-by-default) if the element never exists", func() {
b.WithTimeout(time.Millisecond * 60).GetValue("#non-existing")
ExpectFailures(SatisfyAll(
ContainSubstring("Timed out after"),
ContainSubstring("have a value"),
))
})
It("fails fast under Immediate() if the element does not exist", func() {
Ω(b.Immediate().GetValue("#non-existing")).Should(BeNil())
ExpectFailures(ContainSubstring("have a value"))
})
})
Describe("CurrentValueForEach", func() {
It("returns a snapshot of the rationalized value for all matching elements", func() {
Ω(b.CurrentValueForEach("#check-boxes input[type='checkbox']")).Should(HaveExactElements(true, false, false, false))
b.SetPropertyForEachImmediately("#check-boxes input[type='checkbox']", "checked", true)
Ω(b.CurrentValueForEach("#check-boxes input[type='checkbox']")).Should(HaveExactElements(true, true, true, true))
})
It("returns an empty slice when no elements are found (it does not poll)", func() {
Ω(b.CurrentValueForEach("#non-existing")).Should(BeEmpty())
})
})
Describe("HaveValue", func() {
It("matches if returned value matches", func() {
Ω("#hidden-text-input").Should(b.HaveValue("my-hidden-value"))
Ω("#counter-input").Should(b.HaveValue("0"))
Ω("#disabled-text-input").Should(b.HaveValue("i'm off"))
Ω("#text-area").Should(b.HaveValue("Something long"))
Ω("#droid").Should(b.HaveValue("r2d2"))
})
It("works with nested matchers", func() {
Ω("#counter-input").Should(b.HaveValue(WithTransform(strconv.Atoi, BeNumerically("==", 0))))
Ω("#counter-input").ShouldNot(b.HaveValue(WithTransform(strconv.Atoi, BeNumerically("==", 10))))
matcher := b.HaveValue(WithTransform(strconv.Atoi, BeNumerically("==", 1)))
match, err := matcher.Match("#counter-input")
Ω(err).ShouldNot(HaveOccurred())
Ω(match).Should(BeFalse())
Ω(matcher.FailureMessage("#counter-input")).Should(Equal("HaveValue for #counter-input:\nExpected\n <int>: 0\nto be ==\n <int>: 1"))
})
It("errors if the DOM element does not exist", func() {
match, err := b.HaveValue("foo").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("SetValue", func() {
Context("when called directly", func() {
It("sets the value correctly", func() {
Eventually("#text-input-mirror").Should(b.HaveInnerText("initial value"))
Ω("#text-input").Should(b.HaveValue("initial value"))
b.SetValue("#text-input", "new value")
Ω("#text-input").Should(b.HaveValue("new value"))
Ω("#text-input-mirror").Should(b.HaveInnerText("new value"))
Ω("#counter-input").Should(b.HaveValue("0"))
b.SetValue("#counter-input", 3)
Ω("#counter-input").Should(b.HaveValue("3"))
b.SetValue("#text-area", "Something even longer")
Ω("#text-area").Should(b.HaveValue("Something even longer"))
b.SetValue("#droid", "bb8")
Ω("#droid").Should(b.HaveValue("bb8"))
Ω(b.XPath("option").WithAttr("value", "bb8")).Should(b.HaveProperty("selected", BeTrue()))
})
It("times out (poll-by-default) if the element never exists", func() {
b.WithTimeout(time.Millisecond*60).SetValue("#non-existing", "foo")
ExpectFailures(SatisfyAll(
ContainSubstring("Timed out after"),
ContainSubstring("could not find DOM element matching selector: #non-existing"),
))
})
It("fails fast under Immediate() if the element does not exist", func() {
b.Immediate().SetValue("#non-existing", "foo")
ExpectFailures(ContainSubstring("could not find DOM element matching selector: #non-existing"))
})
It("fails fast under Immediate() if the element is not visible", func() {
b.Immediate().SetValue("#hidden-text-input", "foo")
ExpectFailures(ContainSubstring("DOM element is not visible: #hidden-text-input"))
Ω("#hidden-text-input").Should(b.HaveValue("my-hidden-value"))
})
It("fails fast under Immediate() if the element is not enabled", func() {
b.Immediate().SetValue("#disabled-text-input", "foo")
ExpectFailures(ContainSubstring("DOM element is not enabled: #disabled-text-input"))
Ω("#disabled-text-input").Should(b.HaveValue("i'm off"))
})
It("fails if attempting to set the value of a select input to an option that does not exist", func() {
b.Immediate().SetValue("#droid", "grogu")
ExpectFailures(ContainSubstring("Select input does not have option with value \"grogu\": #droid"))
})
It("is a hard error to configure the bare-matcher form", func() {
b.WithTimeout(time.Second).SetValue("foo")
ExpectFailures(ContainSubstring("SetValue(...) returns a matcher"))
})
})
Context("when used as a matcher", func() {
It("sets the values correctly", func() {
Eventually("#text-input-mirror").Should(b.HaveInnerText("initial value"))
Ω("#text-input").Should(b.HaveValue("initial value"))
Ω("#text-input").Should(b.SetValue("new value"))
Ω("#text-input").Should(b.HaveValue("new value"))
Ω("#text-input-mirror").Should(b.HaveInnerText("new value"))
Ω("#counter-input").Should(b.HaveValue("0"))
Ω("#counter-input").Should(b.SetValue(3))
Ω("#counter-input").Should(b.HaveValue("3"))
Ω("#text-area").Should(b.SetValue("Something even longer"))
Ω("#text-area").Should(b.HaveValue("Something even longer"))
Ω("#droid").Should(b.SetValue("bb8"))
Ω("#droid").Should(b.HaveValue("bb8"))
Ω(b.XPath("option").WithAttr("value", "bb8")).Should(b.HaveProperty("selected", BeTrue()))
})
It("retries when called in an eventually", func() {
Eventually("#disabled-text-input-mirror").Should(b.HaveInnerText("i'm off"))
Ω("#disabled-text-input").ShouldNot(b.BeEnabled())
b.Run("enableTextInput()")
Eventually("#disabled-text-input").Should(b.SetValue("i'm on"))
Ω("#disabled-text-input").Should(b.HaveValue("i'm on"))
Ω("#disabled-text-input-mirror").Should(b.HaveInnerText("i'm on"))
})
It("returns an error when the element does not exist", func() {
match, err := b.SetValue("foo").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
It("returns an error when the element is not visible", func() {
match, err := b.SetValue("foo").Match("#hidden-text-input")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("DOM element is not visible: #hidden-text-input"))
})
It("returns an error when the element is not enabled", func() {
match, err := b.SetValue("foo").Match("#disabled-text-input")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("DOM element is not enabled: #disabled-text-input"))
})
It("fails if attempting to set the value of a select input to an option that does not exist", func() {
match, err := b.SetValue("grogu").Match("#droid")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Select input does not have option with value \"grogu\": #droid"))
})
})
Context("when passed a ValueLabel", func() {
It("selects a single-select option by its visible label", func() {
b.SetValue("#droid", b.ValueLabel("BB-8"))
Ω("#droid").Should(b.HaveValue("bb8"))
Ω("#droid").Should(b.SetValue(b.ValueLabel("C-3PO")))
Ω("#droid").Should(b.HaveValue("c3po"))
})
It("selects multi-select options by label, mixing labels and raw values", func() {
b.SetValue("#party", []any{b.ValueLabel("Obi-Wan"), "han", b.ValueLabel("The Emperor")})
Ω(b.GetValue("#party")).Should(ConsistOf("obi-wan", "han", "emperor"))
})
It("fails when no option has the given label", func() {
b.Immediate().SetValue("#droid", b.ValueLabel("Grogu"))
ExpectFailures(ContainSubstring("Select input does not have option with label \"Grogu\": #droid"))
})
It("fails when used on a non-select element", func() {
b.Immediate().SetValue("#text-input", b.ValueLabel("nope"))
ExpectFailures(ContainSubstring("ValueLabel is only supported for <select> elements: #text-input"))
})
})
Context("when the input is a controlled (React/Vue-style) input", func() {
It("drives the value past the framework's value tracker", func() {
// #react-controlled installs a value tracker that shadows the element's own value setter
// and only commits when the change is seen via the native prototype setter. A raw
// `n.value = v` would leave window._reactCommitted at "".
Ω(b.Run("window._reactCommitted")).Should(Equal(""))
b.SetValue("#react-controlled", "chloroplast")
Ω("#react-controlled").Should(b.HaveValue("chloroplast"))
Eventually("window._reactCommitted").Should(b.EvaluateTo("chloroplast"))
})
})
Context("when setting a text input", func() {
It("does not blur the input (so onBlur handlers do not fire)", func() {
Ω(b.Run("window._blurFired")).Should(BeFalse())
b.SetValue("#blur-tracker", "hi")
Ω("#blur-tracker").Should(b.HaveValue("hi"))
Consistently("window._blurFired").Should(b.EvaluateTo(false))
})
})
})
})
Describe("Working with Checkboxes", func() {
BeforeEach(func() {
Eventually("#checked-color").Should(b.HaveInnerText("red"))
})
It("returns booleans", func() {
Ω(b.GetValue("#red")).Should(BeTrue())
Ω(b.GetValue("#blue")).Should(BeFalse())
})
Context("when setting values directly", func() {
It("sets the checkboxes correctly", func() {
b.SetValue("#red", true)
Ω("#red").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("red"))
b.SetValue("#red", false)
Ω("#red").Should(b.HaveValue(false))
Ω("#checked-color").Should(b.HaveInnerText("black"))
b.SetValue("#blue", true)
Ω("#blue").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("blue"))
b.SetValue("#red", true)
Ω("#red").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("purple"))
})
It("fails fast under Immediate() if the element is not visible", func() {
b.Immediate().SetValue("#green", true)
ExpectFailures(ContainSubstring("DOM element is not visible: #green"))
Ω("#checked-color").Should(b.HaveInnerText("red"))
})
It("fails fast under Immediate() if the element is not enabled", func() {
b.Immediate().SetValue("#yellow", true)
ExpectFailures(ContainSubstring("DOM element is not enabled: #yellow"))
Ω("#checked-color").Should(b.HaveInnerText("red"))
})
It("fails if not provided a boolean value", func() {
b.Immediate().SetValue("#red", "true")
ExpectFailures(ContainSubstring("Checkboxes only accept boolean values: #red"))
})
})
Context("when setting values as a matcher", func() {
It("sets the checkboxes correctly", func() {
Ω("#red").Should(b.SetValue(true))
Ω("#red").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("red"))
Ω("#red").Should(b.SetValue(false))
Ω("#red").Should(b.HaveValue(false))
Ω("#checked-color").Should(b.HaveInnerText("black"))
Ω("#blue").Should(b.SetValue(true))
Ω("#blue").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("blue"))
Ω("#red").Should(b.SetValue(true))
Ω("#red").Should(b.HaveValue(true))
Ω("#checked-color").Should(b.HaveInnerText("purple"))
})
It("retries when called in an eventually", func() {
Ω("#yellow").ShouldNot(Or(b.HaveValue(true), b.BeEnabled()))
Ω("#checked-color").Should(b.HaveInnerText("red"))
b.Run("enableYellow()")
Eventually("#yellow").Should(b.SetValue(true))
Ω("#checked-color").Should(b.HaveInnerText("yellow"))
})
It("returns an error when the element is not visible", func() {
match, err := b.SetValue(true).Match("#green")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("DOM element is not visible: #green"))
})
It("returns an error when the element is not enabled", func() {
match, err := b.SetValue(true).Match("#yellow")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("DOM element is not enabled: #yellow"))
})
It("returns an error when not provided a boolean value", func() {
match, err := b.SetValue("true").Match("#red")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Checkboxes only accept boolean values: #red"))
})
})
})
Describe("working with radio buttons", func() {
It("returns the value of the group, regardless of which radio button is selected", func() {
Ω(b.GetValue("input[name='appliances']")).Should(Equal("toaster"))
Ω(b.GetValue("input[name='appliances'][value='stove']")).Should(Equal("toaster"))
Ω(b.GetValue("input[name='transportation']")).Should(Equal("hovercraft"))
Ω("input[name='appliances']").Should(b.HaveValue("toaster"))
Ω("input[name='transportation'][value='bike']").Should(b.HaveValue("hovercraft"))
})
It("returns nil if no options is selected", func() {
Ω(b.GetValue("input[name='turtle']")).Should(BeNil())
Ω("input[name='turtle']").Should(b.HaveValue(BeNil()))
})
Context("when setting values directly", func() {
It("sets the appropriate radio button in the group correctly", func() {
b.SetValue("input[name='appliances']", "stove")
Ω("input[name='appliances']").Should(b.HaveValue("stove"))
Ω("input[name='appliances'][value='toaster']").Should(b.HaveProperty("checked", false))
Ω("input[name='appliances'][value='stove']").Should(b.HaveProperty("checked", true))
Ω("input[name='transportation']").Should(b.HaveValue("hovercraft"))
Ω("input[name='transportation'][value='hovercraft']").Should(b.HaveProperty("checked", true))
b.SetValue("input[name='transportation'][value='hovercraft']", "car")
Ω("input[name='transportation']").Should(b.HaveValue("car"))
Ω("input[name='transportation'][value='hovercraft']").Should(b.HaveProperty("checked", false))
Ω("input[name='transportation'][value='car']").Should(b.HaveProperty("checked", true))
})
It("fails fast under Immediate() if the element is not visible", func() {
b.Immediate().SetValue("input[name='appliances']", "microwave")
ExpectFailures(ContainSubstring("The \"microwave\" option is not visible: input[name='appliances']"))
Ω("input[name='appliances']").Should(b.HaveValue("toaster"))
})
It("fails fast under Immediate() if the element is not enabled", func() {
b.Immediate().SetValue("input[name='transportation']", "bike")
ExpectFailures(ContainSubstring("The \"bike\" option is not enabled: input[name='transportation']"))
Ω("input[name='transportation']").Should(b.HaveValue("hovercraft"))
})
It("fails if provided an invalid value", func() {
b.Immediate().SetValue("input[name='turtle']", "splinter")
ExpectFailures(ContainSubstring("Radio input does not have option with value \"splinter\": input[name='turtle']"))
})
It("fails if provided a boolean value", func() {
b.Immediate().SetValue("input[name='appliances'][value='stove']", true)
ExpectFailures(ContainSubstring("Radio inputs only accept string values: input[name='appliances'][value='stove']"))
})
})
Context("when setting values as a matcher", func() {
It("sets the appropriate radio button in the group correctly", func() {
Ω("input[name='appliances']").Should(b.SetValue("stove"))
Ω("input[name='appliances']").Should(b.HaveValue("stove"))
Ω("input[name='appliances'][value='toaster']").Should(b.HaveProperty("checked", false))
Ω("input[name='appliances'][value='stove']").Should(b.HaveProperty("checked", true))
Ω("input[name='transportation']").Should(b.HaveValue("hovercraft"))
Ω("input[name='transportation'][value='hovercraft']").Should(b.HaveProperty("checked", true))
Ω("input[name='transportation'][value='hovercraft']").Should(b.SetValue("car"))
Ω("input[name='transportation']").Should(b.HaveValue("car"))
Ω("input[name='transportation'][value='hovercraft']").Should(b.HaveProperty("checked", false))
Ω("input[name='transportation'][value='car']").Should(b.HaveProperty("checked", true))
})
It("auto-fails if the element is not visible", func() {
match, err := b.SetValue("microwave").Match("input[name='appliances']")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("The \"microwave\" option is not visible: input[name='appliances']"))
Ω("input[name='appliances']").Should(b.HaveValue("toaster"))
})
It("auto-fails if the element is not enabled", func() {
match, err := b.SetValue("bike").Match("input[name='transportation']")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("The \"bike\" option is not enabled: input[name='transportation']"))
Ω("input[name='transportation']").Should(b.HaveValue("hovercraft"))
})
It("fails if provided an invalid value", func() {
match, err := b.SetValue("splinter").Match("input[name='turtle']")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Radio input does not have option with value \"splinter\": input[name='turtle']"))
})
It("fails if provided a boolean value", func() {
match, err := b.SetValue(true).Match("input[name='appliances'][value='stove']")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Radio inputs only accept string values: input[name='appliances'][value='stove']"))
})
})
})
Describe("working with multi-select inputs", func() {
It("returns the selected options as a slice of strings", func() {
Ω(b.GetValue("#party")).Should(ConsistOf("luke", "han", "vader"))
})
It("returns an empty slice if no options are selected", func() {
Ω(b.GetValue("#empty-party")).Should(BeEmpty())
})
Context("when setting values directly", func() {
It("sets the appropriate options on the group correctly", func() {
b.SetValue("#party", []string{"obi-wan", "han", "emperor"})
Ω(b.GetValue("#party")).Should(ConsistOf("obi-wan", "han", "emperor"))
b.SetValue("#party", []string{})
Ω(b.GetValue("#party")).Should(BeEmpty())
})
It("fails fast under Immediate() if one of the options is not enabled", func() {
b.Immediate().SetValue("#party", []string{"obi-wan", "han", "leia", "tarkin"})
ExpectFailures(ContainSubstring("The \"leia\" option is not enabled: #party"))
})
It("fails if provided an invalid value", func() {
b.Immediate().SetValue("#party", []string{"obi-wan", "han", "chewie", "tarkin"})
ExpectFailures(ContainSubstring("The \"chewie\" option does not exist: #party"))
})
It("fails if provided a non-slice value", func() {
b.Immediate().SetValue("#party", "han")
ExpectFailures(ContainSubstring("Multi-select inputs only accept []string values: #party"))
})
})
Context("when setting values as a matcher", func() {
It("sets the appropriate options on the group correctly", func() {
Ω("#party").Should(b.SetValue([]string{"obi-wan", "han", "emperor"}))
Ω(b.GetValue("#party")).Should(ConsistOf("obi-wan", "han", "emperor"))
Ω("#party").Should(b.SetValue([]string{}))
Ω(b.GetValue("#party")).Should(BeEmpty())
})
It("auto-fails if one of the options is not enabled", func() {
match, err := b.SetValue([]string{"obi-wan", "han", "leia", "tarkin"}).Match("#party")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("The \"leia\" option is not enabled: #party"))
})
It("fails if provided an invalid value", func() {
match, err := b.SetValue([]string{"obi-wan", "han", "chewie", "tarkin"}).Match("#party")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("The \"chewie\" option does not exist: #party"))
})
It("fails if provided a non-slice value", func() {
match, err := b.SetValue("han").Match("#party")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Multi-select inputs only accept []string values: #party"))
})
})
})
Describe("HaveClass", func() {
It("matches if the elements has the class", func() {
Ω("#hidden-parent").Should(b.HaveClass("hidden"))
Ω("#classy").Should(b.HaveClass("dog"))
Ω("#classy").Should(b.HaveClass("cat"))
Ω("#classy").ShouldNot(b.HaveClass("fish"))
})
It("returns an error when the element does not exist", func() {
match, err := b.HaveClass("foo").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("HaveText", func() {
It("matches against the whitespace-normalized innerText", func() {
Ω("#hello").Should(b.HaveText("Hello Biloba!"))
Ω("#spacey-text").Should(b.HaveText("Hello there Biloba!"))
Ω("#spacey-text").ShouldNot(b.HaveText("Hello there\n\n Biloba!"))
})
It("supports matchers", func() {
Ω("#spacey-text").Should(b.HaveText(ContainSubstring("there Biloba")))
Eventually("#spacey-text").Should(b.HaveText(HavePrefix("Hello there")))
})
It("returns an error when the element does not exist", func() {
match, err := b.HaveText("foo").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("HaveAttribute", func() {
It("checks attribute existence when given only a name", func() {
Ω("#link").Should(b.HaveAttribute("href"))
Ω("#link").Should(b.HaveAttribute("data-role"))
Ω("#link").ShouldNot(b.HaveAttribute("data-missing"))
})
It("checks the attribute value when given a second argument", func() {
Ω("#link").Should(b.HaveAttribute("href", "/about"))
Ω("#link").Should(b.HaveAttribute("data-role", "nav"))
Ω("#link").Should(b.HaveAttribute("href", HaveSuffix("about")))
Ω("#link").ShouldNot(b.HaveAttribute("href", "/contact"))
})
It("is distinct from HaveProperty (attribute vs property)", func() {
//the href property is resolved to an absolute URL; the attribute is the raw value
Ω("#link").Should(b.HaveAttribute("href", "/about"))
Ω("#link").Should(b.HaveProperty("href", HaveSuffix("/about")))
Ω("#link").ShouldNot(b.HaveProperty("href", "/about"))
})
It("returns an error when the element does not exist", func() {
match, err := b.HaveAttribute("href").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
match, err = b.HaveAttribute("href", "/about").Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
})
Describe("BeChecked", func() {
It("matches checked checkboxes and radio buttons", func() {
Ω("#red").Should(b.BeChecked())
Ω("#blue").ShouldNot(b.BeChecked())
Ω("input[name='appliances'][value='toaster']").Should(b.BeChecked())
Ω("input[name='appliances'][value='stove']").ShouldNot(b.BeChecked())
})
It("updates as the checkbox state changes", func() {
b.SetValue("#blue", true)
Eventually("#blue").Should(b.BeChecked())
})
It("returns an error when the element does not exist", func() {
match, err := b.BeChecked().Match("#non-existing")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("could not find DOM element matching selector: #non-existing"))
})
It("errors - in both directions - when the element has no checked property at all", func() {
// #check-boxes is the <div> that WRAPS the checkboxes (the same mistake as selecting the
// enclosing <label>). It can never be checked, so reading its undefined `checked` and
// flattening it to false would make ShouldNot(b.BeChecked()) pass forever.
match, err := b.BeChecked().Match("#check-boxes")
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError(SatisfyAll(
ContainSubstring(`<div#check-boxes> has no "checked" property`),
ContainSubstring("did you select the label or wrapper rather than the input?"),
ContainSubstring("selector: #check-boxes"),
)))
failure := ""
g := NewGomega(func(message string, callerSkip ...int) { failure = message })
g.Expect("#check-boxes").ShouldNot(b.BeChecked())
Ω(failure).Should(ContainSubstring(`has no "checked" property`))
})
It("keeps an unchecked input a plain non-match, not an error", func() {
// the distinction that matters: #blue IS a checkbox, it just isn't checked. That must stay
// an honest, error-free ShouldNot.
match, err := b.BeChecked().Match("#blue")
Ω(match).Should(BeFalse())
Ω(err).ShouldNot(HaveOccurred())
Ω("#blue").ShouldNot(b.BeChecked())
// ...and a checked one still matches
match, err = b.BeChecked().Match("#red")
Ω(match).Should(BeTrue())
Ω(err).ShouldNot(HaveOccurred())
})
})
Describe("BeFocused", func() {
It("matches the document's activeElement", func() {
Ω("#focus-input").ShouldNot(b.BeFocused())
b.InvokeOn("#focus-input", "focus")
Eventually("#focus-input").Should(b.BeFocused())
Ω("#hello").ShouldNot(b.BeFocused())
})