-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdialog_handling_test.go
256 lines (213 loc) · 10.3 KB
/
dialog_handling_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
package biloba_test
import (
"github.com/onsi/biloba"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("DialogHandling", func() {
BeforeEach(func() {
b.Navigate(fixtureServer + "/dialogs.html")
Eventually("#status").Should(b.HaveInnerText("Green Alert!"))
})
Context("when no handler is registered and a dialog appears", func() {
It("handles the dialog and tells the user", func() {
b.Click(b.XPath("button").WithText("Red Alert"))
Eventually(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: alert - Red Alert!"))
Ω("#status").Should(b.HaveInnerText("Red Alert!"))
})
It("autohandles most dialogs with 'false'", func() {
b.Click(b.XPath("button").WithText("Enter Warp?"))
Eventually(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: confirm - Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert!"))
})
It("authoandles beforeunload with 'true'", func() {
b.Click(b.XPath("a").WithText("Evacuate"))
Eventually(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: beforeunload"))
Eventually(b.Location).Should(HaveSuffix("/dom.html"))
})
})
Describe("Registering handlers", func() {
BeforeEach(func() {
gt.buffer.Clear()
})
Context("handling alerts", func() {
It("can handle alerts", func() {
b.HandleAlertDialogs().MatchingMessage(ContainSubstring("Red"))
b.Click(b.XPath("button").WithText("Red Alert"))
Eventually("#status").Should(b.HaveInnerText("Red Alert!"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
b.Click(b.XPath("button").WithText("Yellow Alert"))
Eventually("#status").Should(b.HaveInnerText("Yellow Alert!"))
Ω(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: alert - Yellow Alert!"))
})
It("can handle all allerts silently", func() {
b.HandleAlertDialogs()
b.Click(b.XPath("button").WithText("Red Alert"))
Eventually("#status").Should(b.HaveInnerText("Red Alert!"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
b.Click(b.XPath("button").WithText("Yellow Alert"))
Eventually("#status").Should(b.HaveInnerText("Yellow Alert!"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
})
Context("handling confirms", func() {
It("autohandles as false", func() {
b.HandleConfirmDialogs().MatchingMessage("nope").WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Eventually(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: confirm - Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert!"))
})
It("can be configured to handle true", func() {
b.HandleConfirmDialogs().MatchingMessage("Enter Warp?").WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 0"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
It("can be reconfigured", func() {
b.HandleConfirmDialogs().MatchingMessage("Enter Warp?").WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 0"))
//later handler wins
handler := b.HandleConfirmDialogs().MatchingMessage("Enter Warp?").WithResponse(false)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert!"))
//we can remove the later handler and the earlier handler wins
b.RemoveDialogHandler(handler)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 0"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
})
Context("handling prompts", func() {
BeforeEach(func() {
b.HandleConfirmDialogs().WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 0"))
})
It("autohandles as false", func() {
b.Click(b.XPath("button").WithText("Set Speed"))
Eventually(gt.buffer).Should(gbytes.Say("Biloba automatically handled an {{red}}unhandled dialog{{/}} - you should add an explicit dialog handler: prompt - Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 0"))
})
It("can return accept the default value", func() {
b.HandlePromptDialogs().MatchingMessage("Set Speed").WithResponse(true)
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 3"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
It("can return a value", func() {
b.HandlePromptDialogs().MatchingMessage("Set Speed").WithText("7")
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 7"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
It("can return empty string as a value", func() {
b.HandlePromptDialogs().MatchingMessage("Set Speed").WithText("")
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed:"))
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
It("can be reconfigured", func() {
b.HandlePromptDialogs().MatchingMessage("Set Speed").WithText("7")
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 7"))
//later handler wins
handler := b.HandlePromptDialogs().MatchingMessage("Set Speed").WithText("10")
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 10"))
//we can remove the later handler and the earlier handler wins
b.RemoveDialogHandler(handler)
b.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 7"))
})
})
Context("handling onbeforeunload", func() {
It("can be configured to handle false", func() {
handler := b.HandleBeforeunloadDialogs().WithResponse(false)
b.Click(b.XPath("a").WithText("Evacuate"))
Consistently(b.Location).Should(HaveSuffix("/dialogs.html"))
//so we can run navigate away after this test ends!
b.RemoveDialogHandler(handler)
Ω(gt.buffer.Contents()).Should(BeEmpty())
})
})
It("works with multiple tabs", func() {
tab := b.NewTab().Navigate(fixtureServer + "/dialogs.html")
Eventually("#status").Should(tab.HaveInnerText("Green Alert!"))
b.HandleConfirmDialogs().WithResponse(true)
tab.HandleConfirmDialogs().WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?"))
tab.Click(b.XPath("button").WithText("Enter Warp?"))
b.HandlePromptDialogs().WithText("10")
tab.HandlePromptDialogs().WithText("11")
b.Click(b.XPath("button").WithText("Set Speed"))
tab.Click(b.XPath("button").WithText("Set Speed"))
Ω("#status").Should(b.HaveInnerText("Green Alert! - Warp Speed: 10"))
Ω("#status").Should(tab.HaveInnerText("Green Alert! - Warp Speed: 11"))
})
})
Describe("looking for and making assertions on dialogs", func() {
BeforeEach(func() {
b.Click(b.XPath("button").WithText("Red Alert")) //unhandled
b.HandleAlertDialogs()
b.Click(b.XPath("button").WithText("Yellow Alert")) //handled
b.Click(b.XPath("button").WithText("Enter Warp?")) //unhandled
b.HandleConfirmDialogs().WithResponse(true)
b.Click(b.XPath("button").WithText("Enter Warp?")) //handled
b.HandlePromptDialogs().WithText("10")
b.Click(b.XPath("button").WithText("Set Speed")) //handled
b.HandlePromptDialogs().WithResponse(true)
b.Click(b.XPath("button").WithText("Set Speed")) //handled with default prompt
b.HandlePromptDialogs().WithResponse(false)
b.Click(b.XPath("button").WithText("Set Speed")) //handled with false response
handler := b.HandleBeforeunloadDialogs().WithResponse(false)
b.Click(b.XPath("a").WithText("Evacuate")) //handled
b.RemoveDialogHandler(handler)
})
It("can return a list of all dialogs", func() {
Ω(len(b.Dialogs())).Should(Or(Equal(8), Equal(9))) //there is a beforeunload that gets triggered navigating to this page. but it won't be there if we happen to be the first spec!
})
It("returns nil if there are no most recent dialogs", func() {
Ω(biloba.Dialogs{}.MostRecent()).Should(BeNil())
})
It("can return the most recent dialog", func() {
Ω(b.Dialogs().MostRecent()).Should(HaveField("Type", biloba.DialogTypeBeforeunload))
Ω(b.Dialogs().MostRecent()).Should(HaveField("Autohandled", false))
})
It("can filter by type", func() {
alerts := b.Dialogs().OfType(biloba.DialogTypeAlert)
Ω(alerts).Should(HaveLen(2))
Ω(alerts[0]).Should(HaveField("Message", "Red Alert!"))
Ω(alerts[0]).Should(HaveField("Autohandled", true))
Ω(alerts[1]).Should(HaveField("Message", "Yellow Alert!"))
Ω(alerts[1]).Should(HaveField("Autohandled", false))
})
It("can filter by message", func() {
alerts := b.Dialogs().MatchingMessage(ContainSubstring("Alert"))
Ω(alerts).Should(HaveLen(2))
Ω(alerts[0]).Should(HaveField("Message", "Red Alert!"))
Ω(alerts[0]).Should(HaveField("Autohandled", true))
Ω(alerts[1]).Should(HaveField("Message", "Yellow Alert!"))
Ω(alerts[1]).Should(HaveField("Autohandled", false))
})
It("records how confirm dialogs were handled", func() {
warps := b.Dialogs().OfType(biloba.DialogTypeConfirm)
Ω(warps).Should(HaveLen(2))
Ω(warps[0].Autohandled).Should(BeTrue())
Ω(warps[0].HandleResponse).Should(BeFalse())
Ω(warps[1].Autohandled).Should(BeFalse())
Ω(warps[1].HandleResponse).Should(BeTrue())
})
It("record how prompt dialogs were handled", func() {
warps := b.Dialogs().OfType(biloba.DialogTypePrompt)
Ω(warps).Should(HaveLen(3))
Ω(warps[0].HandleResponse).Should(BeTrue())
Ω(warps[1].HandleResponse).Should(BeTrue())
Ω(warps[2].HandleResponse).Should(BeFalse())
Ω(warps[0].HandleText).Should(Equal("10"))
Ω(warps[1].HandleText).Should(Equal("3"))
Ω(warps[2].HandleText).Should(Equal(""))
})
})
})