1
1
package tests
2
2
3
3
import (
4
- "fmt"
5
- "net/http"
6
- "sync"
7
4
"testing"
8
- "time"
9
5
10
- "github.com/stretchr/testify/assert"
11
6
"github.com/stretchr/testify/require"
12
-
13
- "github.com/grafana/xk6-browser/api"
14
- "github.com/grafana/xk6-browser/common"
15
7
)
16
8
17
9
func TestFramePress (t * testing.T ) {
@@ -29,130 +21,3 @@ func TestFramePress(t *testing.T) {
29
21
30
22
require .Equal (t , "AbC" , f .InputValue ("#text1" , nil ))
31
23
}
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
- }
0 commit comments