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

Commit 215000d

Browse files
committed
Refactor timeout lifecycle test into table test
Resolves: #647 (comment)
1 parent 43956c6 commit 215000d

File tree

1 file changed

+49
-69
lines changed

1 file changed

+49
-69
lines changed

tests/lifecycle_wait_test.go

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
6666
waitUntil common.LifecycleEvent
6767
pingRequestTextAssert func(result string, pingCount int)
6868
pingJSTextAssert func(result string)
69-
assertFunc func(p api.Page)
69+
assertFunc func(tb *testBrowser, p api.Page) testPromise
70+
wantError string
7071
}{
7172
{
7273
name: "load",
@@ -104,6 +105,35 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
104105
assert.EqualValues(t, "ping.js loaded from server", result)
105106
},
106107
},
108+
{
109+
// Test description
110+
//
111+
// Steps:
112+
// 1. goto /home and wait for the specified lifecycle event.
113+
// 2. call WaitForNavigation without clicking on the link.
114+
// the specified lifecycle event.
115+
//
116+
// Success criteria:
117+
// We want this test to timeout since the navigation has
118+
// completed, a new one hasn't started but we "accidentally"
119+
// call WaitForNavigation.
120+
name: "timeout",
121+
pingSlowness: 0,
122+
pingJSSlow: false,
123+
waitUntil: common.LifecycleEventNetworkIdle,
124+
assertFunc: func(tb *testBrowser, p api.Page) testPromise {
125+
result := p.TextContent("#pingRequestText", nil)
126+
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
127+
128+
waitForNav := p.WaitForNavigation(tb.toGojaValue(&common.FrameWaitForNavigationOptions{
129+
Timeout: 1000,
130+
WaitUntil: common.LifecycleEventNetworkIdle,
131+
}))
132+
133+
return tb.promise(waitForNav)
134+
},
135+
wantError: "Uncaught (in promise) waiting for navigation: timed out after 1s",
136+
},
107137
}
108138

109139
for _, tt := range tests {
@@ -119,9 +149,8 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
119149

120150
if tt.assertFunc != nil {
121151
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
122-
tt.assertFunc(p)
123-
return testPromise{}
124-
}, nil)
152+
return tt.assertFunc(tb, p)
153+
}, nil, tt.wantError)
125154
return
126155
}
127156

@@ -145,66 +174,11 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
145174

146175
result = p.TextContent("#pingJSText", nil)
147176
tt.pingJSTextAssert(result)
148-
})
177+
}, "")
149178
})
150179
}
151180
}
152181

153-
func TestLifecycleWaitForNavigationTimeout(t *testing.T) {
154-
t.Parallel()
155-
156-
// Test description
157-
//
158-
// Steps:
159-
// 1. goto /home and wait for the networkidle lifecycle event.
160-
// 2. use WaitForNavigation with networkidle.
161-
//
162-
// Success criteria:
163-
// Time out reached after navigation completed and
164-
// wait for lifecycle event set, to signify that
165-
// WaitForNavigation must be set before we navigate
166-
// to a new page.
167-
168-
tb := newTestBrowser(t, withFileServer())
169-
p := tb.NewPage(nil)
170-
171-
withHomeHandler(t, tb, "lifecycle_no_ping_js.html")
172-
withPingHandler(t, tb, 0, nil)
173-
174-
waitUntil := common.LifecycleEventNetworkIdle
175-
var resolved, rejected bool
176-
err := tb.await(func() error {
177-
opts := tb.toGojaValue(common.FrameGotoOptions{
178-
WaitUntil: waitUntil,
179-
Timeout: 30 * time.Second,
180-
})
181-
prm := tb.promise(p.Goto(tb.URL("/home"), opts)).then(
182-
func() testPromise {
183-
result := p.TextContent("#pingRequestText", nil)
184-
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
185-
186-
waitForNav := p.WaitForNavigation(tb.toGojaValue(&common.FrameWaitForNavigationOptions{
187-
Timeout: 1000,
188-
WaitUntil: waitUntil,
189-
}))
190-
191-
return tb.promise(waitForNav)
192-
},
193-
)
194-
prm.then(func() {
195-
resolved = true
196-
}, func() {
197-
rejected = true
198-
})
199-
200-
return nil
201-
})
202-
require.NoError(t, err)
203-
204-
assert.False(t, resolved)
205-
assert.True(t, rejected)
206-
}
207-
208182
func TestLifecycleWaitForLoadState(t *testing.T) {
209183
t.Parallel()
210184

@@ -305,7 +279,7 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
305279
assertHome(t, tb, p, tt.waitUntil, func() testPromise {
306280
tt.assertFunc(p)
307281
return testPromise{}
308-
}, nil)
282+
}, nil, "")
309283
return
310284
}
311285

@@ -320,7 +294,7 @@ func TestLifecycleWaitForLoadState(t *testing.T) {
320294
p.WaitForLoadState(tt.waitUntil.String(), nil)
321295

322296
return testPromise{}
323-
}, nil)
297+
}, nil, "")
324298
})
325299
}
326300
}
@@ -415,7 +389,7 @@ func TestLifecycleReload(t *testing.T) {
415389
tt.pingJSTextAssert(result)
416390

417391
return testPromise{}
418-
}, nil)
392+
}, nil, "")
419393
})
420394
}
421395
}
@@ -499,7 +473,7 @@ func TestLifecycleGotoWithSubFrame(t *testing.T) {
499473
tt.pingJSTextAssert(result)
500474

501475
return testPromise{}
502-
}, nil)
476+
}, nil, "")
503477
})
504478
}
505479
}
@@ -569,7 +543,7 @@ func TestLifecycleGoto(t *testing.T) {
569543
tt.pingJSTextAssert(result)
570544

571545
return testPromise{}
572-
}, nil)
546+
}, nil, "")
573547
})
574548
}
575549
}
@@ -601,7 +575,7 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
601575
assert.EqualValues(t, "ping.js loaded from server", result)
602576

603577
return testPromise{}
604-
}, nil)
578+
}, nil, "")
605579
})
606580

607581
t.Run("doesn't unblock wait for networkIdle too early", func(t *testing.T) {
@@ -623,7 +597,7 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
623597
assert.EqualValues(t, "ping.js loaded from server", result)
624598

625599
return testPromise{}
626-
}, nil)
600+
}, nil, "")
627601
})
628602

629603
t.Run("doesn't unblock wait on networkIdle early when load and domcontentloaded complete at once", func(t *testing.T) {
@@ -640,7 +614,7 @@ func TestLifecycleGotoNetworkIdle(t *testing.T) {
640614
assert.EqualValues(t, "Waiting... pong 10 - for loop complete", result)
641615

642616
return testPromise{}
643-
}, nil)
617+
}, nil, "")
644618
})
645619
}
646620

@@ -713,7 +687,7 @@ func assertHome(
713687
t *testing.T,
714688
tb *testBrowser, p api.Page,
715689
waitUntil common.LifecycleEvent,
716-
check func() testPromise, secondCheck func(),
690+
check func() testPromise, secondCheck func(), wantError string,
717691
) {
718692
t.Helper()
719693

@@ -738,6 +712,12 @@ func assertHome(
738712

739713
return nil
740714
})
715+
716+
if wantError != "" {
717+
require.EqualError(t, err, wantError)
718+
return
719+
}
720+
741721
require.NoError(t, err)
742722

743723
assert.True(t, resolved)

0 commit comments

Comments
 (0)