forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nav_test.go
428 lines (363 loc) · 9.81 KB
/
nav_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
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
package chromedp
import (
"context"
"errors"
"fmt"
_ "image/png"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
)
func TestNavigate(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
var urlstr, title string
if err := Run(ctx,
Location(&urlstr),
Title(&title),
); err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(urlstr, "image.html") {
t.Errorf("want to be on image.html, at %q", urlstr)
}
exptitle := "this is title"
if title != exptitle {
t.Errorf("want title to be %q, got %q", title, exptitle)
}
}
func TestNavigationEntries(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "")
defer cancel()
tests := []struct {
file, waitID string
}{
{"form.html", "#form"},
{"image.html", "#icon-brankas"},
}
var entries []*page.NavigationEntry
var index int64
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Errorf("expected to have 1 navigation entry: got %d", len(entries))
}
if index != 0 {
t.Errorf("expected navigation index is 0, got: %d", index)
}
expIdx, expEntries := 1, 2
for i, test := range tests {
if err := Run(ctx,
Navigate(testdataDir+"/"+test.file),
NavigationEntries(&index, &entries),
); err != nil {
t.Fatal(err)
}
if len(entries) != expEntries {
t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries))
}
if want := int64(i + 1); index != want {
t.Errorf("test %d expected navigation index is %d, got: %d", i, want, index)
}
expIdx++
expEntries++
}
}
func TestNavigateToHistoryEntry(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
var entries []*page.NavigationEntry
var index int64
if err := Run(ctx,
NavigationEntries(&index, &entries),
Navigate(testdataDir+"/form.html"),
); err != nil {
t.Fatal(err)
}
var title string
if err := Run(ctx,
NavigateToHistoryEntry(entries[index].ID),
Title(&title),
); err != nil {
t.Fatal(err)
}
if title != entries[index].Title {
t.Errorf("expected title to be %q, instead title is %q", entries[index].Title, title)
}
}
func TestNavigateBack(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var title, exptitle string
if err := Run(ctx,
Title(&exptitle),
Navigate(testdataDir+"/image.html"),
NavigateBack(),
Title(&title),
); err != nil {
t.Fatal(err)
}
if title != exptitle {
t.Errorf("expected title to be %q, instead title is %q", exptitle, title)
}
}
func TestNavigateForward(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var title, exptitle string
if err := Run(ctx,
Navigate(testdataDir+"/image.html"),
Title(&exptitle),
NavigateBack(),
NavigateForward(),
Title(&title),
); err != nil {
t.Fatal(err)
}
if title != exptitle {
t.Errorf("expected title to be %q, instead title is %q", exptitle, title)
}
}
func TestStop(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
if err := Run(ctx, Stop()); err != nil {
t.Fatal(err)
}
}
func TestReload(t *testing.T) {
t.Parallel()
count := 0
// create test server
mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, `<html>
<head>
<title>Title %d</title>
</head>
</html>`, count)
count++
})
s := httptest.NewServer(mux)
defer s.Close()
ctx, cancel := testAllocate(t, "")
defer cancel()
var firstTitle, secondTitle string
if err := Run(ctx,
Navigate(s.URL),
Title(&firstTitle),
Reload(),
Title(&secondTitle),
); err != nil {
t.Fatal(err)
}
if want := "Title 0"; firstTitle != want {
t.Errorf("expected first title to be %q, instead title is %q", want, firstTitle)
}
if want := "Title 1"; secondTitle != want {
t.Errorf("expected second title to be %q, instead title is %q", want, secondTitle)
}
}
func TestLocation(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "form.html")
defer cancel()
var urlstr string
if err := Run(ctx, Location(&urlstr)); err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(urlstr, "form.html") {
t.Fatalf("expected to be on form.html, got %q", urlstr)
}
}
func TestTitle(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
var title string
if err := Run(ctx, Title(&title)); err != nil {
t.Fatal(err)
}
exptitle := "this is title"
if title != exptitle {
t.Fatalf("expected title to be %q, got %q", exptitle, title)
}
}
func TestQueryIframe(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "iframe.html")
defer cancel()
var iframes, forms []*cdp.Node
if err := Run(ctx, Nodes(`iframe`, &iframes, ByQuery)); err != nil {
t.Fatal(err)
}
iframe := iframes[0]
if err := Run(ctx, Nodes(`#form`, &forms, ByQuery, FromNode(iframe))); err != nil {
t.Fatal(err)
}
form := forms[0]
var gotFoo string
if err := Run(ctx,
WaitVisible(`#form`, ByQuery, FromNode(iframe)),
Text("#foo", &gotFoo, ByQuery, FromNode(form)),
Click("#btn1", ByQuery, FromNode(iframe)),
Click("#btn2", ByQuery, FromNode(form)),
); err != nil {
t.Fatal(err)
}
if want := "insert"; gotFoo != want {
t.Fatalf("wanted %q, got %q", want, gotFoo)
}
}
func TestNavigateContextTimeout(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "")
defer cancel()
// Serve the page, but cancel the context almost immediately after.
// Navigate shouldn't block waiting for the load to finish, which may
// not come as the target is cancelled.
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
go func() {
time.Sleep(time.Millisecond)
cancel()
}()
}))
defer s.Close()
if err := Run(ctx, Navigate(s.URL)); err != nil && err != context.Canceled {
t.Fatal(err)
}
}
func writeHTML(content string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, strings.TrimSpace(content))
})
}
func TestNavigateWhileLoading(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "")
defer cancel()
mux := http.NewServeMux()
mux.Handle("/", writeHTML(`
<img src="/img.jpg"></img>
`))
ch := make(chan struct{})
mux.HandleFunc("/img.jpg", func(w http.ResponseWriter, r *http.Request) {
<-ch
})
s := httptest.NewServer(mux)
defer s.Close()
// First, navigate to a page that starts loading, but doesn't finish.
// Then, tell the server to finish loading the page.
// Immediately after, navigate to another page.
// Finally, grab the page title, which should correspond with the last
// page.
//
// This has caused problems in the past. Because the first page might
// fire its load event just as we start the second navigate, the second
// navigate used to get confused, either blocking forever or not waiting
// for the right load event (the second).
var title string
if err := Run(ctx,
ActionFunc(func(ctx context.Context) error {
var wg sync.WaitGroup
wg.Add(1)
lctx, cancel := context.WithCancel(ctx)
ListenTarget(lctx, func(ev interface{}) {
if ev, ok := ev.(*page.EventLifecycleEvent); ok {
if ev.Name == "init" {
cancel()
wg.Done()
}
}
})
_, _, _, err := page.Navigate(s.URL).Do(ctx)
// Make sure the Page.lifecycleEvent with the name "init" is emitted
// before starting the second navigate.
//
// Otherwise, it's possible that this event is emitted after the
// second navigate, and the second navigate will handle the wrong
// events. See https://github.com/chromedp/chromedp/issues/1080.
//
// The implementation of responseAction() is buggy in this case.
// But it's hard to fix it since there is not way to tell whether
// the events are from the first navigate.
//
// I (ZekeLu) will just deflake this test by making sure the second
// navigate won't see this event from the first navigate.
//
// The issue can be reproduced by commenting out the next line.
wg.Wait()
ch <- struct{}{}
return err
}),
Navigate(testdataDir+"/image.html"),
Title(&title),
); err != nil {
t.Fatal(err)
}
exptitle := "this is title"
if title != exptitle {
t.Errorf("want title to be %q, got %q", exptitle, title)
}
}
func TestNavigateWithoutWaitingForLoad(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "")
defer cancel()
// If we run a query without waiting for the page to load, chromedp used
// to panic.
if err := Run(ctx,
ActionFunc(func(ctx context.Context) error {
_, _, _, err := page.Navigate(testdataDir + "/form.html").Do(ctx)
return err
}),
WaitVisible(`#form`, ByID), // for form.html
); err != nil {
t.Fatal(err)
}
}
func TestNavigateCancelled(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "")
defer cancel()
loadStarted := make(chan struct{})
mux := http.NewServeMux()
mux.Handle("/", writeHTML(`<img src="/img.jpg"></img>`))
mux.HandleFunc("/img.jpg", func(w http.ResponseWriter, r *http.Request) {
// Block until the entire test is done.
<-ctx.Done()
})
s := httptest.NewServer(mux)
defer s.Close()
defer cancel() // if we call s.Close first, the ctx.Done above hangs
// Navigate to a page that will navigate, but never finish loading. Once
// it has the HTML and starts loading an image, cancel the Run context.
// This should result in us seeing a context error.
action := ActionFunc(func(ctx context.Context) error {
_, _, _, err := page.Navigate(s.URL).Do(ctx)
loadStarted <- struct{}{}
return err
})
ctx2, cancel2 := context.WithCancel(ctx)
go func() {
<-loadStarted
cancel2()
}()
if _, err := RunResponse(ctx2, action); !errors.Is(err, context.Canceled) {
t.Fatalf("expected error to be %q, got: %v", context.Canceled, err)
}
}