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

Commit 52264a4

Browse files
committed
Refactor lifecycle waitUntil tests into one file
1 parent 32f3774 commit 52264a4

File tree

4 files changed

+156
-148
lines changed

4 files changed

+156
-148
lines changed

tests/frame_test.go

Lines changed: 0 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package tests
22

33
import (
4-
"fmt"
5-
"net/http"
6-
"sync"
74
"testing"
8-
"time"
95

10-
"github.com/stretchr/testify/assert"
116
"github.com/stretchr/testify/require"
12-
13-
"github.com/grafana/xk6-browser/api"
14-
"github.com/grafana/xk6-browser/common"
157
)
168

179
func TestFramePress(t *testing.T) {
@@ -29,130 +21,3 @@ func TestFramePress(t *testing.T) {
2921

3022
require.Equal(t, "AbC", f.InputValue("#text1", nil))
3123
}
32-
33-
func TestLifecycleNetworkIdle(t *testing.T) {
34-
t.Parallel()
35-
36-
assertHome := func(tb *testBrowser, p api.Page, check func()) {
37-
var resolved, rejected bool
38-
err := tb.await(func() error {
39-
opts := tb.toGojaValue(common.FrameGotoOptions{
40-
WaitUntil: common.LifecycleEventNetworkIdle,
41-
Timeout: 30 * time.Second,
42-
})
43-
tb.promise(p.Goto(tb.URL("/home"), opts)).then(
44-
func() {
45-
check()
46-
resolved = true
47-
},
48-
func() {
49-
rejected = true
50-
},
51-
)
52-
53-
return nil
54-
})
55-
require.NoError(t, err)
56-
57-
assert.True(t, resolved)
58-
assert.False(t, rejected)
59-
}
60-
61-
t.Run("doesn't timeout waiting for networkIdle", func(t *testing.T) {
62-
t.Parallel()
63-
64-
tb := newTestBrowser(t, withHTTPServer())
65-
p := tb.NewPage(nil)
66-
tb.withHandler("/home", func(w http.ResponseWriter, _ *http.Request) {
67-
fmt.Fprintf(w, `
68-
<html>
69-
<head></head>
70-
<body>
71-
<div id="serverMsg">Waiting...</div>
72-
<script src="/ping.js" async></script>
73-
</body>
74-
</html>
75-
`)
76-
})
77-
78-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
79-
fmt.Fprintf(w, `
80-
var serverMsgOutput = document.getElementById("serverMsg");
81-
serverMsgOutput.innerText = "ping.js loaded from server";
82-
`)
83-
})
84-
85-
assertHome(tb, p, func() {
86-
result := p.TextContent("#serverMsg", nil)
87-
assert.EqualValues(t, "ping.js loaded from server", result)
88-
})
89-
})
90-
91-
t.Run("doesn't unblock wait for networkIdle too early", func(t *testing.T) {
92-
t.Parallel()
93-
94-
tb := newTestBrowser(t, withFileServer())
95-
p := tb.NewPage(nil)
96-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
97-
http.Redirect(w, r, tb.staticURL("prolonged_network_idle.html"), http.StatusMovedPermanently)
98-
})
99-
100-
var counter int64
101-
ch := make(chan bool)
102-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
103-
<-ch
104-
105-
var counterMu sync.Mutex
106-
counterMu.Lock()
107-
defer counterMu.Unlock()
108-
109-
time.Sleep(time.Millisecond * 50)
110-
111-
counter++
112-
fmt.Fprintf(w, "pong %d", counter)
113-
})
114-
115-
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
116-
fmt.Fprintf(w, `
117-
var serverMsgOutput = document.getElementById("serverMsg");
118-
serverMsgOutput.innerText = "ping.js loaded from server";
119-
`)
120-
close(ch)
121-
})
122-
123-
assertHome(tb, p, func() {
124-
result := p.TextContent("#prolongNetworkIdleLoad", nil)
125-
assert.EqualValues(t, "Waiting... pong 4 - for loop complete", result)
126-
127-
result = p.TextContent("#serverMsg", nil)
128-
assert.EqualValues(t, "ping.js loaded from server", result)
129-
})
130-
})
131-
132-
t.Run("doesn't unblock wait on networkIdle early when load and domcontentloaded complete at once", func(t *testing.T) {
133-
t.Parallel()
134-
135-
tb := newTestBrowser(t, withFileServer())
136-
p := tb.NewPage(nil)
137-
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
138-
http.Redirect(w, r, tb.staticURL("prolonged_network_idle_10.html"), http.StatusMovedPermanently)
139-
})
140-
141-
var counterMu sync.Mutex
142-
var counter int64
143-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
144-
counterMu.Lock()
145-
defer counterMu.Unlock()
146-
147-
time.Sleep(time.Millisecond * 50)
148-
149-
counter++
150-
fmt.Fprintf(w, "pong %d", counter)
151-
})
152-
153-
assertHome(tb, p, func() {
154-
result := p.TextContent("#prolongNetworkIdleLoad", nil)
155-
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
156-
})
157-
})
158-
}

