mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 07:57:27 +08:00
feat: support pre hook and post hook for actions
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2505091122
|
v5.0.0-beta-2505092301
|
||||||
|
|||||||
@@ -307,11 +307,12 @@ func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
|||||||
|
|
||||||
func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||||
log.Info().Float64("x", x).Float64("y", y).Msg("ADBDriver.TapAbsXY")
|
log.Info().Float64("x", x).Float64("y", y).Msg("ADBDriver.TapAbsXY")
|
||||||
var err error
|
actionOptions := option.NewActionOptions(opts...)
|
||||||
x, y, err = handlerTapAbsXY(ad, x, y, opts...)
|
x, y, err := preHandler_TapAbsXY(ad, actionOptions, x, y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer postHandler(ad, actionOptions)
|
||||||
|
|
||||||
// adb shell input tap x y
|
// adb shell input tap x y
|
||||||
xStr := fmt.Sprintf("%.1f", x)
|
xStr := fmt.Sprintf("%.1f", x)
|
||||||
|
|||||||
@@ -298,14 +298,13 @@ func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
|||||||
func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||||
log.Info().Float64("x", x).Float64("y", y).Msg("UIA2Driver.TapAbsXY")
|
log.Info().Float64("x", x).Float64("y", y).Msg("UIA2Driver.TapAbsXY")
|
||||||
// register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap"))
|
// register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap"))
|
||||||
|
actionOptions := option.NewActionOptions(opts...)
|
||||||
var err error
|
x, y, err := preHandler_TapAbsXY(ud, actionOptions, x, y)
|
||||||
x, y, err = handlerTapAbsXY(ud, x, y, opts...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer postHandler(ud, actionOptions)
|
||||||
|
|
||||||
actionOptions := option.NewActionOptions(opts...)
|
|
||||||
duration := 100.0
|
duration := 100.0
|
||||||
if actionOptions.PressDuration > 0 {
|
if actionOptions.PressDuration > 0 {
|
||||||
duration = actionOptions.PressDuration * 1000 // convert to ms
|
duration = actionOptions.PressDuration * 1000 // convert to ms
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@@ -133,6 +134,33 @@ func TestDriver_ADB_TapXY(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDriver_ADB_TapXY_WithHook(t *testing.T) {
|
||||||
|
driver := setupADBDriverExt(t)
|
||||||
|
x, y := 0.4, 0.5
|
||||||
|
err := driver.TapXY(x, y,
|
||||||
|
option.WithHooks(
|
||||||
|
func() {
|
||||||
|
log.Info().Msg("pre hook")
|
||||||
|
x += 1
|
||||||
|
},
|
||||||
|
func() {
|
||||||
|
log.Info().Msg("post hook")
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
err = driver.TapXY(0.4, 0.5,
|
||||||
|
option.WithPreHook(func() {
|
||||||
|
log.Info().Msg("pre hook")
|
||||||
|
}),
|
||||||
|
option.WithPostHook(func() {
|
||||||
|
log.Info().Msg("post hook")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDriver_ADB_TapAbsXY(t *testing.T) {
|
func TestDriver_ADB_TapAbsXY(t *testing.T) {
|
||||||
driver := setupADBDriverExt(t)
|
driver := setupADBDriverExt(t)
|
||||||
err := driver.TapAbsXY(100, 300)
|
err := driver.TapAbsXY(100, 300)
|
||||||
|
|||||||
@@ -511,13 +511,13 @@ func (wd *BrowserDriver) Tap(x, y float64, options ...option.ActionOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wd *BrowserDriver) TapFloat(x, y float64, opts ...option.ActionOption) error {
|
func (wd *BrowserDriver) TapFloat(x, y float64, opts ...option.ActionOption) error {
|
||||||
var err error
|
actionOptions := option.NewActionOptions(opts...)
|
||||||
x, y, err = handlerTapAbsXY(wd, x, y, opts...)
|
x, y, err := preHandler_TapAbsXY(wd, actionOptions, x, y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer postHandler(wd, actionOptions)
|
||||||
|
|
||||||
actionOptions := option.NewActionOptions(opts...)
|
|
||||||
duration := 0.1
|
duration := 0.1
|
||||||
if actionOptions.Duration > 0 {
|
if actionOptions.Duration > 0 {
|
||||||
duration = actionOptions.Duration
|
duration = actionOptions.Duration
|
||||||
|
|||||||
+13
-4
@@ -5,14 +5,17 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handlerTapAbsXY(driver IDriver, rawX, rawY float64, opts ...option.ActionOption) (
|
func preHandler_TapAbsXY(driver IDriver, options *option.ActionOptions, rawX, rawY float64) (
|
||||||
x, y float64, err error) {
|
x, y float64, err error) {
|
||||||
|
|
||||||
actionOptions := option.NewActionOptions(opts...)
|
if options.PreHook != nil {
|
||||||
x, y = actionOptions.ApplyTapOffset(rawX, rawY)
|
options.PreHook()
|
||||||
|
}
|
||||||
|
|
||||||
|
x, y = options.ApplyTapOffset(rawX, rawY)
|
||||||
|
|
||||||
// mark UI operation
|
// mark UI operation
|
||||||
if actionOptions.MarkOperationEnabled {
|
if options.MarkOperationEnabled {
|
||||||
if markErr := MarkUIOperation(driver, ACTION_TapAbsXY, []float64{x, y}); markErr != nil {
|
if markErr := MarkUIOperation(driver, ACTION_TapAbsXY, []float64{x, y}); markErr != nil {
|
||||||
log.Warn().Err(markErr).Msg("Failed to mark tap operation")
|
log.Warn().Err(markErr).Msg("Failed to mark tap operation")
|
||||||
}
|
}
|
||||||
@@ -81,3 +84,9 @@ func handlerSwipe(driver IDriver, rawFomX, rawFromY, rawToX, rawToY float64, opt
|
|||||||
|
|
||||||
return fromX, fromY, toX, toY, nil
|
return fromX, fromY, toX, toY, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postHandler(_ IDriver, options *option.ActionOptions) {
|
||||||
|
if options.PostHook != nil {
|
||||||
|
options.PostHook()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -154,14 +154,13 @@ func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
|||||||
|
|
||||||
func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||||
log.Info().Float64("x", x).Float64("y", y).Msg("HDCDriver.TapAbsXY")
|
log.Info().Float64("x", x).Float64("y", y).Msg("HDCDriver.TapAbsXY")
|
||||||
|
actionOptions := option.NewActionOptions(opts...)
|
||||||
var err error
|
x, y, err := preHandler_TapAbsXY(hd, actionOptions, x, y)
|
||||||
x, y, err = handlerTapAbsXY(hd, x, y, opts...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer postHandler(hd, actionOptions)
|
||||||
|
|
||||||
actionOptions := option.NewActionOptions(opts...)
|
|
||||||
if actionOptions.Identifier != "" {
|
if actionOptions.Identifier != "" {
|
||||||
startTime := int(time.Now().UnixMilli())
|
startTime := int(time.Now().UnixMilli())
|
||||||
hd.points = append(hd.points, ExportPoint{Start: startTime, End: startTime + 100, Ext: actionOptions.Identifier, RunTime: 100})
|
hd.points = append(hd.points, ExportPoint{Start: startTime, End: startTime + 100, Ext: actionOptions.Identifier, RunTime: 100})
|
||||||
|
|||||||
@@ -597,11 +597,12 @@ func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
|||||||
x = wd.toScale(x)
|
x = wd.toScale(x)
|
||||||
y = wd.toScale(y)
|
y = wd.toScale(y)
|
||||||
|
|
||||||
var err error
|
actionOptions := option.NewActionOptions(opts...)
|
||||||
x, y, err = handlerTapAbsXY(wd, x, y, opts...)
|
x, y, err := preHandler_TapAbsXY(wd, actionOptions, x, y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer postHandler(wd, actionOptions)
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"x": x,
|
"x": x,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type ActionOptions struct {
|
|||||||
Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"`
|
Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"`
|
||||||
|
|
||||||
ScreenOptions
|
ScreenOptions
|
||||||
|
HookOptions
|
||||||
|
|
||||||
// set custiom options such as textview, id, description
|
// set custiom options such as textview, id, description
|
||||||
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package option
|
||||||
|
|
||||||
|
// HookOptions contains options for action hooks
|
||||||
|
type HookOptions struct {
|
||||||
|
// pre hook before action
|
||||||
|
PreHook func()
|
||||||
|
// post hook after action
|
||||||
|
PostHook func()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPreHook sets the pre hook before action
|
||||||
|
func WithPreHook(preHook func()) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.PreHook = preHook
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPostHook sets the post hook after action
|
||||||
|
func WithPostHook(postHook func()) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.PostHook = postHook
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithHooks sets the pre hook and post hook
|
||||||
|
func WithHooks(preHook func(), postHook func()) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.PreHook = preHook
|
||||||
|
o.PostHook = postHook
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user