refactor: move Call function

This commit is contained in:
lilong.129
2025-06-09 20:52:32 +08:00
parent f1544d4a5c
commit dd52faef57
5 changed files with 36 additions and 53 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2506091916
v5.0.0-beta-2506092052

View File

@@ -9,6 +9,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/uixt"
"github.com/httprunner/httprunner/v5/uixt/option"
)
// StepFunction implements IStep interface.
@@ -78,3 +79,36 @@ func runStepFunction(r *SessionRunner, step IStep) (stepResult *StepResult, err
stepResult.Success = true
return stepResult, nil
}
// Call custom function, used for pre/post action hook
func Call(desc string, fn func(), opts ...option.ActionOption) error {
actionOptions := option.NewActionOptions(opts...)
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)
}
}

View File

@@ -884,7 +884,7 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
// call custom function
if action.Method == option.ACTION_CallFunction {
if funcDesc, ok := action.Params.(string); ok {
err := uiDriver.Call(funcDesc, action.Fn, action.GetOptions()...)
err := Call(funcDesc, action.Fn, action.GetOptions()...)
if err != nil {
return stepResult, err
}

View File

@@ -8,7 +8,6 @@ import (
"testing"
"time"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -134,23 +133,6 @@ func TestDriver_ADB_TapXY(t *testing.T) {
assert.Nil(t, err)
}
func TestDriver_ADB_TapXY_WithHook(t *testing.T) {
driver := setupADBDriverExt(t)
err := driver.Call("pre hook", func() {
log.Info().Msg("pre hook")
}, option.WithTimeout(1))
assert.Nil(t, err)
err = driver.TapXY(0.4, 0.5)
assert.Nil(t, err)
err = driver.Call("post hook", func() {
log.Info().Msg("post hook")
}, option.WithTimeout(1))
assert.Nil(t, err)
}
func TestDriver_ADB_TapAbsXY(t *testing.T) {
driver := setupADBDriverExt(t)
err := driver.TapAbsXY(100, 300)

View File

@@ -13,39 +13,6 @@ import (
"github.com/rs/zerolog/log"
)
// Call custom function, used for pre/post action hook
func (dExt *XTDriver) Call(desc string, fn func(), opts ...option.ActionOption) error {
actionOptions := option.NewActionOptions(opts...)
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) (
x, y float64, err error) {