@@ -14,6 +14,111 @@ import (
14
14
"github.com/grafana/xk6-browser/common"
15
15
)
16
16
17
+ func TestLifecycleWaitForNavigation (t * testing.T ) {
18
+ // Test description
19
+ //
20
+ // 1. goto /home and wait for the specified lifecycle event.
21
+ // 2. click on a link that navigates to a page, and wait on
22
+ // the specified lifecycle event.
23
+ //
24
+ // Success criteria: The click will perform a navigation away
25
+ // from the current page, it should wait for
26
+ // the specified lifecycle event and the result
27
+ // of the page should match the original nav.
28
+
29
+ t .Parallel ()
30
+
31
+ tests := []struct {
32
+ name string
33
+ pingSlowness time.Duration
34
+ pingJSSlow bool
35
+ waitUntil common.LifecycleEvent
36
+ pingRequestTextAssert func (result string , pingCount int )
37
+ pingJSTextAssert func (result string )
38
+ assertFunc func (p api.Page )
39
+ }{
40
+ {
41
+ name : "load" ,
42
+ pingSlowness : time .Millisecond * 100 ,
43
+ pingJSSlow : false ,
44
+ waitUntil : common .LifecycleEventLoad ,
45
+ pingRequestTextAssert : func (result string , pingCount int ) {
46
+ assert .NotEqualValues (t , fmt .Sprintf ("Waiting... pong %d - for loop complete" , pingCount ), result )
47
+ },
48
+ pingJSTextAssert : func (result string ) {
49
+ assert .EqualValues (t , "ping.js loaded from server" , result )
50
+ },
51
+ },
52
+ {
53
+ name : "domcontentloaded" ,
54
+ pingSlowness : time .Millisecond * 100 ,
55
+ pingJSSlow : true ,
56
+ waitUntil : common .LifecycleEventDOMContentLoad ,
57
+ pingRequestTextAssert : func (result string , pingCount int ) {
58
+ assert .NotEqualValues (t , fmt .Sprintf ("Waiting... pong %d - for loop complete" , pingCount ), result )
59
+ },
60
+ pingJSTextAssert : func (result string ) {
61
+ assert .EqualValues (t , "Waiting..." , result )
62
+ },
63
+ },
64
+ {
65
+ name : "networkidle" ,
66
+ pingSlowness : 0 ,
67
+ pingJSSlow : false ,
68
+ waitUntil : common .LifecycleEventNetworkIdle ,
69
+ pingRequestTextAssert : func (result string , pingCount int ) {
70
+ assert .EqualValues (t , fmt .Sprintf ("Waiting... pong %d - for loop complete" , pingCount ), result )
71
+ },
72
+ pingJSTextAssert : func (result string ) {
73
+ assert .EqualValues (t , "ping.js loaded from server" , result )
74
+ },
75
+ },
76
+ }
77
+
78
+ for _ , tt := range tests {
79
+ t .Run (tt .name , func (t * testing.T ) {
80
+ t .Parallel ()
81
+
82
+ tb := newTestBrowser (t , withFileServer ())
83
+ p := tb .NewPage (nil )
84
+
85
+ withHomeHandler (t , tb , "wait_for_nav_lifecycle.html" )
86
+ withPingHandler (t , tb , tt .pingSlowness , nil )
87
+ withPingJSHandler (t , tb , tt .pingJSSlow , nil )
88
+
89
+ if tt .assertFunc != nil {
90
+ assertHome (t , tb , p , tt .waitUntil , func () testPromise {
91
+ tt .assertFunc (p )
92
+ return testPromise {}
93
+ }, nil )
94
+ return
95
+ }
96
+
97
+ assertHome (t , tb , p , tt .waitUntil , func () testPromise {
98
+ result := p .TextContent ("#pingRequestText" , nil )
99
+ tt .pingRequestTextAssert (result , 10 )
100
+
101
+ result = p .TextContent ("#pingJSText" , nil )
102
+ tt .pingJSTextAssert (result )
103
+
104
+ waitForNav := p .WaitForNavigation (tb .toGojaValue (& common.FrameWaitForNavigationOptions {
105
+ Timeout : 30000 ,
106
+ WaitUntil : tt .waitUntil ,
107
+ }))
108
+ click := p .Click ("#homeLink" , nil )
109
+
110
+ return tb .promiseAll (waitForNav , click )
111
+ }, func () {
112
+ result := p .TextContent ("#pingRequestText" , nil )
113
+ tt .pingRequestTextAssert (result , 20 )
114
+
115
+ result = p .TextContent ("#pingJSText" , nil )
116
+ tt .pingJSTextAssert (result )
117
+ })
118
+ })
119
+ }
120
+ }
121
+
17
122
// General guidelines on lifecycle events:
18
123
//
19
124
// - load: This lifecycle event is emitted by the browser once:
0 commit comments