@@ -94,8 +94,28 @@ func (p *Page) ExecuteActions(input *contextargs.Context, actions []*Action, var
94
94
err = p .TimeInputElement (act , outData )
95
95
case ActionSelectInput :
96
96
err = p .SelectInputElement (act , outData )
97
+ case ActionWaitDOM :
98
+ event := proto .PageLifecycleEventNameDOMContentLoaded
99
+ err = p .WaitPageLifecycleEvent (act , outData , event )
100
+ case ActionWaitFCP :
101
+ event := proto .PageLifecycleEventNameFirstContentfulPaint
102
+ err = p .WaitPageLifecycleEvent (act , outData , event )
103
+ case ActionWaitFMP :
104
+ event := proto .PageLifecycleEventNameFirstMeaningfulPaint
105
+ err = p .WaitPageLifecycleEvent (act , outData , event )
106
+ case ActionWaitIdle :
107
+ event := proto .PageLifecycleEventNameNetworkIdle
108
+ err = p .WaitPageLifecycleEvent (act , outData , event )
97
109
case ActionWaitLoad :
98
- err = p .WaitLoad (act , outData )
110
+ event := proto .PageLifecycleEventNameLoad
111
+ err = p .WaitPageLifecycleEvent (act , outData , event )
112
+ case ActionWaitStable :
113
+ err = p .WaitStable (act , outData )
114
+ // NOTE(dwisiswant0): Mapping `ActionWaitLoad` to `Page.WaitStable`,
115
+ // just in case waiting for the `proto.PageLifecycleEventNameLoad` event
116
+ // doesn't meet expectations.
117
+ // case ActionWaitLoad, ActionWaitStable:
118
+ // err = p.WaitStable(act, outData)
99
119
case ActionGetResource :
100
120
err = p .GetResource (act , outData )
101
121
case ActionExtract :
@@ -204,6 +224,17 @@ func createBackOffSleeper(pollTimeout, timeout time.Duration) utils.Sleeper {
204
224
}
205
225
}
206
226
227
+ func getNavigationFunc (p * Page , act * Action , event proto.PageLifecycleEventName ) (func (), error ) {
228
+ dur , err := getTimeout (p , act )
229
+ if err != nil {
230
+ return nil , errors .Wrap (err , "Wrong timeout given" )
231
+ }
232
+
233
+ fn := p .page .Timeout (dur ).WaitNavigation (event )
234
+
235
+ return fn , nil
236
+ }
237
+
207
238
func getTimeout (p * Page , act * Action ) (time.Duration , error ) {
208
239
return geTimeParameter (p , act , "timeout" , 3 , time .Second )
209
240
}
@@ -518,20 +549,38 @@ func (p *Page) SelectInputElement(act *Action, out ActionData) error {
518
549
return nil
519
550
}
520
551
521
- // WaitLoad waits for the page to load
522
- func (p * Page ) WaitLoad (act * Action , out ActionData ) error {
523
- p .page .Timeout (2 * time .Second ).WaitNavigation (proto .PageLifecycleEventNameFirstMeaningfulPaint )()
524
-
525
- // Wait for the window.onload event and also wait for the network requests
526
- // to become idle for a maximum duration of 3 seconds. If the requests
527
- // do not finish,
528
- if err := p .page .WaitLoad (); err != nil {
529
- return errors .Wrap (err , "could not wait load event" )
552
+ // WaitPageLifecycleEvent waits for specified page lifecycle event name
553
+ func (p * Page ) WaitPageLifecycleEvent (act * Action , out ActionData , event proto.PageLifecycleEventName ) error {
554
+ fn , err := getNavigationFunc (p , act , event )
555
+ if err != nil {
556
+ return err
530
557
}
531
- _ = p .page .WaitIdle (1 * time .Second )
558
+
559
+ fn ()
560
+
532
561
return nil
533
562
}
534
563
564
+ // WaitStable waits until the page is stable
565
+ func (p * Page ) WaitStable (act * Action , out ActionData ) error {
566
+ var dur time.Duration = time .Second // default stable page duration: 1s
567
+
568
+ timeout , err := getTimeout (p , act )
569
+ if err != nil {
570
+ return errors .Wrap (err , "Wrong timeout given" )
571
+ }
572
+
573
+ argDur := act .Data ["duration" ]
574
+ if argDur != "" {
575
+ dur , err = time .ParseDuration (argDur )
576
+ if err != nil {
577
+ dur = time .Second
578
+ }
579
+ }
580
+
581
+ return p .page .Timeout (timeout ).WaitStable (dur )
582
+ }
583
+
535
584
// GetResource gets a resource from an element from page.
536
585
func (p * Page ) GetResource (act * Action , out ActionData ) error {
537
586
element , err := p .pageElementBy (act .Data )
0 commit comments