Skip to content

Commit

Permalink
refactor(headless): use getTimeParameter
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 committed Jan 31, 2025
1 parent 7e6bf4e commit ea34794
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions pkg/protocols/headless/engine/page_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,27 +802,21 @@ func (p *Page) WaitEvent(act *Action, out ActionData) (func() error, error) {
}

waitEvent = tmp
parsedMaxDuration := 10 * time.Second // 10 sec is default max wait duration for any event

// allow user to specify max-duration for wait-event
maxDuration, err := p.getActionArg(act, "max-duration")
maxDuration, err := getTimeParameter(p, act, "max-duration", 5, time.Second)
if err != nil {
return nil, err
}

if maxDuration != "" {
parsedMaxDuration, err = time.ParseDuration(maxDuration)
if err != nil {
return nil, errorutil.NewWithErr(err).Msgf("could not parse max-duration")
}
}

// Just wait the event to happen
waitFunc := func() (err error) {
// execute actual wait event
ctx, cancel := context.WithTimeoutCause(context.Background(), parsedMaxDuration, ErrActionExecDealine)
ctx, cancel := context.WithTimeoutCause(context.Background(), maxDuration, ErrActionExecDealine)
defer cancel()

err = contextutil.ExecFunc(ctx, p.page.WaitEvent(waitEvent))

return
}

Expand All @@ -831,21 +825,12 @@ func (p *Page) WaitEvent(act *Action, out ActionData) (func() error, error) {

// HandleDialog handles JavaScript dialog (alert, confirm, prompt, or onbeforeunload).
func (p *Page) HandleDialog(act *Action, out ActionData) error {
parsedMaxDuration := 10 * time.Second // default max wait duration for dialog

maxDuration, err := p.getActionArg(act, "max-duration")
maxDuration, err := getTimeParameter(p, act, "max-duration", 10, time.Second)
if err != nil {
return err
}

if maxDuration != "" {
parsedMaxDuration, err = time.ParseDuration(maxDuration)
if err != nil {
return errorutil.NewWithErr(err).Msgf("could not parse max-duration")
}
}

ctx, cancel := context.WithTimeout(context.Background(), parsedMaxDuration)
ctx, cancel := context.WithTimeout(context.Background(), maxDuration)
defer cancel()

wait, handle := p.page.HandleDialog()
Expand Down

0 comments on commit ea34794

Please sign in to comment.