-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tabs_test.go
250 lines (207 loc) · 9.72 KB
/
tabs_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
package biloba_test
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("Tabs", func() {
Describe("creating new tabs", func() {
Context("explicitly", func() {
It("allows the user to create a new tab that is its own universe", func() {
b.Navigate(fixtureServer + "/nav-a.html")
Eventually(b.Title).Should(Equal("Nav-A Testpage"))
tab := b.NewTab().Navigate(fixtureServer + "/dom.html")
Eventually(tab.Title).Should(Equal("DOM Testpage"))
Ω(b.Title()).Should(Equal("Nav-A Testpage"))
By("it correctly wires up logging and directs interactions to the correct tab")
Eventually("#increment").Should(tab.Click())
tab.Click("#increment")
tab.Click("#increment")
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("1"))
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("2"))
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("3"))
})
It("the new tab has a different BrowserContextID than the original tab", func() {
tab := b.NewTab()
Ω(b.BrowserContextID()).ShouldNot(Equal(tab.BrowserContextID()))
})
})
Context("when a user-interaction opens a new tab (href target=_blank)", func() {
It("can find the tab and latch onto it", func() {
b.Navigate(fixtureServer + "/nav-a.html")
Eventually(b.XPath().WithTextContains("Go to DOM (new tab)")).Should(b.Click())
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithURL(ContainSubstring("dom.html"))))
tab := b.AllSpawnedTabs().Find(b.TabWithURL(ContainSubstring("dom.html")))
Eventually("#increment").Should(tab.Click())
tab.Click("#increment")
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("1"))
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("2"))
})
It("should have the same BrowserContextID as the spawning tab", func() {
b.Navigate(fixtureServer + "/nav-a.html")
Eventually(b.XPath().WithTextContains("Go to DOM (new tab)")).Should(b.Click())
time.Sleep(time.Millisecond * 200)
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithURL(ContainSubstring("dom.html"))))
tab := b.AllSpawnedTabs().Find(b.TabWithURL(ContainSubstring("dom.html")))
Ω(b.BrowserContextID()).Should(Equal(tab.BrowserContextID()))
})
})
Context("when javacript opens a new tab", func() {
It("can find the tab and latch onto it", func() {
b.Navigate(fixtureServer + "/auto-open.html")
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithURL(ContainSubstring("dom.html"))))
tab := b.AllSpawnedTabs().Find(b.TabWithURL(ContainSubstring("dom.html")))
Eventually("#increment").Should(tab.Click())
tab.Click("#increment")
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("1"))
Eventually(gt.buffer).Should(gbytes.Say("increment to"))
Eventually(gt.buffer).Should(gbytes.Say("2"))
})
})
It("wires up events correctly for new tabs", func() {
b.Run("console.log('this is the main tab')")
Eventually(gt.buffer).Should(gbytes.Say("this is the main tab"))
g2 := b.NewTab()
g2.Run("console.log('this is the new tab')")
Eventually(gt.buffer).Should(gbytes.Say("this is the new tab"))
})
})
Describe("looking for tabs", func() {
BeforeEach(func() {
b.Navigate(fixtureServer + "/nav-a.html")
b.NewTab().Navigate(fixtureServer + "/dom.html")
b.NewTab().Navigate(fixtureServer + "/xpath.html")
Eventually(b.AllTabs).Should(HaveLen(3))
})
It("can return all tabs", func() {
Ω(b.AllTabs()).Should(ConsistOf(
HaveField("Title()", "Nav-A Testpage"),
HaveField("Title()", "DOM Testpage"),
HaveField("Title()", "XPath Testpage"),
))
})
It("any tab, in fact, can return all tabs", func() {
tab := b.AllTabs().Find(b.TabWithTitle("DOM Testpage"))
Ω(tab.AllTabs()).Should(ConsistOf(
HaveField("Title()", "Nav-A Testpage"),
HaveField("Title()", "DOM Testpage"),
HaveField("Title()", "XPath Testpage"),
))
})
It("returns nil when the tab can't be found", func() {
Ω(b.AllSpawnedTabs().Find(b.TabWithDOMElement("#non-existing"))).Should(BeNil())
Ω(b.AllSpawnedTabs().Find(b.TabWithTitle("non-existing"))).Should(BeNil())
Ω(b.AllSpawnedTabs().Find(b.TabWithURL("non-existing.html"))).Should(BeNil())
Ω(b.AllTabs().Find(b.TabWithDOMElement("#non-existing"))).Should(BeNil())
Ω(b.AllTabs().Find(b.TabWithTitle("non-existing"))).Should(BeNil())
Ω(b.AllTabs().Find(b.TabWithURL("non-existing.html"))).Should(BeNil())
})
It("does not include non-spawned tabs in spawned tabs", func() {
Ω(b.AllSpawnedTabs()).Should(BeEmpty())
b.Click("#to-b-new")
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithTitle("Nav-B Testpage")))
})
It("groups spawned tabs appropriately", func() {
tab := b.NewTab()
Ω(tab.AllSpawnedTabs()).Should(BeEmpty())
tab.Navigate(fixtureServer + "/auto-open.html")
Eventually(tab).Should(tab.HaveSpawnedTab(tab.TabWithTitle("DOM Testpage")))
Ω(tab.AllSpawnedTabs()).Should(HaveLen(1))
spawnedTab := tab.AllSpawnedTabs().Find(tab.TabWithDOMElement("#hello"))
Ω(spawnedTab.Title()).Should(Equal("DOM Testpage"))
By("this is currently a bit weird, but spawned tabs consider everything in the browser context to be their spawned tabs")
Ω(spawnedTab.AllSpawnedTabs()).Should(HaveLen(1))
Ω(spawnedTab).Should(spawnedTab.HaveSpawnedTab(spawnedTab.TabWithTitle("AutoOpen Testpage")))
Ω(spawnedTab.AllSpawnedTabs().Find(spawnedTab.TabWithTitle("AutoOpen Testpage"))).Should(Equal(tab))
By("the root tab doens't have any of these spawned tabs")
Ω(b.AllSpawnedTabs()).Should(BeEmpty())
Ω(b).ShouldNot(b.HaveSpawnedTab(b.TabWithTitle("DOM Testpage")))
Ω(b.AllSpawnedTabs().Find(b.TabWithDOMElement("#hello"))).Should(BeNil())
By("and when we open a tab from the root tab - it dosn't get attached to the other tabs")
b.Click("#to-b-new")
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithTitle("Nav-B Testpage")))
Ω(tab).ShouldNot(tab.HaveSpawnedTab(b.TabWithTitle("Nav-B Testpage")))
Ω(tab.AllSpawnedTabs().Find(tab.TabWithTitle("Nav-B Testpage"))).Should(BeNil())
})
It("can find tabs by title", func() {
tab := b.AllTabs().Find(b.TabWithTitle("Nav-A Testpage"))
Eventually("#to-b").Should(tab.Exist())
tab = b.AllTabs().Find(b.TabWithTitle(ContainSubstring("DOM")))
Eventually("#increment").Should(tab.Exist())
})
It("can find tabs by URL", func() {
tab := b.AllTabs().Find(b.TabWithURL(HaveSuffix("xpath.html")))
Eventually("#aquarium").Should(tab.Exist())
tab = b.AllTabs().Find(b.TabWithURL(fixtureServer + "/dom.html"))
Eventually("#increment").Should(tab.Exist())
})
It("can find tabs by DOM element", func() {
tab := b.AllTabs().Find(b.TabWithDOMElement("#increment"))
Ω(tab.Title()).Should(Equal("DOM Testpage"))
tab = b.AllTabs().Find(b.TabWithDOMElement(b.XPath().WithID("aquarium")))
Ω(tab.Title()).Should(Equal("XPath Testpage"))
})
It("can match by title, URL, and DOM element", func() {
Ω(b).Should(b.HaveTab(b.TabWithTitle("Nav-A Testpage")))
Ω(b).ShouldNot(b.HaveTab(b.TabWithTitle("Nav-B Testpage")))
Ω(b).Should(b.HaveTab(b.TabWithURL(fixtureServer + "/dom.html")))
Ω(b).ShouldNot(b.HaveTab(b.TabWithURL(HaveSuffix(".htm"))))
Ω(b).Should(b.HaveTab(b.TabWithDOMElement(b.XPath().WithID("increment"))))
Ω(b).Should(b.HaveTab(b.TabWithDOMElement("#aquarium")))
Ω(b).ShouldNot(b.HaveTab(b.TabWithDOMElement("#non-existing")))
})
})
Describe("closing tabs", func() {
It("closes non-root tabs", func() {
b.Navigate(fixtureServer + "/nav-a.html")
tab := b.NewTab().Navigate(fixtureServer + "/dom.html")
Ω(b).Should(b.HaveTab(b.TabWithTitle("DOM Testpage")))
Ω(b.AllTabs()).Should(HaveLen(2))
Ω(tab.Close()).Should(Succeed())
Eventually(b.AllTabs).Should(HaveLen(1))
Ω(b).ShouldNot(b.HaveTab(b.TabWithTitle("DOM Testpage")))
})
It("fails when attempting to close the root tab", func() {
Ω(b.Close()).Should(MatchError("invalid attempt to close the root tab"))
})
})
Describe("a tab flow with stuff going on", Ordered, func() {
It("can spawn new tabs, etc.", func() {
b.Navigate(fixtureServer + "/nav-a.html")
Eventually(b.Title).Should(Equal("Nav-A Testpage"))
Ω(b.AllTabs()).Should(ConsistOf(HaveField("Title()", "Nav-A Testpage")))
b.Click("#to-b-new")
Eventually(b).Should(b.HaveSpawnedTab(b.TabWithTitle("Nav-B Testpage")))
g2 := b.AllTabs().Find(b.TabWithTitle("Nav-B Testpage"))
Ω(g2).Should(Equal(b.AllSpawnedTabs().Find(b.TabWithURL(ContainSubstring("/nav-b.html")))))
Ω(g2).Should(Equal(b.AllSpawnedTabs().Find(b.TabWithDOMElement("#to-a"))))
g3 := b.NewTab().Navigate(fixtureServer + "/dom.html")
Eventually(g3.Title).Should(Equal("DOM Testpage"))
Ω(b.Title()).Should(Equal("Nav-A Testpage"))
Ω(g2.Title()).Should(Equal("Nav-B Testpage"))
Ω(g3.Title()).Should(Equal("DOM Testpage"))
Ω(b.AllTabs()).Should(ConsistOf(
HaveField("Title()", "Nav-A Testpage"),
HaveField("Title()", "DOM Testpage"),
HaveField("Title()", "Nav-B Testpage"),
))
Ω(g2.Close()).Should(Succeed())
Ω(g3.Close()).Should(Succeed())
Eventually(b.AllTabs).Should(ConsistOf(HaveField("Title()", "Nav-A Testpage")))
Ω(b).ShouldNot(b.HaveTab(b.TabWithTitle("DOM Testpage")))
Ω(b).ShouldNot(b.HaveTab(b.TabWithTitle("Nav-B Testpage")))
Ω(b).Should(b.HaveTab(b.TabWithTitle("Nav-A Testpage")))
Ω(b.Close()).Should(MatchError("invalid attempt to close the root tab"))
})
It("cleans up tabs between tests", func() {
Ω(b.AllTabs()).Should(ConsistOf(b))
})
})
})