feat: set timeout for Call function

This commit is contained in:
lilong.129
2025-05-12 08:48:54 +08:00
parent f6ad6c9eff
commit 7a6890a160
4 changed files with 35 additions and 12 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2505100950 v5.0.0-beta-2505120848
+2 -2
View File
@@ -449,12 +449,12 @@ func (s *StepMobile) ClosePopups(opts ...option.ActionOption) *StepMobile {
return s return s
} }
func (s *StepMobile) Call(name string, fn func()) *StepMobile { func (s *StepMobile) Call(name string, fn func(), opts ...option.ActionOption) *StepMobile {
s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{ s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{
Method: uixt.ACTION_CallFunction, Method: uixt.ACTION_CallFunction,
Params: name, // function description Params: name, // function description
Fn: fn, Fn: fn,
Options: nil, Options: option.NewActionOptions(opts...),
}) })
return s return s
} }
+1 -1
View File
@@ -336,7 +336,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
return dExt.ClosePopupsHandler() return dExt.ClosePopupsHandler()
case ACTION_CallFunction: case ACTION_CallFunction:
if funcDesc, ok := action.Params.(string); ok { if funcDesc, ok := action.Params.(string); ok {
return dExt.Call(funcDesc, action.Fn) return dExt.Call(funcDesc, action.Fn, action.GetOptions()...)
} }
return fmt.Errorf("invalid function description: %v", action.Params) return fmt.Errorf("invalid function description: %v", action.Params)
case ACTION_AIAction: case ACTION_AIAction:
+31 -8
View File
@@ -1,21 +1,44 @@
package uixt package uixt
import ( import (
"fmt"
"time" "time"
"github.com/httprunner/httprunner/v5/uixt/option" "github.com/httprunner/httprunner/v5/uixt/option"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
// Call custom function, used for pre/post hook for actions // Call custom function, used for pre/post action hook
func (dExt *XTDriver) Call(desc string, fn func()) error { func (dExt *XTDriver) Call(desc string, fn func(), opts ...option.ActionOption) error {
startTime := time.Now() actionOptions := option.NewActionOptions(opts...)
fn()
log.Info().Str("desc", desc).
Int64("duration(ms)", time.Since(startTime).Milliseconds()).
Msg("function called")
return nil startTime := time.Now()
defer func() {
log.Info().Str("desc", desc).
Int64("duration(ms)", time.Since(startTime).Milliseconds()).
Msg("function called")
}()
if actionOptions.Timeout == 0 {
// wait for function to finish
fn()
return nil
}
// set timeout for function execution
done := make(chan struct{})
go func() {
defer close(done)
fn()
}()
select {
case <-done:
// function completed within timeout
return nil
case <-time.After(time.Duration(actionOptions.Timeout) * time.Second):
return fmt.Errorf("function execution exceeded timeout of %d seconds", actionOptions.Timeout)
}
} }
func preHandler_TapAbsXY(driver IDriver, options *option.ActionOptions, rawX, rawY float64) ( func preHandler_TapAbsXY(driver IDriver, options *option.ActionOptions, rawX, rawY float64) (