Skip to content

Commit

Permalink
Fix wait in networkIdle when event already fired
Browse files Browse the repository at this point in the history
We need to try to recalculate the `Frame`'s lifecycle when we receive
a `networkIdle` cdp event. In complex cases, the other lifecycle events
have fired quite early on in the loading of the page, but the network
is still busy which delays the `networkIdle` (and more importantly) the
`EventFrameStoppedLoading` event. The hope is that once we do receive
the `networkIdle` event, we can assume that `EventFrameStoppedLoading`
has also been received, which means that the Frame is in the correct
state and is ready to be recalculated, which in turn will fire off the
internal `networkidle` events. Previously we relied on
`DOMContentLoaded` to have been received at the same time as
`networkIdle` so the Frame would have been in the correct state and the
recalculate would have taken into account the `networkidle` state.
  • Loading branch information
ankur22 committed Oct 11, 2022
1 parent 508bcb8 commit 0887571
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions common/frame_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ func (m *FrameManager) frameLifecycleEvent(frameID cdp.FrameID, event LifecycleE
}
}

// This only tries to recalculate the lifecycle and not set the
// `networkIdle` state to the Frame. `networkIdle` events need to be
// handled after `frameLoadingStopped`` has been called (which
// internally sets the `networkidle` state to the Frame). Once
// this is done, we can safely recalculateLifecycle which should
// emit the internal `networkidle` event.
func (m *FrameManager) frameNetworkIdleLifecycleEvent(frameID cdp.FrameID, event LifecycleEvent) {
m.logger.Debugf("FrameManager:frameNetworkIdleLifecycleEvent",
"fmid:%d fid:%v event:%s",
m.ID(), frameID, lifecycleEventToString[event])

frame := m.getFrameByID(frameID)
if frame != nil {
m.MainFrame().recalculateLifecycle() // Recalculate life cycle state from the top
}
}

func (m *FrameManager) frameLoadingStarted(frameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameLoadingStarted",
"fmid:%d fid:%v", m.ID(), frameID)
Expand Down
6 changes: 4 additions & 2 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ func (fs *FrameSession) handleFrameTree(frameTree *cdppage.FrameTree) {

func (fs *FrameSession) navigateFrame(frame *Frame, url, referrer string) (string, error) {
fs.logger.Debugf("FrameSession:navigateFrame",
"sid:%v tid:%v url:%q referrer:%q",
fs.session.ID(), fs.targetID, url, referrer)
"sid:%v tid:%v url:%q referrer:%q, fid:%s",
fs.session.ID(), fs.targetID, url, referrer, frame.id)

action := cdppage.Navigate(url).WithReferrer(referrer).WithFrameID(cdp.FrameID(frame.ID()))
_, documentID, errorText, err := action.Do(cdp.WithExecutor(fs.ctx, fs.session))
Expand Down Expand Up @@ -721,6 +721,8 @@ func (fs *FrameSession) onPageLifecycle(event *cdppage.EventLifecycleEvent) {
fs.manager.frameLifecycleEvent(event.FrameID, LifecycleEventLoad)
case "DOMContentLoaded":
fs.manager.frameLifecycleEvent(event.FrameID, LifecycleEventDOMContentLoad)
case "networkIdle":
fs.manager.frameNetworkIdleLifecycleEvent(event.FrameID, LifecycleEventNetworkIdle)
}

eventToMetric := map[string]*k6metrics.Metric{
Expand Down

0 comments on commit 0887571

Please sign in to comment.