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

Commit 9837fa5

Browse files
committed
Refactor WaitForLoadState lifecycle tests
Deduplicate them and convert them to a table test.
1 parent 038e596 commit 9837fa5

File tree

1 file changed

+113
-125
lines changed

1 file changed

+113
-125
lines changed

tests/lifecycle_wait_test.go

Lines changed: 113 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -33,140 +33,128 @@ import (
3333
// 2. The async scripts have loaded;
3434
// 3. All other network requests have completed;
3535

36-
func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
37-
// Test description
38-
//
39-
// 1. goto /home and wait for the load lifecycle event.
40-
// 2. use WaitForLoadState with load to ensure that load
41-
// lifecycle event has already fired.
42-
//
43-
// Success criteria: We don't wait for all network requests to
44-
// complete, but we are interested in waiting
45-
// for all async scripts to have fully loaded
46-
// (which is when load is fired). We also want
47-
// to ensure that the load event is stored
48-
// internally, and we don't block on WaitForLoadState.
36+
func TestLifecycleWaitForLoadState(t *testing.T) {
4937
t.Parallel()
5038

51-
tb := newTestBrowser(t, withFileServer())
52-
p := tb.NewPage(nil)
53-
54-
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
55-
withPingHandler(t, tb, time.Millisecond*100, nil)
56-
withPingJSHandler(t, tb, false, nil)
57-
58-
waitUntil := common.LifecycleEventLoad
59-
assertHome(t, tb, p, waitUntil, func() {
60-
result := p.TextContent("#pingRequestText", nil)
61-
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
62-
63-
result = p.TextContent("#pingJSText", nil)
64-
assert.EqualValues(t, "ping.js loaded from server", result)
65-
66-
// This shouldn't block and return after calling hasLifecycleEventFired.
67-
p.WaitForLoadState(waitUntil.String(), nil)
68-
})
69-
}
70-
71-
func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
72-
// Test description
73-
//
74-
// 1. goto /home and wait for the domcontentloaded lifecycle event.
75-
// 2. use WaitForLoadState with domcontentloaded to ensure that
76-
// domcontentloaded lifecycle event has already fired.
77-
//
78-
// Success criteria: We don't wait for all network requests or the
79-
// async scripts to complete, and we're only
80-
// interested in the html file being loaded. We
81-
// also want to ensure that the domcontentloaded
82-
// event is stored internally, and we don't block
83-
// on WaitForLoadState.
84-
85-
t.Parallel()
86-
87-
tb := newTestBrowser(t, withFileServer())
88-
p := tb.NewPage(nil)
89-
90-
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
91-
withPingHandler(t, tb, time.Millisecond*100, nil)
92-
withPingJSHandler(t, tb, true, nil)
93-
94-
waitUntil := common.LifecycleEventDOMContentLoad
95-
assertHome(t, tb, p, waitUntil, func() {
96-
result := p.TextContent("#pingRequestText", nil)
97-
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
98-
99-
result = p.TextContent("#pingJSText", nil)
100-
assert.EqualValues(t, "Waiting...", result)
101-
102-
// This shouldn't block and return after calling hasLifecycleEventFired.
103-
p.WaitForLoadState(waitUntil.String(), nil)
104-
})
105-
}
106-
107-
func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
108-
// Test description
109-
//
110-
// 1. goto /home and wait for the networkidle lifecycle event.
111-
// 2. use WaitForLoadState with networkidle to ensure that
112-
// networkidle lifecycle event has already fired.
113-
//
114-
// Success criteria: We wait for all network requests and async
115-
// scripts to complete. We also want to ensure
116-
// that the networkidle event is stored internally,
117-
// and we don't block on WaitForLoadState.
118-
119-
t.Parallel()
120-
121-
tb := newTestBrowser(t, withFileServer())
122-
p := tb.NewPage(nil)
123-
124-
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
125-
withPingHandler(t, tb, 0, nil)
126-
withPingJSHandler(t, tb, false, nil)
127-
128-
waitUntil := common.LifecycleEventNetworkIdle
129-
assertHome(t, tb, p, waitUntil, func() {
130-
result := p.TextContent("#pingRequestText", nil)
131-
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
132-
133-
result = p.TextContent("#pingJSText", nil)
134-
assert.EqualValues(t, "ping.js loaded from server", result)
135-
136-
// This shouldn't block and return after calling hasLifecycleEventFired.
137-
p.WaitForLoadState(waitUntil.String(), nil)
138-
})
139-
}
39+
tests := []struct {
40+
name string
41+
pingSlowness time.Duration
42+
pingJSSlow bool
43+
waitUntil common.LifecycleEvent
44+
pingRequestTextAssert func(result string)
45+
pingJSTextAssert func(result string)
46+
assertFunc func(p api.Page)
47+
}{
48+
{
49+
// Test description
50+
//
51+
// 1. goto /home and wait for the load lifecycle event.
52+
// 2. use WaitForLoadState with load.
53+
//
54+
// Success criteria: We want to ensure that the load event is persisted in
55+
// memory, and we don't block on WaitForLoadState.
56+
name: "load",
57+
pingSlowness: time.Millisecond * 100,
58+
pingJSSlow: false,
59+
waitUntil: common.LifecycleEventLoad,
60+
pingRequestTextAssert: func(result string) {
61+
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
62+
},
63+
pingJSTextAssert: func(result string) {
64+
assert.EqualValues(t, "ping.js loaded from server", result)
65+
},
66+
},
67+
{
68+
// Test description
69+
//
70+
// 1. goto /home and wait for the domcontentloaded lifecycle event.
71+
// 2. use WaitForLoadState with domcontentloaded.
72+
//
73+
// Success criteria: We want to ensure that the domcontentloaded event is
74+
// persisted in memory, and we don't block on WaitForLoadState.
75+
name: "domcontentloaded",
76+
pingSlowness: time.Millisecond * 100,
77+
pingJSSlow: true,
78+
waitUntil: common.LifecycleEventDOMContentLoad,
79+
pingRequestTextAssert: func(result string) {
80+
assert.NotEqualValues(t, "Waiting... pong 10 - for loop complete", result)
81+
},
82+
pingJSTextAssert: func(result string) {
83+
assert.EqualValues(t, "Waiting...", result)
84+
},
85+
},
86+
{
87+
// Test description
88+
//
89+
// 1. goto /home and wait for the networkidle lifecycle event.
90+
// 2. use WaitForLoadState with networkidle.
91+
//
92+
// Success criteria: We want to ensure that the networkidle event is
93+
// persisted in memory, and we don't block on WaitForLoadState.
94+
name: "networkidle",
95+
pingSlowness: 0,
96+
pingJSSlow: false,
97+
waitUntil: common.LifecycleEventNetworkIdle,
98+
pingRequestTextAssert: func(result string) {
99+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
100+
},
101+
pingJSTextAssert: func(result string) {
102+
assert.EqualValues(t, "ping.js loaded from server", result)
103+
},
104+
},
105+
{
106+
// Test description
107+
//
108+
// 1. goto /home and wait for the domcontentloaded lifecycle event.
109+
// 2. use WaitForLoadState with networkidle.
110+
//
111+
// Success criteria: We want to quickly move to calling WaitForLoadState
112+
// so that we wait until networkidle is received from
113+
// the browser. So not relying on the persisted state in memory.
114+
name: "domcontentloaded then networkidle",
115+
pingSlowness: time.Millisecond * 100,
116+
pingJSSlow: false,
117+
waitUntil: common.LifecycleEventDOMContentLoad,
118+
assertFunc: func(p api.Page) {
119+
p.WaitForLoadState(common.LifecycleEventNetworkIdle.String(), nil)
120+
121+
result := p.TextContent("#pingRequestText", nil)
122+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
123+
124+
result = p.TextContent("#pingJSText", nil)
125+
assert.EqualValues(t, "ping.js loaded from server", result)
126+
},
127+
},
128+
}
140129

