-
Notifications
You must be signed in to change notification settings - Fork 42
/
frame_manager.go
730 lines (594 loc) · 20.6 KB
/
frame_manager.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
package common
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/log"
k6modules "go.k6.io/k6/js/modules"
"github.com/chromedp/cdproto/cdp"
cdppage "github.com/chromedp/cdproto/page"
)
// FrameManager manages all frames in a page and their life-cycles, it's a purely internal component.
type FrameManager struct {
ctx context.Context
session session
page *Page
timeoutSettings *TimeoutSettings
// protects from the data race between:
// - Frame.startNetworkIdleTimer.frameLifecycleEvent.recalculateLifecycle
// - *FrameSession.initEvents.onFrameNavigated->FrameManager.frameNavigated
mainFrameMu sync.RWMutex
mainFrame *Frame
// Needed as the frames map will be accessed from multiple Go routines,
// the main VU/JS go routine and the Go routine listening for CDP messages.
framesMu sync.RWMutex
frames map[cdp.FrameID]*Frame
barriersMu sync.RWMutex
barriers []*Barrier
vu k6modules.VU
logger *log.Logger
id int64
}
// frameManagerID is used for giving a unique ID to a frame manager.
var frameManagerID int64
// NewFrameManager creates a new HTML document frame manager.
func NewFrameManager(
ctx context.Context,
s session,
p *Page,
ts *TimeoutSettings,
l *log.Logger,
) *FrameManager {
m := &FrameManager{
ctx: ctx,
session: s,
page: p,
timeoutSettings: ts,
frames: make(map[cdp.FrameID]*Frame),
barriers: make([]*Barrier, 0),
vu: k6ext.GetVU(ctx),
logger: l,
id: atomic.AddInt64(&frameManagerID, 1),
}
m.logger.Debugf("FrameManager:New", "fmid:%d", m.ID())
return m
}
func (m *FrameManager) addBarrier(b *Barrier) {
m.logger.Debugf("FrameManager:addBarrier", "fmid:%d", m.ID())
m.barriersMu.Lock()
defer m.barriersMu.Unlock()
m.barriers = append(m.barriers, b)
}
func (m *FrameManager) removeBarrier(b *Barrier) {
m.logger.Debugf("FrameManager:removeBarrier", "fmid:%d", m.ID())
m.barriersMu.Lock()
defer m.barriersMu.Unlock()
index := -1
for i, b2 := range m.barriers {
if b == b2 {
index = i
break
}
}
m.barriers = append(m.barriers[:index], m.barriers[index+1:]...)
}
func (m *FrameManager) frameAbortedNavigation(frameID cdp.FrameID, errorText, documentID string) {
m.logger.Debugf("FrameManager:frameAbortedNavigation",
"fmid:%d fid:%v err:%s docid:%s",
m.ID(), frameID, errorText, documentID)
m.framesMu.Lock()
defer m.framesMu.Unlock()
frame := m.frames[frameID]
if frame == nil {
return
}
frame.pendingDocumentMu.Lock()
if frame.pendingDocument == nil {
frame.pendingDocumentMu.Unlock()
return
}
if documentID != "" && frame.pendingDocument.documentID != documentID {
frame.pendingDocumentMu.Unlock()
return
}
m.logger.Debugf("FrameManager:frameAbortedNavigation:emit:EventFrameNavigation",
"fmid:%d fid:%v err:%s docid:%s fname:%s furl:%s",
m.ID(), frameID, errorText, documentID, frame.Name(), frame.URL())
ne := &NavigationEvent{
url: frame.URL(),
name: frame.Name(),
newDocument: frame.pendingDocument,
err: errors.New(errorText),
}
frame.pendingDocument = nil
frame.pendingDocumentMu.Unlock()
frame.emit(EventFrameNavigation, ne)
}
func (m *FrameManager) frameAttached(frameID cdp.FrameID, parentFrameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameAttached", "fmid:%d fid:%v pfid:%v",
m.ID(), frameID, parentFrameID)
m.framesMu.Lock()
defer m.framesMu.Unlock()
if _, ok := m.frames[frameID]; ok {
m.logger.Debugf("FrameManager:frameAttached:return",
"fmid:%d fid:%v pfid:%v cannot find frame",
m.ID(), frameID, parentFrameID)
return
}
if parentFrame, ok := m.frames[parentFrameID]; ok {
frame := NewFrame(m.ctx, m, parentFrame, frameID, m.logger)
// TODO: create a addFrame func
m.frames[frameID] = frame
parentFrame.addChildFrame(frame)
m.logger.Debugf("FrameManager:frameAttached:emit:EventPageFrameAttached",
"fmid:%d fid:%v pfid:%v", m.ID(), frameID, parentFrameID)
m.page.emit(EventPageFrameAttached, frame)
}
}
func (m *FrameManager) frameDetached(frameID cdp.FrameID, reason cdppage.FrameDetachedReason) error {
m.logger.Debugf("FrameManager:frameDetached", "fmid:%d fid:%v", m.ID(), frameID)
frame, ok := m.getFrameByID(frameID)
if !ok {
m.logger.Debugf("FrameManager:frameDetached:return",
"fmid:%d fid:%v cannot find frame",
m.ID(), frameID)
return nil
}
// This helps prevent an iframe and its child frames from being removed
// when the type of detach is a swap. After this detach event usually
// the iframe navigates, which requires the frames to be present for the
// navigate to work.
fs := m.page.getFrameSession(frameID)
if fs != nil {
m.logger.Debugf("FrameManager:frameDetached:sessionFound",
"fmid:%d fid:%v fsID1:%v fsID2:%v found session for frame",
m.ID(), frameID, fs.session.ID(), m.session.ID())
if fs.session.ID() != m.session.ID() {
m.logger.Debugf("FrameManager:frameDetached:notSameSession:return",
"fmid:%d fid:%v event session and frame session do not match",
m.ID(), frameID)
return nil
}
}
if reason == cdppage.FrameDetachedReasonSwap {
// When a local frame is swapped out for a remote
// frame, we want to keep the current frame which is
// still referenced by the (incoming) remote frame, but
// remove all its child frames.
return m.removeChildFramesRecursively(frame)
}
return m.removeFramesRecursively(frame)
}
func (m *FrameManager) frameLifecycleEvent(frameID cdp.FrameID, event LifecycleEvent) {
m.logger.Debugf("FrameManager:frameLifecycleEvent",
"fmid:%d fid:%v event:%s",
m.ID(), frameID, lifecycleEventToString[event])
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLifecycleEvent(event)
}
}
func (m *FrameManager) frameLoadingStarted(frameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameLoadingStarted",
"fmid:%d fid:%v", m.ID(), frameID)
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLoadingStarted()
}
}
func (m *FrameManager) frameLoadingStopped(frameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameLoadingStopped",
"fmid:%d fid:%v", m.ID(), frameID)
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLoadingStopped()
}
}
func (m *FrameManager) frameNavigated(frameID cdp.FrameID, parentFrameID cdp.FrameID, documentID string, name string, url string, initial bool) error {
m.logger.Debugf("FrameManager:frameNavigated",
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
// TODO: add test to make sure the navigated frame has correct ID, parent ID, loader ID, name and URL
m.framesMu.Lock()
defer m.framesMu.Unlock()
isMainFrame := parentFrameID == ""
frame := m.frames[frameID]
if !isMainFrame && frame == nil {
tempDone := make(chan struct{})
go func() {
defer close(tempDone)
time.Sleep(5 * time.Second)
}()
<-tempDone
return errors.New("we either navigate top level or have old version of the navigated frame")
}
m.logger.Debugf("FrameManager:frameNavigated:removeFrames",
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
if frame != nil {
m.framesMu.Unlock()
for _, child := range frame.ChildFrames() {
if err := m.removeFramesRecursively(child); err != nil {
m.framesMu.Lock()
return fmt.Errorf("removing child frames recursively: %w", err)
}
}
m.framesMu.Lock()
}
var mainFrame *Frame
if isMainFrame && frame == nil {
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:initialMainFrameNavigation",
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
// Initial main frame navigation.
frame = NewFrame(m.ctx, m, nil, frameID, m.logger)
mainFrame = frame
} else if isMainFrame && frame.ID() != string(frameID) {
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:delete",
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t oldfid:%v",
m.ID(), frameID, parentFrameID, documentID, name, url, initial, frame.ID())
// Update frame ID to retain frame identity on cross-process navigation.
delete(m.frames, cdp.FrameID(frame.ID()))
frame.setID(frameID)
mainFrame = frame
}
if mainFrame != nil {
m.frames[frameID] = frame
m.setMainFrame(frame)
}
frame.navigated(name, url, documentID)
frame.pendingDocumentMu.Lock()
defer frame.pendingDocumentMu.Unlock()
var (
keepPending *DocumentInfo
pendingDocument = frame.pendingDocument
)
if pendingDocument != nil {
if pendingDocument.documentID == "" {
pendingDocument.documentID = documentID
}
if pendingDocument.documentID == documentID {
// Committing a pending document.
frame.currentDocument = pendingDocument
} else {
// Sometimes, we already have a new pending when the old one commits.
// An example would be Chromium error page followed by a new navigation request,
// where the error page commit arrives after Network.requestWillBeSent for the
// new navigation.
// We commit, but keep the pending request since it's not done yet.
keepPending = pendingDocument
frame.currentDocument = &DocumentInfo{
documentID: documentID,
request: nil,
}
}
frame.pendingDocument = nil
} else {
// No pending, just commit a new document.
frame.currentDocument = &DocumentInfo{
documentID: documentID,
request: nil,
}
}
m.logger.Debugf("FrameManager:frameNavigated",
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t pdoc:nil - fcurdoc:%v",
m.ID(), frameID, parentFrameID, documentID, name, url, initial, documentID)
frame.clearLifecycle()
frame.emit(EventFrameNavigation, &NavigationEvent{url: url, name: name, newDocument: frame.currentDocument})
// TODO: when we add API support for storage we need to track origins
// if !initial {
// //f.page.frameNavigatedToNewDocument(f)
// }
// Restore pending if any (see comments above about keepPending).
frame.pendingDocument = keepPending
return nil
}
func (m *FrameManager) frameNavigatedWithinDocument(frameID cdp.FrameID, url string) {
m.logger.Debugf("FrameManager:frameNavigatedWithinDocument",
"fmid:%d fid:%v url:%s", m.ID(), frameID, url)
m.framesMu.Lock()
defer m.framesMu.Unlock()
frame := m.frames[frameID]
if frame == nil {
m.logger.Debugf("FrameManager:frameNavigatedWithinDocument:nilFrame:return",
"fmid:%d fid:%v url:%s", m.ID(), frameID, url)
return
}
m.logger.Debugf("FrameManager:frameNavigatedWithinDocument",
"fmid:%d fid:%v furl:%s url:%s", m.ID(), frameID, frame.URL(), url)
frame.setURL(url)
frame.emit(EventFrameNavigation, &NavigationEvent{url: url, name: frame.Name()})
}
func (m *FrameManager) frameRequestedNavigation(frameID cdp.FrameID, url string, documentID string) error {
m.logger.Debugf("FrameManager:frameRequestedNavigation",
"fmid:%d fid:%v url:%s docid:%s", m.ID(), frameID, url, documentID)
m.framesMu.Lock()
defer m.framesMu.Unlock()
frame := m.frames[frameID]
if frame == nil {
m.logger.Debugf("FrameManager:frameRequestedNavigation:nilFrame:return",
"fmid:%d fid:%v url:%s docid:%s", m.ID(), frameID, url, documentID)
return fmt.Errorf("no frame exists with ID %s", frameID)
}
m.barriersMu.RLock()
defer m.barriersMu.RUnlock()
for _, b := range m.barriers {
m.logger.Debugf("FrameManager:frameRequestedNavigation:AddFrameNavigation",
"fmid:%d fid:%v furl:%s url:%s docid:%s", m.ID(), frameID, frame.URL(), url, documentID)
b.AddFrameNavigation(frame)
}
frame.pendingDocumentMu.Lock()
defer frame.pendingDocumentMu.Unlock()
if frame.pendingDocument != nil && frame.pendingDocument.documentID == documentID {
m.logger.Debugf("FrameManager:frameRequestedNavigation:return",
"fmid:%d fid:%v furl:%s url:%s docid:%s pdocid:%s pdoc:dontSet",
m.ID(), frameID, frame.URL(), url, documentID,
frame.pendingDocument.documentID)
// Do not override request with nil
return nil
}
m.logger.Debugf("FrameManager:frameRequestedNavigation:return",
"fmid:%d fid:%v furl:%s url:%s docid:%s pdoc:set",
m.ID(), frameID, frame.URL(), url, documentID)
frame.pendingDocument = &DocumentInfo{documentID: documentID}
return nil
}
// getFrameByID finds a frame with id. If found, it returns the frame and true,
// otherwise, it returns nil and false.
func (m *FrameManager) getFrameByID(id cdp.FrameID) (*Frame, bool) {
m.framesMu.RLock()
defer m.framesMu.RUnlock()
frame, ok := m.frames[id]
return frame, ok
}
func (m *FrameManager) removeChildFramesRecursively(frame *Frame) error {
for _, child := range frame.ChildFrames() {
if err := m.removeFramesRecursively(child); err != nil {
return fmt.Errorf("removing child frames recursively: %w", err)
}
}
return nil
}
func (m *FrameManager) removeFramesRecursively(frame *Frame) error {
for _, child := range frame.ChildFrames() {
m.logger.Debugf("FrameManager:removeFramesRecursively",
"fmid:%d cfid:%v pfid:%v cfname:%s cfurl:%s",
m.ID(), child.ID(), frame.ID(), child.Name(), child.URL())
if err := m.removeFramesRecursively(child); err != nil {
return fmt.Errorf("removing frames recursively: %w", err)
}
}
if err := frame.detach(); err != nil {
return fmt.Errorf("removing frames recursively: detaching frame: %w", err)
}
m.framesMu.Lock()
m.logger.Debugf("FrameManager:removeFramesRecursively:delParentFrame",
"fmid:%d fid:%v fname:%s furl:%s",
m.ID(), frame.ID(), frame.Name(), frame.URL())
delete(m.frames, cdp.FrameID(frame.ID()))
m.framesMu.Unlock()
if !m.page.IsClosed() {
m.logger.Debugf("FrameManager:removeFramesRecursively:emit:EventPageFrameDetached",
"fmid:%d fid:%v fname:%s furl:%s",
m.ID(), frame.ID(), frame.Name(), frame.URL())
m.page.emit(EventPageFrameDetached, frame)
}
return nil
}
func (m *FrameManager) requestFailed(req *Request, canceled bool) {
m.logger.Debugf("FrameManager:requestFailed", "fmid:%d rurl:%s", m.ID(), req.URL())
defer m.page.emit(EventPageRequestFailed, req)
frame := req.getFrame()
if frame == nil {
m.logger.Debugf("FrameManager:requestFailed", "frame is nil")
return
}
frame.deleteRequest(req.getID())
frame.pendingDocumentMu.RLock()
if frame.pendingDocument == nil || frame.pendingDocument.request != req {
m.logger.Debugf("FrameManager:requestFailed:return", "fmid:%d pdoc:nil", m.ID())
frame.pendingDocumentMu.RUnlock()
return
}
errorText := req.errorText
if canceled {
errorText += "; maybe frame was detached?"
}
docID := frame.pendingDocument.documentID
frame.pendingDocumentMu.RUnlock()
m.frameAbortedNavigation(cdp.FrameID(frame.ID()), errorText, docID)
}
func (m *FrameManager) requestFinished(req *Request) {
m.logger.Debugf("FrameManager:requestFinished", "fmid:%d rurl:%s",
m.ID(), req.URL())
defer m.page.emit(EventPageRequestFinished, req)
frame := req.getFrame()
if frame == nil {
m.logger.Debugf("FrameManager:requestFinished:return",
"fmid:%d rurl:%s frame:nil", m.ID(), req.URL())
return
}
frame.deleteRequest(req.getID())
/*
else if frame.inflightRequestsLen() <= 10 {
for reqID, _ := range frame.inflightRequests {
req := frame.requestByID(reqID)
}
}
*/
}
func (m *FrameManager) requestReceivedResponse(res *Response) {
m.logger.Debugf("FrameManager:requestReceivedResponse", "fmid:%d rurl:%s", m.ID(), res.URL())
m.page.emit(EventPageResponse, res)
}
func (m *FrameManager) requestStarted(req *Request) {
m.logger.Debugf("FrameManager:requestStarted", "fmid:%d rurl:%s", m.ID(), req.URL())
m.framesMu.Lock()
defer m.framesMu.Unlock()
defer m.page.emit(EventPageRequest, req)
frame := req.getFrame()
if frame == nil {
m.logger.Debugf("FrameManager:requestStarted:return",
"fmid:%d rurl:%s frame:nil", m.ID(), req.URL())
return
}
frame.addRequest(req.getID())
if req.documentID != "" {
frame.pendingDocumentMu.Lock()
frame.pendingDocument = &DocumentInfo{documentID: req.documentID, request: req}
frame.pendingDocumentMu.Unlock()
}
m.logger.Debugf("FrameManager:requestStarted", "fmid:%d rurl:%s pdoc:nil", m.ID(), req.URL())
}
// Frames returns a list of frames on the page.
func (m *FrameManager) Frames() []*Frame {
m.framesMu.RLock()
defer m.framesMu.RUnlock()
frames := make([]*Frame, 0)
for _, frame := range m.frames {
frames = append(frames, frame)
}
return frames
}
// MainFrame returns the main frame of the page.
func (m *FrameManager) MainFrame() *Frame {
m.mainFrameMu.RLock()
defer m.mainFrameMu.RUnlock()
return m.mainFrame
}
// setMainFrame sets the main frame of the page.
func (m *FrameManager) setMainFrame(f *Frame) {
m.mainFrameMu.Lock()
defer m.mainFrameMu.Unlock()
m.logger.Debugf("FrameManager:setMainFrame",
"fmid:%d fid:%v furl:%s",
m.ID(), f.ID(), f.URL())
m.mainFrame = f
}
// MainFrameURL returns the main frame's url.
func (m *FrameManager) MainFrameURL() string {
m.mainFrameMu.RLock()
defer m.mainFrameMu.RUnlock()
return m.mainFrame.URL()
}
// NavigateFrame will navigate specified frame to specified URL.
//
//nolint:funlen,cyclop
func (m *FrameManager) NavigateFrame(frame *Frame, url string, parsedOpts *FrameGotoOptions) (*Response, error) {
var (
fmid = m.ID()
fid = frame.ID()
furl = frame.URL()
)
m.logger.Debugf("FrameManager:NavigateFrame",
"fmid:%d fid:%v furl:%s url:%s", fmid, fid, furl, url)
defer m.logger.Debugf("FrameManager:NavigateFrame:return",
"fmid:%d fid:%v furl:%s url:%s", fmid, fid, furl, url)
timeoutCtx, timeoutCancelFn := context.WithTimeout(m.ctx, parsedOpts.Timeout)
defer timeoutCancelFn()
newDocIDCh := make(chan string, 1)
navEvtCh, navEvtCancel := createWaitForEventHandler(
timeoutCtx, frame, []string{EventFrameNavigation},
func(data any) bool {
newDocID := <-newDocIDCh
if evt, ok := data.(*NavigationEvent); ok {
if evt.newDocument != nil {
return evt.newDocument.documentID == newDocID
}
}
return false
})
defer navEvtCancel()
lifecycleEvtCh, lifecycleEvtCancel := createWaitForEventPredicateHandler(
timeoutCtx, frame, []string{EventFrameAddLifecycle},
func(data any) bool {
le, ok := data.(FrameLifecycleEvent)
if !ok {
return false
}
// skip the initial blank page if we are navigating to a non-blank page.
// otherwise, we will get a lifecycle event for the initial blank page
// and return prematurely before waiting for the navigation to complete.
if url != BlankPage && le.URL == BlankPage {
m.logger.Debugf(
"FrameManager:NavigateFrame:createWaitForEventPredicateHandler",
"fmid:%d fid:%v furl:%s url:%s waitUntil:%s event.lifecycle:%q event.url:%q skipping %s",
fmid, fid, furl, url, parsedOpts.WaitUntil, le.Event, le.URL, BlankPage,
)
return false
}
return le.Event == parsedOpts.WaitUntil
})
defer lifecycleEvtCancel()
fs := frame.page.getFrameSession(cdp.FrameID(frame.ID()))
if fs == nil {
m.logger.Debugf("FrameManager:NavigateFrame",
"fmid:%d fid:%v furl:%s url:%s fs:nil",
fmid, fid, furl, url)
// Attaching an iframe to an existing page doesn't seem to trigger a "Target.attachedToTarget" event
// from the browser even when "Target.setAutoAttach" is true. If this is the case fallback to the
// main frame's session.
fs = frame.page.mainFrameSession
}
newDocumentID, err := fs.navigateFrame(frame, url, parsedOpts.Referer)
if err != nil {
return nil, fmt.Errorf("navigating to %q: %w", url, err)
}
if newDocumentID == "" {
// It's a navigation within the same document (e.g. via anchor links or
// the History API), so don't wait for a response nor any lifecycle
// events.
return nil, nil
}
// unblock the waiter goroutine
newDocIDCh <- newDocumentID
wrapTimeoutError := func(err error) error {
if errors.Is(err, context.DeadlineExceeded) {
err = &k6ext.UserFriendlyError{
Err: err,
Timeout: parsedOpts.Timeout,
}
return fmt.Errorf("navigating to %q: %w", url, err)
}
m.logger.Debugf("FrameManager:NavigateFrame",
"fmid:%d fid:%v furl:%s url:%s timeoutCtx done: %v",
fmid, fid, furl, url, err)
return err // TODO maybe wrap this as well?
}
var resp *Response
select {
case evt := <-navEvtCh:
if e, ok := evt.(*NavigationEvent); ok {
req := e.newDocument.request
// Request could be nil in case of navigation to e.g. BlankPage.
if req != nil {
req.responseMu.RLock()
resp = req.response
req.responseMu.RUnlock()
}
}
case <-timeoutCtx.Done():
return nil, wrapTimeoutError(timeoutCtx.Err())
}
select {
case <-lifecycleEvtCh:
case <-timeoutCtx.Done():
return nil, wrapTimeoutError(timeoutCtx.Err())
}
return resp, nil
}
// Page returns the page that this frame manager belongs to.
func (m *FrameManager) Page() *Page {
if m.page != nil {
return m.page
}
return nil
}
// ID returns the unique ID of a FrameManager value.
func (m *FrameManager) ID() int64 {
return atomic.LoadInt64(&m.id)
}