diff --git a/common/frame_session.go b/common/frame_session.go index cbcccf629..080e64939 100644 --- a/common/frame_session.go +++ b/common/frame_session.go @@ -705,6 +705,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.frameLifecycleEvent(event.FrameID, LifecycleEventNetworkIdle) } eventToMetric := map[string]*k6metrics.Metric{ diff --git a/tests/frame_test.go b/tests/frame_test.go index 7741feaf7..5c7c44026 100644 --- a/tests/frame_test.go +++ b/tests/frame_test.go @@ -1,9 +1,15 @@ package tests import ( + "fmt" + "net/http" "testing" + "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/grafana/xk6-browser/common" ) func TestFramePress(t *testing.T) { @@ -21,3 +27,58 @@ func TestFramePress(t *testing.T) { require.Equal(t, "AbC", f.InputValue("#text1", nil)) } + +func TestLifecycleNetworkIdle(t *testing.T) { + t.Parallel() + + t.Run("doesn't timeout waiting for networkIdle", func(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + p := tb.NewPage(nil) + + tb.withHandler("/home", func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprintf(w, ` + + + +
Waiting...
+ + + + + `) + }) + tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprintf(w, ` + var serverMsgOutput = document.getElementById("serverMsg"); + serverMsgOutput.innerText = "ping.js loaded from server"; + `) + }) + + var resolved, rejected bool + err := tb.await(func() error { + opts := tb.toGojaValue(common.FrameGotoOptions{ + WaitUntil: common.LifecycleEventNetworkIdle, + Timeout: 30 * time.Second, + }) + tb.promise(p.Goto(tb.URL("/home"), opts)).then( + func() { + result := p.TextContent("#serverMsg", nil) + assert.EqualValues(t, "ping.js loaded from server", result) + + resolved = true + }, + func() { + rejected = true + }, + ) + + return nil + }) + require.NoError(t, err) + + assert.True(t, resolved) + assert.False(t, rejected) + }) +}