141-
func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T) {
142-
// Test description
143-
//
144-
// 1. goto /home and wait for the domcontentloaded lifecycle event.
145-
// 2. use WaitForLoadState with networkidle to now wait for the
146-
// lifecycle event from the browser.
147-
//
148-
// Success criteria: We want to quickly move to calling WaitForLoadState
149-
// so that we block until a networkidle lifecycle
150-
// event is received from the browser.
130+
for _, tt := range tests {
131+
t.Run(tt.name, func(t *testing.T) {
132+
t.Parallel()
151133

152-
t.Parallel()
134+
tb := newTestBrowser(t, withFileServer())
135+
p := tb.NewPage(nil)
153136

154-
tb := newTestBrowser(t, withFileServer())
155-
p := tb.NewPage(nil)
137+
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
138+
withPingHandler(t, tb, tt.pingSlowness, nil)
139+
withPingJSHandler(t, tb, tt.pingJSSlow, nil)
156140

157-
withHomeHandler(t, tb, "wait_for_nav_lifecycle.html")
158-
withPingHandler(t, tb, time.Millisecond*100, nil)
159-
withPingJSHandler(t, tb, false, nil)
141+
if tt.assertFunc != nil {
142+
assertHome(t, tb, p, tt.waitUntil, func() { tt.assertFunc(p) })
143+
return
144+
}
160145

161-
assertHome(t, tb, p, common.LifecycleEventDOMContentLoad, func() {
162-
p.WaitForLoadState(common.LifecycleEventNetworkIdle.String(), nil)
146+
assertHome(t, tb, p, tt.waitUntil, func() {
147+
result := p.TextContent("#pingRequestText", nil)
148+
tt.pingRequestTextAssert(result)
163149

164-
result := p.TextContent("#pingRequestText", nil)
165-
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
150+
result = p.TextContent("#pingJSText", nil)
151+
tt.pingJSTextAssert(result)
166152

167-
result = p.TextContent("#pingJSText", nil)
168-
assert.EqualValues(t, "ping.js loaded from server", result)
169-
})
153+
// This shouldn't block and return after calling hasLifecycleEventFired.
154+
p.WaitForLoadState(tt.waitUntil.String(), nil)
155+
})
156+
})
157+
}
170158
}
171159

172160
func TestLifecycleReloadLoad(t *testing.T) {

0 commit comments

Comments
 (0)