fix: driver tests

This commit is contained in:
lilong.129
2025-02-18 15:33:54 +08:00
parent cb0f829fa2
commit c8f7e2fa70
4 changed files with 37 additions and 64 deletions

View File

@@ -140,16 +140,16 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
ACTION_SwipeToTapApp, action.Params)
case ACTION_SwipeToTapText:
if text, ok := action.Params.(string); ok {
return dExt.swipeToTapTexts([]string{text}, action.GetOptions()...)
return dExt.SwipeToTapTexts([]string{text}, action.GetOptions()...)
}
return fmt.Errorf("invalid %s params, should be app text(string), got %v",
ACTION_SwipeToTapText, action.Params)
case ACTION_SwipeToTapTexts:
if texts, ok := action.Params.([]string); ok {
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
return dExt.SwipeToTapTexts(texts, action.GetOptions()...)
}
if texts, err := builtin.ConvertToStringSlice(action.Params); err == nil {
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
return dExt.SwipeToTapTexts(texts, action.GetOptions()...)
}
return fmt.Errorf("invalid %s params: %v", ACTION_SwipeToTapTexts, action.Params)
case ACTION_AppTerminate:

View File

@@ -91,7 +91,7 @@ func prepareSwipeAction(dExt *XTDriver, params interface{}, opts ...option.Actio
}
}
func (dExt *XTDriver) swipeToTapTexts(texts []string, opts ...option.ActionOption) error {
func (dExt *XTDriver) SwipeToTapTexts(texts []string, opts ...option.ActionOption) error {
if len(texts) == 0 {
return errors.New("no text to tap")
}
@@ -152,12 +152,9 @@ func (dExt *XTDriver) SwipeToTapApp(appName string, opts ...option.ActionOption)
}
opts = append(opts, option.WithDirection("left"))
opts = append(opts, option.WithMaxRetryTimes(5))
actionOptions := option.NewActionOptions(opts...)
// default to retry 5 times
if actionOptions.MaxRetryTimes == 0 {
opts = append(opts, option.WithMaxRetryTimes(5))
}
// tap app icon above the text
if len(actionOptions.Offset) == 0 {
opts = append(opts, option.WithTapOffset(0, -25))
@@ -167,5 +164,5 @@ func (dExt *XTDriver) SwipeToTapApp(appName string, opts ...option.ActionOption)
opts = append(opts, option.WithInterval(1))
}
return dExt.swipeToTapTexts([]string{appName}, opts...)
return dExt.SwipeToTapTexts([]string{appName}, opts...)
}

View File

@@ -3,27 +3,29 @@
package uixt
import (
"path/filepath"
"regexp"
"testing"
"github.com/httprunner/httprunner/v5/internal/config"
"github.com/httprunner/httprunner/v5/pkg/uixt/ai"
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewDriver1(t *testing.T) {
device, _ := NewAndroidDevice(option.WithUIA2(true))
driver, _ := device.NewDriver()
func TestDriverExt_NewMethod1(t *testing.T) {
device, err := NewAndroidDevice(option.WithUIA2(true))
require.Nil(t, err)
driver, err := device.NewDriver()
require.Nil(t, err)
driverExt := NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt.TapByOCR("推荐")
}
func TestNewDriver2(t *testing.T) {
device, _ := NewAndroidDevice()
driver, _ := NewUIA2Driver(device)
func TestDriverExt_NewMethod2(t *testing.T) {
device, err := NewAndroidDevice()
require.Nil(t, err)
driver, err := NewUIA2Driver(device)
require.Nil(t, err)
driverExt := NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt.TapByOCR("推荐")
@@ -80,7 +82,13 @@ func setupDriverExt(t *testing.T) *XTDriver {
}
}
func TestAndroidSwipeAction(t *testing.T) {
func TestDriverExt_TapByOCR(t *testing.T) {
driver := setupDriverExt(t)
err := driver.TapByOCR("天气")
assert.Nil(t, err)
}
func TestDriverExt_prepareSwipeAction(t *testing.T) {
driver := setupDriverExt(t)
swipeAction := prepareSwipeAction(driver, "up", option.WithDirection("down"))
@@ -92,69 +100,37 @@ func TestAndroidSwipeAction(t *testing.T) {
assert.Nil(t, err)
}
func TestAndroidSwipeToTapApp(t *testing.T) {
func TestDriverExt_SwipeToTapApp(t *testing.T) {
driver := setupDriverExt(t)
err := driver.SwipeToTapApp("抖音")
assert.Nil(t, err)
}
func TestAndroidSwipeToTapTexts(t *testing.T) {
func TestDriverExt_SwipeToTapTexts(t *testing.T) {
driver := setupDriverExt(t)
err := driver.AppLaunch("com.ss.android.ugc.aweme")
assert.Nil(t, err)
err = driver.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, option.WithDirection("up"))
err = driver.SwipeToTapTexts(
[]string{"点击进入直播间", "直播中"},
option.WithDirection("up"),
option.WithMaxRetryTimes(10))
assert.Nil(t, err)
}
func TestGetScreenShot(t *testing.T) {
driver := setupADBDriverExt(t)
imagePath := filepath.Join(config.ScreenShotsPath, "test_screenshot")
_, err := driver.ScreenShot(option.WithScreenShotFileName(imagePath))
if err != nil {
t.Fatalf("GetScreenShot failed: %v", err)
}
t.Logf("screenshot saved at: %s", imagePath)
}
func TestCheckPopup(t *testing.T) {
func TestDriverExt_CheckPopup(t *testing.T) {
driver := setupADBDriverExt(t)
popup, err := driver.CheckPopup()
if err != nil {
t.Logf("check popup failed, err: %v", err)
} else if popup == nil {
require.Nil(t, err)
if popup == nil {
t.Log("no popup found")
} else {
t.Logf("found popup: %v", popup)
}
}
func TestClosePopup(t *testing.T) {
func TestDriverExt_ClosePopupsHandler(t *testing.T) {
driver := setupADBDriverExt(t)
if err := driver.ClosePopupsHandler(); err != nil {
t.Fatal(err)
}
}
func matchPopup(text string) bool {
for _, popup := range popups {
if regexp.MustCompile(popup[1]).MatchString(text) {
return true
}
}
return false
}
func TestMatchRegex(t *testing.T) {
testData := []string{
"以后再说", "我知道了", "同意", "拒绝", "稍后",
"始终允许", "继续使用", "仅在使用中允许",
}
for _, text := range testData {
if !matchPopup(text) {
t.Fatal(text)
}
}
err := driver.ClosePopupsHandler()
assert.Nil(t, err)
}