From 7e6dd8e3443de20f4e1a32c2c243b95a3520f9fb Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 12 Feb 2025 15:33:21 +0800 Subject: [PATCH] refactor: merge tests --- internal/version/VERSION | 2 +- pkg/uixt/driver_action_test.go | 48 ---------- pkg/uixt/driver_popups_test.go | 49 ---------- pkg/uixt/driver_screenshot_test.go | 23 ----- pkg/uixt/driver_swipe_test.go | 38 -------- pkg/uixt/driver_tap_test.go | 31 ------ pkg/uixt/driver_test.go | 148 +++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+), 190 deletions(-) delete mode 100644 pkg/uixt/driver_action_test.go delete mode 100644 pkg/uixt/driver_popups_test.go delete mode 100644 pkg/uixt/driver_screenshot_test.go delete mode 100644 pkg/uixt/driver_swipe_test.go delete mode 100644 pkg/uixt/driver_tap_test.go diff --git a/internal/version/VERSION b/internal/version/VERSION index 0893bb68..b488f58b 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2502121525 +v5.0.0+2502121533 diff --git a/pkg/uixt/driver_action_test.go b/pkg/uixt/driver_action_test.go deleted file mode 100644 index ccba486a..00000000 --- a/pkg/uixt/driver_action_test.go +++ /dev/null @@ -1,48 +0,0 @@ -//go:build localtest - -package uixt - -import ( - "testing" - "time" -) - -func checkErr(t *testing.T, err error, msg ...string) { - if err != nil { - if len(msg) == 0 { - t.Fatal(err) - } else { - t.Fatal(msg, err) - } - } -} - -func TestGetSimulationDuration(t *testing.T) { - params := []float64{1.23} - duration := getSimulationDuration(params) - if duration != 1230 { - t.Fatal("getSimulationDuration failed") - } - - params = []float64{1, 2} - duration = getSimulationDuration(params) - if duration < 1000 || duration > 2000 { - t.Fatal("getSimulationDuration failed") - } - - params = []float64{1, 5, 0.7, 5, 10, 0.3} - duration = getSimulationDuration(params) - if duration < 1000 || duration > 10000 { - t.Fatal("getSimulationDuration failed") - } -} - -func TestSleepStrict(t *testing.T) { - startTime := time.Now() - sleepStrict(startTime, 1230) - dur := time.Since(startTime).Milliseconds() - t.Log(dur) - if dur < 1230 || dur > 1300 { - t.Fatalf("sleepRandom failed, dur: %d", dur) - } -} diff --git a/pkg/uixt/driver_popups_test.go b/pkg/uixt/driver_popups_test.go deleted file mode 100644 index 57c1e4b5..00000000 --- a/pkg/uixt/driver_popups_test.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build localtest - -package uixt - -import ( - "regexp" - "testing" -) - -func TestCheckPopup(t *testing.T) { - setupAndroidAdbDriver(t) - popup, err := driverExt.CheckPopup() - if err != nil { - t.Logf("check popup failed, err: %v", err) - } else if popup == nil { - t.Log("no popup found") - } else { - t.Logf("found popup: %v", popup) - } -} - -func TestClosePopup(t *testing.T) { - setupAndroidAdbDriver(t) - - if err := driverExt.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) - } - } -} diff --git a/pkg/uixt/driver_screenshot_test.go b/pkg/uixt/driver_screenshot_test.go deleted file mode 100644 index 8bf776d4..00000000 --- a/pkg/uixt/driver_screenshot_test.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build localtest - -package uixt - -import ( - "path/filepath" - "testing" - - "github.com/httprunner/httprunner/v5/internal/config" - "github.com/httprunner/httprunner/v5/pkg/uixt/option" -) - -func TestGetScreenShot(t *testing.T) { - setupAndroidAdbDriver(t) - - imagePath := filepath.Join(config.ScreenShotsPath, "test_screenshot") - _, err := driverExt.ScreenShot(option.WithScreenShotFileName(imagePath)) - if err != nil { - t.Fatalf("GetScreenShot failed: %v", err) - } - - t.Logf("screenshot saved at: %s", imagePath) -} diff --git a/pkg/uixt/driver_swipe_test.go b/pkg/uixt/driver_swipe_test.go deleted file mode 100644 index 97529832..00000000 --- a/pkg/uixt/driver_swipe_test.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:build localtest - -package uixt - -import ( - "testing" - - "github.com/httprunner/httprunner/v5/pkg/uixt/option" -) - -func TestAndroidSwipeAction(t *testing.T) { - setupAndroidAdbDriver(t) - - swipeAction := prepareSwipeAction(driverExt, "up", option.WithDirection("down")) - err := swipeAction(driverExt) - checkErr(t, err) - - swipeAction = prepareSwipeAction(driverExt, "up", option.WithCustomDirection(0.5, 0.5, 0.5, 0.9)) - err = swipeAction(driverExt) - checkErr(t, err) -} - -func TestAndroidSwipeToTapApp(t *testing.T) { - setupAndroidAdbDriver(t) - - err := driverExt.SwipeToTapApp("抖音") - checkErr(t, err) -} - -func TestAndroidSwipeToTapTexts(t *testing.T) { - setupAndroidAdbDriver(t) - - err := driverExt.AppLaunch("com.ss.android.ugc.aweme") - checkErr(t, err) - - err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, option.WithDirection("up")) - checkErr(t, err) -} diff --git a/pkg/uixt/driver_tap_test.go b/pkg/uixt/driver_tap_test.go deleted file mode 100644 index abca47c8..00000000 --- a/pkg/uixt/driver_tap_test.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build localtest - -package uixt - -import ( - "testing" - - "github.com/httprunner/httprunner/v5/pkg/uixt/ai" -) - -var ( - iosDevice *IOSDevice - iosDriverExt *XTDriver -) - -func init() { - iosDevice, _ = NewIOSDevice() - driver, _ := iosDevice.NewDriver() - iosDriverExt = NewXTDriver(driver, - ai.WithCVService(ai.CVServiceTypeVEDEM)) -} - -func TestDriverExt_TapXY(t *testing.T) { - err := iosDriverExt.TapXY(0.4, 0.5) - checkErr(t, err) -} - -func TestDriverExt_TapAbsXY(t *testing.T) { - err := iosDriverExt.TapAbsXY(100, 300) - checkErr(t, err) -} diff --git a/pkg/uixt/driver_test.go b/pkg/uixt/driver_test.go index 7fb63889..57225bd0 100644 --- a/pkg/uixt/driver_test.go +++ b/pkg/uixt/driver_test.go @@ -3,8 +3,12 @@ package uixt import ( + "path/filepath" + "regexp" "testing" + "time" + "github.com/httprunner/httprunner/v5/internal/config" "github.com/httprunner/httprunner/v5/pkg/uixt/ai" "github.com/httprunner/httprunner/v5/pkg/uixt/option" ) @@ -58,3 +62,147 @@ func TestDriverExt(t *testing.T) { androidDevice := driver.GetDevice().(*AndroidDevice) androidDevice.InstallAPK("/path/to/app.apk") } + +var ( + iosDevice *IOSDevice + iosDriverExt *XTDriver +) + +func init() { + iosDevice, _ = NewIOSDevice() + driver, _ := iosDevice.NewDriver() + iosDriverExt = NewXTDriver(driver, + ai.WithCVService(ai.CVServiceTypeVEDEM)) +} + +func TestDriverExt_TapXY(t *testing.T) { + err := iosDriverExt.TapXY(0.4, 0.5) + checkErr(t, err) +} + +func TestDriverExt_TapAbsXY(t *testing.T) { + err := iosDriverExt.TapAbsXY(100, 300) + checkErr(t, err) +} + +func TestAndroidSwipeAction(t *testing.T) { + setupAndroidAdbDriver(t) + + swipeAction := prepareSwipeAction(driverExt, "up", option.WithDirection("down")) + err := swipeAction(driverExt) + checkErr(t, err) + + swipeAction = prepareSwipeAction(driverExt, "up", option.WithCustomDirection(0.5, 0.5, 0.5, 0.9)) + err = swipeAction(driverExt) + checkErr(t, err) +} + +func TestAndroidSwipeToTapApp(t *testing.T) { + setupAndroidAdbDriver(t) + + err := driverExt.SwipeToTapApp("抖音") + checkErr(t, err) +} + +func TestAndroidSwipeToTapTexts(t *testing.T) { + setupAndroidAdbDriver(t) + + err := driverExt.AppLaunch("com.ss.android.ugc.aweme") + checkErr(t, err) + + err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, option.WithDirection("up")) + checkErr(t, err) +} + +func checkErr(t *testing.T, err error, msg ...string) { + if err != nil { + if len(msg) == 0 { + t.Fatal(err) + } else { + t.Fatal(msg, err) + } + } +} + +func TestGetSimulationDuration(t *testing.T) { + params := []float64{1.23} + duration := getSimulationDuration(params) + if duration != 1230 { + t.Fatal("getSimulationDuration failed") + } + + params = []float64{1, 2} + duration = getSimulationDuration(params) + if duration < 1000 || duration > 2000 { + t.Fatal("getSimulationDuration failed") + } + + params = []float64{1, 5, 0.7, 5, 10, 0.3} + duration = getSimulationDuration(params) + if duration < 1000 || duration > 10000 { + t.Fatal("getSimulationDuration failed") + } +} + +func TestSleepStrict(t *testing.T) { + startTime := time.Now() + sleepStrict(startTime, 1230) + dur := time.Since(startTime).Milliseconds() + t.Log(dur) + if dur < 1230 || dur > 1300 { + t.Fatalf("sleepRandom failed, dur: %d", dur) + } +} + +func TestGetScreenShot(t *testing.T) { + setupAndroidAdbDriver(t) + + imagePath := filepath.Join(config.ScreenShotsPath, "test_screenshot") + _, err := driverExt.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) { + setupAndroidAdbDriver(t) + popup, err := driverExt.CheckPopup() + if err != nil { + t.Logf("check popup failed, err: %v", err) + } else if popup == nil { + t.Log("no popup found") + } else { + t.Logf("found popup: %v", popup) + } +} + +func TestClosePopup(t *testing.T) { + setupAndroidAdbDriver(t) + + if err := driverExt.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) + } + } +}