Skip to content

Commit

Permalink
Fix networkIdle waitUntil option for load
Browse files Browse the repository at this point in the history
When a site that is navigated to emits a DOMContentLoaded well before
load, this is a signal that we're waiting on assets to load over the
network. In such a case (e.g. navigating to google.com) when we goto
with waitUntil = networkIdle, due to how we handle such lifecycle
events, we end up in a situation where the networkIdle event is
ignored. This change ensure that we action on these events too.

Partly fixes: https://github.com/orgs/grafana/projects/80/views/14
  • Loading branch information
ankur22 committed Oct 19, 2022
1 parent bdeae6b commit d81d9aa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
61 changes: 61 additions & 0 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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, `
<html>
<head></head>
<body>
<div id="serverMsg">Waiting...</div>
<script src="/ping.js" async></script>
</body>
</html>
`)
})
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)
})
}

0 comments on commit d81d9aa

Please sign in to comment.