forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_test.go
More file actions
260 lines (202 loc) · 5.93 KB
/
browser_test.go
File metadata and controls
260 lines (202 loc) · 5.93 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
package rod_test
import (
"context"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/ysmood/kit"
"github.com/ysmood/rod"
"github.com/ysmood/rod/lib/launcher"
"github.com/ysmood/rod/lib/proto"
)
func (s *S) TestBrowserPages() {
page := s.browser.Page(srcFile("fixtures/click.html"))
defer page.Close()
pages := s.browser.Pages()
s.Len(pages, 3)
}
func (s *S) TestBrowserContext() {
s.browser.Timeout(time.Minute).CancelTimeout()
}
func (s *S) TestIncognito() {
file := srcFile("fixtures/click.html")
k := kit.RandString(8)
b := s.browser.Incognito()
page := b.Page(file)
page.Eval(`k => localStorage[k] = 1`, k)
s.Nil(s.page.Navigate(file).Eval(`k => localStorage[k]`, k).Value())
s.EqualValues(1, page.Eval(`k => localStorage[k]`, k).Int())
}
func (s *S) TestBrowserWaitEvent() {
wait := s.browser.WaitEvent()
s.page.Navigate(srcFile("fixtures/click.html"))
wait(&proto.PageFrameNavigated{})
}
func (s *S) TestBrowserCall() {
v, err := proto.BrowserGetVersion{}.Call(s.browser)
kit.E(err)
s.Regexp("HeadlessChrome", v.Product)
}
func (s *S) TestBrowserHandleAuth() {
url, engine, close := serve()
defer close()
// mock the server
engine.NoRoute(func(ctx kit.GinContext) {
u, p, ok := ctx.Request.BasicAuth()
if !ok {
ctx.Header("WWW-Authenticate", `Basic realm="web"`)
ctx.Writer.WriteHeader(401)
return
}
s.Equal("a", u)
s.Equal("b", p)
})
s.browser.HandleAuth("a", "b")
s.browser.Page(url).Close()
}
func (s *S) TestMonitor() {
b := rod.New().Connect()
defer b.Close()
p := b.Page(srcFile("fixtures/click.html")).WaitLoad()
host := b.ServeMonitor("127.0.0.1:0").Listener.Addr().String()
s.Contains(kit.Req("http://"+host).MustString(), string(p.TargetID))
s.Contains(kit.Req("http://"+host+"/page/"+string(p.TargetID)).MustString(), p.TargetID)
s.Greater(len(kit.Req("http://"+host+"/screenshot/"+string(p.TargetID)).MustBytes()), 1000)
}
func (s *S) TestRemoteLaunch() {
srv := kit.MustServer("127.0.0.1:0")
defer func() { _ = srv.Listener.Close() }()
proxy := &launcher.Proxy{Log: func(s string) {}}
srv.Engine.NoRoute(gin.WrapH(proxy))
go func() { _ = srv.Do() }()
l := launcher.NewRemote("ws://" + srv.Listener.Addr().String())
b := rod.New().Client(l.Client()).Connect()
p := b.Page(srcFile("fixtures/click.html"))
p.Element("button").Click()
s.True(p.Has("[a=ok]"))
b.Close()
kit.Sleep(0.3)
dir, _ := l.Get("user-data-dir")
s.NoDirExists(dir)
}
func (s *S) TestConcurrentOperations() {
p := s.page.Navigate(srcFile("fixtures/click.html"))
list := []int64{}
kit.All(func() {
list = append(list, p.Eval(`() => new Promise(r => setTimeout(r, 100, 2))`).Int())
}, func() {
list = append(list, p.Eval(`() => 1`).Int())
})()
s.Equal([]int64{1, 2}, list)
}
func (s *S) TestPromiseLeak() {
/*
Perform a slow action then navigate the page to another url,
we can see the slow operation will still be executed.
The unexpected part is that the promise will resolve to the next page's url.
*/
p := s.page.Navigate(srcFile("fixtures/click.html"))
var out string
kit.All(func() {
out = p.Eval(`() => new Promise(r => setTimeout(() => r(location.href), 200))`).String()
}, func() {
kit.Sleep(0.1)
p.Navigate(srcFile("fixtures/input.html"))
})()
s.Contains(out, "input.html")
}
func (s *S) TestObjectLeak() {
/*
Seems like it won't leak
*/
p := s.page.Navigate(srcFile("fixtures/click.html"))
el := p.Element("button")
p.Navigate(srcFile("fixtures/input.html")).WaitLoad()
s.Panics(func() {
el.Describe()
})
}
func (s *S) TestBlockingNavigation() {
/*
Navigate can take forever if a page doesn't response.
If one page is blocked, other pages should still work.
*/
url, engine, close := serve()
defer close()
pause, cancel := context.WithCancel(context.Background())
defer cancel()
engine.GET("/a", func(ctx kit.GinContext) {
<-pause.Done()
})
engine.GET("/b", ginHTML(`<html>ok</html>`))
blocked := s.browser.Page("")
defer blocked.Close()
go func() {
s.Panics(func() {
blocked.Navigate(url + "/a")
})
}()
kit.Sleep(0.3)
p := s.browser.Page(url + "/b")
defer p.Close()
}
func (s *S) TestResolveBlocking() {
url, engine, close := serve()
defer close()
pause, cancel := context.WithCancel(context.Background())
defer cancel()
engine.NoRoute(func(ctx kit.GinContext) {
<-pause.Done()
})
p := s.browser.Page("")
defer p.Close()
go func() {
kit.Sleep(0.1)
p.StopLoading()
}()
s.Panics(func() {
p.Navigate(url)
})
}
// It's obvious that, the v8 will take more time to parse long function.
// For BenchmarkCache and BenchmarkNoCache, the difference is nearly 12% which is too much to ignore.
func BenchmarkCacheOff(b *testing.B) {
p := rod.New().Connect().Page(srcFile("fixtures/click.html"))
b.ResetTimer()
for n := 0; n < b.N; n++ {
p.Eval(`(time) => {
// won't call this function, it's used to make the declaration longer
function foo (id, left, top, width, height, msg) {
var div = document.createElement('div')
var msgDiv = document.createElement('div')
div.id = id
div.style = 'position: fixed; z-index:2147483647; border: 2px dashed red;'
+ 'border-radius: 3px; box-shadow: #5f3232 0 0 3px; pointer-events: none;'
+ 'box-sizing: border-box;'
+ 'left:' + left + 'px;'
+ 'top:' + top + 'px;'
+ 'height:' + height + 'px;'
+ 'width:' + width + 'px;'
if (height === 0) {
div.style.border = 'none'
}
msgDiv.style = 'position: absolute; color: #cc26d6; font-size: 12px; background: #ffffffeb;'
+ 'box-shadow: #333 0 0 3px; padding: 2px 5px; border-radius: 3px; white-space: nowrap;'
+ 'top:' + height + 'px; '
msgDiv.innerHTML = msg
div.appendChild(msgDiv)
document.body.appendChild(div)
}
return time
}`, time.Now().UnixNano())
}
}
func BenchmarkCache(b *testing.B) {
p := rod.New().Connect().Page(srcFile("fixtures/click.html"))
b.ResetTimer()
for n := 0; n < b.N; n++ {
p.Eval(`(time) => {
return time
}`, time.Now().UnixNano())
}
}