fix: swipe with params

This commit is contained in:
lilong.129
2025-05-14 14:36:46 +08:00
parent 6f428ce5cd
commit d145784910
6 changed files with 123 additions and 10 deletions

View File

@@ -151,9 +151,6 @@ func convertCompatMobileStep(mobileUI *MobileUI) {
if ma.Method == uixt.ACTION_SwipeToTapText && actionOptions.MaxRetryTimes == 0 {
ma.ActionOptions.MaxRetryTimes = 10
}
if ma.Method == uixt.ACTION_Swipe {
ma.ActionOptions.Direction = ma.Params
}
mobileUI.Actions[i] = ma
}
}

View File

@@ -195,12 +195,12 @@ func Interface2Float64(i interface{}) (float64, error) {
return float64(v), nil
case float64:
return v, nil
case string:
case string: // e.g. "1", "0.5"
floatVar, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
return floatVar, err
return floatVar, nil
}
// json.Number
value, ok := i.(builtinJSON.Number)

View File

@@ -0,0 +1,96 @@
package builtin
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInterface2Float64(t *testing.T) {
tests := []struct {
name string
input interface{}
want float64
wantErr bool
}{
{
name: "convert int",
input: 42,
want: 42.0,
wantErr: false,
},
{
name: "convert int32",
input: int32(42),
want: 42.0,
wantErr: false,
},
{
name: "convert int64",
input: int64(42),
want: 42.0,
wantErr: false,
},
{
name: "convert float32",
input: float32(42.5),
want: 42.5,
wantErr: false,
},
{
name: "convert float64",
input: 42.5,
want: 42.5,
wantErr: false,
},
{
name: "convert string valid number",
input: "42.5",
want: 42.5,
wantErr: false,
},
{
name: "convert string valid number",
input: "425",
want: 425.0,
wantErr: false,
},
{
name: "convert string invalid number",
input: "invalid",
want: 0,
wantErr: true,
},
{
name: "convert json.Number valid",
input: json.Number("42.5"),
want: 42.5,
wantErr: false,
},
{
name: "convert json.Number invalid",
input: json.Number("invalid"),
want: 0,
wantErr: true,
},
{
name: "convert unsupported type",
input: []int{1, 2, 3},
want: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Interface2Float64(tt.input)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}

View File

@@ -1 +1 @@
v5.0.0-beta-2505122246
v5.0.0-beta-2505141436

View File

@@ -306,6 +306,13 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
} else if sd, ok := action.Params.(SleepConfig); ok {
sleepStrict(sd.StartTime, int64(sd.Seconds*1000))
return nil
} else if param, ok := action.Params.(string); ok {
seconds, err := builtin.ConvertToFloat64(param)
if err != nil {
return errors.Wrapf(err, "invalid sleep params: %v(%T)", action.Params, action.Params)
}
time.Sleep(time.Duration(seconds*1000) * time.Millisecond)
return nil
}
return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params)
case ACTION_SleepMS:

View File

@@ -5,6 +5,7 @@ import (
"math/rand/v2"
"github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/rs/zerolog/log"
)
type ActionOptions struct {
@@ -72,10 +73,22 @@ func (o *ActionOptions) Options() []ActionOption {
case []interface{}:
// loaded from json case
// custom direction: [fromX, fromY, toX, toY]
sx, _ := builtin.Interface2Float64(v[0])
sy, _ := builtin.Interface2Float64(v[1])
ex, _ := builtin.Interface2Float64(v[2])
ey, _ := builtin.Interface2Float64(v[3])
sx, err := builtin.Interface2Float64(v[0])
if err != nil {
log.Error().Err(err).Interface("fromX", v[0]).Msg("convert float64 failed")
}
sy, err := builtin.Interface2Float64(v[1])
if err != nil {
log.Error().Err(err).Interface("fromY", v[1]).Msg("convert float64 failed")
}
ex, err := builtin.Interface2Float64(v[2])
if err != nil {
log.Error().Err(err).Interface("toX", v[2]).Msg("convert float64 failed")
}
ey, err := builtin.Interface2Float64(v[3])
if err != nil {
log.Error().Err(err).Interface("toY", v[3]).Msg("convert float64 failed")
}
options = append(options, WithCustomDirection(
sx, sy,
ex, ey,