refactor: merge tests

This commit is contained in:
lilong.129
2025-02-12 15:33:21 +08:00
parent db23459250
commit 7e6dd8e344
7 changed files with 149 additions and 190 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}
}
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}
}
}