Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 4d18085

Browse files
authored
Merge pull request #174 from grafana/improve/mainframe-handling
2 parents aadc374 + 88b2ba9 commit 4d18085

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

chromium/browser_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func makeLogger(ctx context.Context, launchOpts *common.LaunchOptions) (*common.
174174
reCategoryFilter, _ = regexp.Compile(launchOpts.LogCategoryFilter)
175175
logger = common.NewLogger(ctx, k6Logger, launchOpts.Debug, reCategoryFilter)
176176
)
177+
177178
// set the log level from the launch options (usually from a script's options).
178179
if launchOpts.Debug {
179180
_ = logger.SetLevel("debug")

common/frame.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (f *Frame) clearLifecycle() {
166166
}
167167
f.lifecycleEventsMu.Unlock()
168168

169-
f.page.frameManager.mainFrame.recalculateLifecycle()
169+
f.page.frameManager.MainFrame().recalculateLifecycle()
170170

171171
// keep the request related to the document if present
172172
// in f.inflightRequests
@@ -419,7 +419,7 @@ func (f *Frame) position() *Position {
419419
if frame == nil {
420420
return nil
421421
}
422-
if frame == f.page.frameManager.mainFrame {
422+
if frame == f.page.frameManager.MainFrame() {
423423
return &Position{X: 0, Y: 0}
424424
}
425425
element := frame.FrameElement()
@@ -1358,6 +1358,10 @@ func (f *Frame) Uncheck(selector string, opts goja.Value) {
13581358

13591359
// URL returns the frame URL.
13601360
func (f *Frame) URL() string {
1361+
if f == nil {
1362+
return ""
1363+
}
1364+
13611365
f.propertiesMu.RLock()
13621366
defer f.propertiesMu.RUnlock()
13631367

common/frame_manager.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (m *FrameManager) frameLifecycleEvent(frameID cdp.FrameID, event LifecycleE
203203
frame := m.getFrameByID(frameID)
204204
if frame != nil {
205205
frame.onLifecycleEvent(event)
206-
m.mainFrame.recalculateLifecycle() // Recalculate life cycle state from the top
206+
m.MainFrame().recalculateLifecycle() // Recalculate life cycle state from the top
207207
}
208208
}
209209

@@ -253,23 +253,26 @@ func (m *FrameManager) frameNavigated(frameID cdp.FrameID, parentFrameID cdp.Fra
253253
}
254254
}
255255

256-
if isMainFrame {
257-
if frame != nil {
258-
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:delete",
259-
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t oldfid:%v",
260-
m.ID(), frameID, parentFrameID, documentID, name, url, initial, frame.ID())
256+
var mainFrame *Frame
257+
if isMainFrame && frame == nil {
258+
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:initialMainFrameNavigation",
259+
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
260+
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
261261

262-
// Update frame ID to retain frame identity on cross-process navigation.
263-
delete(m.frames, cdp.FrameID(frame.ID()))
264-
frame.setID(frameID)
265-
} else {
266-
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:initialMainFrameNavigation",
267-
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
268-
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
262+
// Initial main frame navigation.
263+
frame = NewFrame(m.ctx, m, nil, frameID, m.logger)
264+
mainFrame = frame
265+
} else if isMainFrame && frame.ID() != string(frameID) {
266+
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:delete",
267+
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t oldfid:%v",
268+
m.ID(), frameID, parentFrameID, documentID, name, url, initial, frame.ID())
269269

270-
// Initial main frame navigation.
271-
frame = NewFrame(m.ctx, m, nil, frameID, m.logger)
272-
}
270+
// Update frame ID to retain frame identity on cross-process navigation.
271+
delete(m.frames, cdp.FrameID(frame.ID()))
272+
frame.setID(frameID)
273+
mainFrame = frame
274+
}
275+
if mainFrame != nil {
273276
m.frames[frameID] = frame
274277
m.setMainFrame(frame)
275278
}
@@ -548,6 +551,11 @@ func (m *FrameManager) MainFrame() *Frame {
548551
func (m *FrameManager) setMainFrame(f *Frame) {
549552
m.mainFrameMu.Lock()
550553
defer m.mainFrameMu.Unlock()
554+
555+
m.logger.Debugf("FrameManager:setMainFrame",
556+
"fmid:%d fid:%v furl:%s",
557+
m.ID(), f.ID(), f.URL())
558+
551559
m.mainFrame = f
552560
}
553561

common/page.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func (p *Page) Reload(opts goja.Value) api.Response {
681681
k6common.Throw(rt, fmt.Errorf("failed parsing options: %w", err))
682682
}
683683

684-
ch, evCancelFn := createWaitForEventHandler(p.ctx, p.frameManager.mainFrame, []string{EventFrameNavigation}, func(data interface{}) bool {
684+
ch, evCancelFn := createWaitForEventHandler(p.ctx, p.frameManager.MainFrame(), []string{EventFrameNavigation}, func(data interface{}) bool {
685685
return true // Both successful and failed navigations are considered
686686
})
687687
defer evCancelFn() // Remove event handler
@@ -701,7 +701,7 @@ func (p *Page) Reload(opts goja.Value) api.Response {
701701
}
702702

703703
if p.frameManager.mainFrame.hasSubtreeLifecycleEventFired(parsedOpts.WaitUntil) {
704-
waitForEvent(p.ctx, p.frameManager.mainFrame, []string{EventFrameAddLifecycle}, func(data interface{}) bool {
704+
_, _ = waitForEvent(p.ctx, p.frameManager.MainFrame(), []string{EventFrameAddLifecycle}, func(data interface{}) bool {
705705
return data.(LifecycleEvent) == parsedOpts.WaitUntil
706706
}, parsedOpts.Timeout)
707707
}

common/screenshotter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *screenshotter) fullPageSize(p *Page) (*Size, error) {
5151
forceCallable: true,
5252
returnByValue: true,
5353
}
54-
result, err := p.frameManager.mainFrame.evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
54+
result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
5555
() => {
5656
if (!document.body || !document.documentElement) {
5757
return null;
@@ -95,7 +95,7 @@ func (s *screenshotter) originalViewportSize(p *Page) (*Size, *Size, error) {
9595
forceCallable: true,
9696
returnByValue: true,
9797
}
98-
result, err := p.frameManager.mainFrame.evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
98+
result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
9999
() => (
100100
{ width: window.innerWidth, height: window.innerHeight }
101101
)`))

0 commit comments

Comments
 (0)