tests/lifecycle_wait_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"sync"
7+
"testing"
8+
"time"
9+
10+
"github.com/grafana/xk6-browser/api"
11+
"github.com/grafana/xk6-browser/common"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestLifecycleNetworkIdle(t *testing.T) {
17+
t.Parallel()
18+
19+
t.Run("doesn't timeout waiting for networkIdle", func(t *testing.T) {
20+
t.Parallel()
21+
22+
tb := newTestBrowser(t, withHTTPServer())
23+
p := tb.NewPage(nil)
24+
tb.withHandler("/home", func(w http.ResponseWriter, _ *http.Request) {
25+
fmt.Fprintf(w, `
26+
<html>
27+
<head></head>
28+
<body>
29+
<div id="pingJSText">Waiting...</div>
30+
<script src="/ping.js" async></script>
31+
</body>
32+
</html>
33+
`)
34+
})
35+
36+
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
37+
fmt.Fprintf(w, `
38+
var pingJSTextOutput = document.getElementById("pingJSText");
39+
pingJSTextOutput.innerText = "ping.js loaded from server";
40+
`)
41+
})
42+
43+
assertHome(t, tb, p, "/home", common.LifecycleEventNetworkIdle, func() {
44+
result := p.TextContent("#pingJSText", nil)
45+
assert.EqualValues(t, "ping.js loaded from server", result)
46+
})
47+
})
48+
49+
t.Run("doesn't unblock wait for networkIdle too early", func(t *testing.T) {
50+
t.Parallel()
51+
52+
tb := newTestBrowser(t, withFileServer())
53+
p := tb.NewPage(nil)
54+
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
55+
http.Redirect(w, r, tb.staticURL("prolonged_network_idle.html"), http.StatusMovedPermanently)
56+
})
57+
58+
var counter int64
59+
ch := make(chan bool)
60+
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
61+
<-ch
62+
63+
var counterMu sync.Mutex
64+
counterMu.Lock()
65+
defer counterMu.Unlock()
66+
67+
time.Sleep(time.Millisecond * 50)
68+
69+
counter++
70+
fmt.Fprintf(w, "pong %d", counter)
71+
})
72+
73+
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
74+
fmt.Fprintf(w, `
75+
var pingJSTextOutput = document.getElementById("pingJSText");
76+
pingJSTextOutput.innerText = "ping.js loaded from server";
77+
`)
78+
close(ch)
79+
})
80+
81+
assertHome(t, tb, p, "/home", common.LifecycleEventNetworkIdle, func() {
82+
result := p.TextContent("#pingRequestText", nil)
83+
assert.EqualValues(t, "Waiting... pong 4 - for loop complete", result)
84+
85+
result = p.TextContent("#pingJSText", nil)
86+
assert.EqualValues(t, "ping.js loaded from server", result)
87+
})
88+
})
89+
90+
t.Run("doesn't unblock wait on networkIdle early when load and domcontentloaded complete at once", func(t *testing.T) {
91+
t.Parallel()
92+
93+
tb := newTestBrowser(t, withFileServer())
94+
p := tb.NewPage(nil)
95+
tb.withHandler("/home", func(w http.ResponseWriter, r *http.Request) {
96+
http.Redirect(w, r, tb.staticURL("prolonged_network_idle_10.html"), http.StatusMovedPermanently)
97+
})
98+
99+
var counterMu sync.Mutex
100+
var counter int64
101+
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
102+
counterMu.Lock()
103+
defer counterMu.Unlock()
104+
105+
time.Sleep(time.Millisecond * 50)
106+
107+
counter++
108+
fmt.Fprintf(w, "pong %d", counter)
109+
})
110+
111+
assertHome(t, tb, p, "/home", common.LifecycleEventNetworkIdle, func() {
112+
result := p.TextContent("#pingRequestText", nil)
113+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
114+
})
115+
})
116+
}
117+
118+
func assertHome(t *testing.T, tb *testBrowser, p api.Page, gotoURL string, waitUntil common.LifecycleEvent, check func()) {
119+
t.Helper()
120+
121+
var resolved, rejected bool
122+
err := tb.await(func() error {
123+
opts := tb.toGojaValue(common.FrameGotoOptions{
124+
WaitUntil: waitUntil,
125+
Timeout: 30 * time.Second,
126+
})
127+
tb.promise(p.Goto(tb.URL(gotoURL), opts)).then(
128+
func() {
129+
check()
130+
resolved = true
131+
},
132+
func() {
133+
rejected = true
134+
},
135+
)
136+
137+
return nil
138+
})
139+
require.NoError(t, err)
140+
141+
assert.True(t, resolved)
142+
assert.False(t, rejected)
143+
}

