-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathxpath_test.go
More file actions
196 lines (167 loc) · 7.57 KB
/
Copy pathxpath_test.go
File metadata and controls
196 lines (167 loc) · 7.57 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
package biloba_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/biloba"
)
var _ = Describe("Text and TextContains selectors", func() {
BeforeEach(func() {
b.Navigate(fixtureServer + "/xpath.html")
Eventually(b.XPath().WithID("hometown-label")).Should(b.Exist())
})
Describe("b.ByText", func() {
It("selects the element whose text matches exactly", func() {
Ω(b.ByText("Hometown:")).Should(b.HaveProperty("id", "hometown-label"))
})
It("composes with matchers like BeVisible and Exist", func() {
Eventually(b.ByText("Hometown:")).Should(b.Exist())
Ω(b.ByText("Hometown:")).Should(b.BeVisible())
})
It("composes with actions like Click", func() {
// clicking a label is fine; we just assert no test failure occurs
b.Click(b.ByText("Hometown:"))
})
It("returns the first match when multiple elements share the same text", func() {
// both age-label and age-label-2 have text "Age:" — first is returned
Ω(b.ByText("Age:")).Should(b.HaveProperty("id", "age-label"))
})
It("does not match on a partial string", func() {
Ω(b.HasElement(b.ByText("ometow"))).Should(BeFalse())
})
It("fails the spec when no element matches", func() {
b.Click(b.ByText("no such text"))
ExpectFailures(ContainSubstring("no such text"))
})
})
Describe("b.ByTextContains", func() {
It("selects an element whose text contains the substring", func() {
Ω(b.ByTextContains("ometow")).Should(b.HaveProperty("id", "hometown-label"))
})
It("composes with matchers like Exist", func() {
Eventually(b.ByTextContains("ometow")).Should(b.Exist())
})
It("does not match when the substring is absent", func() {
Ω(b.HasElement(b.ByTextContains("zzzzzz"))).Should(BeFalse())
})
It("fails the spec when no element matches", func() {
b.Click(b.ByTextContains("no-such-substring"))
ExpectFailures(ContainSubstring("no-such-substring"))
})
})
})
var _ = DescribeTable("Xpath DSL",
func(path biloba.XPath, expectedId string) {
b.Navigate(fixtureServer + "/xpath.html")
Ω(path).Should(b.HaveProperty("id", expectedId))
},
func(path biloba.XPath, _ string) string {
return path.String()
},
//empty and specific tag variants
Entry(nil, b.XPath().WithAttr("type", "number"), "age-input"),
Entry(nil, b.XPath("input").WithAttr("type", "number"), "age-input"),
Entry(nil, b.XPath("input").HasAttr("disabled"), "phone-input"),
//WithClass combined with different tags
Entry(nil, b.XPath().WithClass("highlight"), "age-label"),
Entry(nil, b.XPath("label").WithClass("highlight"), "age-label"),
Entry(nil, b.XPath("input").WithClass("highlight"), "age-input"),
//WithID
Entry(nil, b.XPath("input").WithID("hometown-input"), "hometown-input"),
//attr start/contain
Entry(nil, b.XPath("input").WithAttrStartsWith("id", "hometown"), "hometown-input"),
Entry(nil, b.XPath("input").WithAttrContains("id", "wn-in"), "hometown-input"),
//text start/contain
Entry(nil, b.XPath().WithText("Hometown:"), "hometown-label"),
Entry(nil, b.XPath().WithTextStartsWith("Hometown"), "hometown-label"),
Entry(nil, b.XPath().WithTextContains("ometow"), "hometown-label"),
//with class
Entry(nil, b.XPath("div").WithClass("fish"), "aquarium"),
Entry(nil, b.XPath("div").WithClass("otter"), "aquarium"),
Entry(nil, b.XPath("div").WithClass("octopus"), "aquarium"),
//boolean logic
Entry(nil, b.XPath("div").WithAttr("name", "habitat").WithClass("tiger"), "zoo"),
Entry(nil, b.XPath("div").WithAttr("name", "habitat"), "aquarium"),
Entry(nil, b.XPath("div").WithAttr("name", "habitat").Not(
b.XPredicate().WithClass("otter"),
), "zoo"),
Entry(nil, b.XPath("div").And(
b.XPredicate().WithAttr("name", "habitat"),
b.XPredicate().WithClass("bear"),
), "zoo"),
Entry(nil, b.XPath("div").And(
b.XPredicate().WithAttr("name", "habitat"),
).Or(
b.XPredicate().WithClass("octopus"),
b.XPredicate().WithClass("bear"),
), "aquarium"),
Entry(nil, b.XPath("div").Or(
b.XPredicate().And(
b.XPredicate().WithClass("red"),
b.XPredicate().WithText("Error"),
),
b.XPredicate().And(
b.XPredicate().WithClass("orange"),
b.XPredicate().WithText("Warning"),
),
).Not(b.XPredicate().HasAttr("fire-drill")), "orange-flag"),
//indexing
Entry(nil, b.XPath("div").WithAttr("name", "habitat").First(), "aquarium"),
Entry(nil, b.XPath("div").WithAttr("name", "habitat").Nth(2), "zoo"),
Entry(nil, b.XPath("div").WithAttr("name", "habitat").Last(), "rainforest"),
Entry(nil, b.XPath("div").WithClass("habitats").Descendant().First(), "common-habitats"),
Entry(nil, b.XPath("div").WithClass("habitats").Descendant().Nth(4), "obscure-habitats"),
Entry(nil, b.XPath("div").WithClass("habitats").Descendant().Last(), "all-microbiota"),
// navigating the tree
// - descendant
Entry(nil, b.XPath("div").WithID("reference").Descendant(), "all-habitats"),
Entry(nil, b.XPath("div").WithID("reference").Descendant().WithAttr("name", "habitat"), "aquarium"),
Entry(nil, b.XPath("div").WithID("reference").Descendant("ul"), "all-microbiota"),
Entry(nil, b.XPath("div").WithID("reference").Descendant().WithAttr("color", "blue"), "common-habitats"),
// - child
Entry(nil, b.XPath("div").WithID("reference").Child(), "all-habitats"),
Entry(nil, b.XPath("div").WithID("reference").Child("ul"), "all-languages"),
// - parent
Entry(nil, b.XPath("li").WithID("critters").Parent(), "all-microbiota"),
Entry(nil, b.XPath("li").WithText("Engli\"sh").Parent(), "all-languages"),
// - ancestor
Entry(nil, b.XPath("li").WithID("critters").Ancestor(), "critters"),
Entry(nil, b.XPath("li").WithID("critters").AncestorNotSelf(), "all-microbiota"),
Entry(nil, b.XPath("li").WithID("critters").Ancestor("div"), "all-habitats"),
Entry(nil, b.XPath("li").WithID("critters").Ancestor("div").Nth(2), "reference"),
// - siblings
Entry(nil, b.XPath().WithID("zoo").FollowingSibling(), "obscure-habitats"),
Entry(nil, b.XPath().WithID("zoo").FollowingSibling("ul"), "all-microbiota"),
Entry(nil, b.XPath("li").WithID("english").FollowingSibling("li"), "spanish"),
Entry(nil, b.XPath("li").WithID("english").FollowingSibling("li").Last(), "arabic"),
Entry(nil, b.XPath("li").WithID("english").PrecedingSibling("li"), "french"),
Entry(nil, b.XPath("li").WithID("english").PrecedingSibling("li").Last(), "swedish"),
// - whith child matching
Entry(nil, b.XPath("ul").WithChildMatching(b.RelativeXPath("li").WithText("Francais")), "all-languages"),
Entry(nil, b.XPath("ul").WithChildMatching(
b.RelativeXPath("li").Or(
b.XPredicate().WithText("Francais"),
b.XPredicate().WithText("Germs"),
)), "all-microbiota"),
)
var _ = DescribeTable("XPath() passes already-formed expressions through verbatim (no auto-prepended axis)",
func(in string, expected string) {
Ω(b.XPath(in).String()).Should(Equal(expected))
},
// a bare element name gets the // axis prepended...
Entry("bare element name", "div", "//div"),
// ...but already-formed expressions are left untouched:
Entry("absolute path", "/html/body/div", "/html/body/div"),
Entry("descendant axis", "//div[@id='foo']", "//div[@id='foo']"),
Entry("relative path", "./span", "./span"),
Entry("wildcard step", "*[@id='foo']", "*[@id='foo']"),
// the regression: a parenthesized/grouped expression must not become //(...)[3]
Entry("parenthesized ordinal", "(//ul[@class='x'])[3]", "(//ul[@class='x'])[3]"),
)
var _ = DescribeTable("RelativeXPath() passes already-formed expressions through verbatim",
func(in string, expected string) {
Ω(b.RelativeXPath(in).String()).Should(Equal(expected))
},
Entry("bare element name", "li", "./li"),
Entry("relative path", "./li", "./li"),
Entry("parenthesized ordinal", "(./li)[1]", "(./li)[1]"),
)