tests/static/prolonged_network_idle.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
<head></head>
44

55
<body>
6-
<div id="prolongNetworkIdleLoad">Waiting...</div>
7-
<div id="serverMsg">Waiting...</div>
6+
<div id="pingRequestText">Waiting...</div>
7+
<div id="pingJSText">Waiting...</div>
88

99
<script>
10-
var prolongNetworkIdleLoadOutput = document.getElementById("prolongNetworkIdleLoad");
10+
var pingRequestTextOutput = document.getElementById("pingRequestText");
1111

12-
var p = prolongNetworkIdleLoad();
12+
var p = pingRequestText();
1313
p.then(() => {
14-
prolongNetworkIdleLoadOutput.innerText += ' - for loop complete';
14+
pingRequestTextOutput.innerText += ' - for loop complete';
1515
})
1616

17-
async function prolongNetworkIdleLoad() {
17+
async function pingRequestText() {
1818
for (var i = 0; i < 4; i++) {
1919
await fetch('/ping')
2020
.then(response => response.text())
2121
.then((data) => {
22-
prolongNetworkIdleLoadOutput.innerText = 'Waiting... ' + data;
22+
pingRequestTextOutput.innerText = 'Waiting... ' + data;
2323
});
2424
}
2525
}

tests/static/prolonged_network_idle_10.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
<head></head>
44

55
<body>
6-
<div id="prolongNetworkIdleLoad">Waiting...</div>
6+
<div id="pingRequestText">Waiting...</div>
77

88
<script>
9-
var prolongNetworkIdleLoadOutput = document.getElementById("prolongNetworkIdleLoad");
9+
var pingRequestTextOutput = document.getElementById("pingRequestText");
1010

11-
var p = prolongNetworkIdleLoad();
11+
var p = pingRequestText();
1212
p.then(() => {
13-
prolongNetworkIdleLoadOutput.innerText += ' - for loop complete';
13+
pingRequestTextOutput.innerText += ' - for loop complete';
1414
})
1515

16-
async function prolongNetworkIdleLoad() {
16+
async function pingRequestText() {
1717
for (var i = 0; i < 10; i++) {
1818
await fetch('/ping')
1919
.then(response => response.text())
2020
.then((data) => {
21-
prolongNetworkIdleLoadOutput.innerText = 'Waiting... ' + data;
21+
pingRequestTextOutput.innerText = 'Waiting... ' + data;
2222
});
2323
}
2424
}

0 commit comments

Comments
 (0)