mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
refactor: IDriver
This commit is contained in:
@@ -163,12 +163,8 @@ func (ad *ADBDriver) WindowSize() (size types.Size, err error) {
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Scale() (scale float64, err error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// PressBack simulates a short press on the BACK button.
|
||||
func (ad *ADBDriver) PressBack(opts ...option.ActionOption) (err error) {
|
||||
// Back simulates a short press on the BACK button.
|
||||
func (ad *ADBDriver) Back() (err error) {
|
||||
// adb shell input keyevent 4
|
||||
_, err = ad.runShellCommand("input", "keyevent", fmt.Sprintf("%d", KCBack))
|
||||
if err != nil {
|
||||
@@ -195,18 +191,18 @@ func (ad *ADBDriver) Orientation() (orientation types.Orientation, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Homescreen() (err error) {
|
||||
return ad.PressKeyCodes(KCHome, KMEmpty)
|
||||
func (ad *ADBDriver) Home() (err error) {
|
||||
return ad.PressKeyCode(KCHome, KMEmpty)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Unlock() (err error) {
|
||||
// Notice: brighten should be executed before unlock
|
||||
// brighten android device screen
|
||||
if err := ad.PressKeyCodes(KCWakeup, KMEmpty); err != nil {
|
||||
if err := ad.PressKeyCode(KCWakeup, KMEmpty); err != nil {
|
||||
log.Error().Err(err).Msg("brighten android device screen failed")
|
||||
}
|
||||
// unlock android device screen
|
||||
if err := ad.PressKeyCodes(KCMenu, KMEmpty); err != nil {
|
||||
if err := ad.PressKeyCode(KCMenu, KMEmpty); err != nil {
|
||||
log.Error().Err(err).Msg("press menu key to unlock screen failed")
|
||||
}
|
||||
|
||||
@@ -219,7 +215,7 @@ func (ad *ADBDriver) Backspace(count int, opts ...option.ActionOption) (err erro
|
||||
return nil
|
||||
}
|
||||
if count == 1 {
|
||||
return ad.PressKeyCode(KCDel)
|
||||
return ad.PressKeyCode(KCDel, KMEmpty)
|
||||
}
|
||||
keyArray := make([]KeyCode, count)
|
||||
|
||||
@@ -231,7 +227,7 @@ func (ad *ADBDriver) Backspace(count int, opts ...option.ActionOption) (err erro
|
||||
|
||||
func (ad *ADBDriver) combinationKey(keyCodes []KeyCode) (err error) {
|
||||
if len(keyCodes) == 1 {
|
||||
return ad.PressKeyCode(keyCodes[0])
|
||||
return ad.PressKeyCode(keyCodes[0], KMEmpty)
|
||||
}
|
||||
strKeyCodes := make([]string, len(keyCodes))
|
||||
for i, keycode := range keyCodes {
|
||||
@@ -242,11 +238,7 @@ func (ad *ADBDriver) combinationKey(keyCodes []KeyCode) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) PressKeyCode(keyCode KeyCode) (err error) {
|
||||
return ad.PressKeyCodes(keyCode, KMEmpty)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta) (err error) {
|
||||
func (ad *ADBDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta) (err error) {
|
||||
// adb shell input keyevent [--longpress] KEYCODE [METASTATE]
|
||||
if metaState != KMEmpty {
|
||||
// press key with metastate, e.g. KMShiftOn/KMCtrlOn
|
||||
@@ -410,18 +402,13 @@ func (ad *ADBDriver) ForceTouchFloat(x, y, pressure float64, second ...float64)
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) SendKeys(text string, opts ...option.ActionOption) (err error) {
|
||||
err = ad.SendUnicodeKeys(text, opts...)
|
||||
func (ad *ADBDriver) Input(text string, opts ...option.ActionOption) error {
|
||||
err := ad.SendUnicodeKeys(text, opts...)
|
||||
if err == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
err = ad.InputText(text, opts...)
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) InputText(text string, opts ...option.ActionOption) error {
|
||||
// adb shell input text <text>
|
||||
_, err := ad.runShellCommand("input", "text", text)
|
||||
_, err = ad.runShellCommand("input", "text", text)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "send keys failed")
|
||||
}
|
||||
@@ -454,7 +441,7 @@ func (ad *ADBDriver) SendUnicodeKeys(text string, opts ...option.ActionOption) (
|
||||
log.Warn().Err(err).Msgf("encode text with modified utf7 failed")
|
||||
return
|
||||
}
|
||||
err = ad.InputText("\""+strings.ReplaceAll(encodedStr, "\"", "\\\"")+"\"", opts...)
|
||||
err = ad.Input("\""+strings.ReplaceAll(encodedStr, "\"", "\\\"")+"\"", opts...)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -515,10 +502,6 @@ func (ad *ADBDriver) SendKeysByAdbKeyBoard(text string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Input(text string, opts ...option.ActionOption) (err error) {
|
||||
return ad.SendKeys(text, opts...)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) AppClear(packageName string) error {
|
||||
if _, err := ad.runShellCommand("pm", "clear", packageName); err != nil {
|
||||
log.Error().Str("packageName", packageName).Err(err).Msg("failed to clear package cache")
|
||||
@@ -528,11 +511,6 @@ func (ad *ADBDriver) AppClear(packageName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) PressButton(devBtn types.DeviceButton) (err error) {
|
||||
err = types.ErrDriverNotImplemented
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Rotation() (rotation types.Rotation, err error) {
|
||||
err = types.ErrDriverNotImplemented
|
||||
return
|
||||
@@ -729,7 +707,7 @@ func (ad *ADBDriver) GetSession() *Session {
|
||||
return ad.Session
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) GetForegroundApp() (app types.AppInfo, err error) {
|
||||
func (ad *ADBDriver) ForegroundInfo() (app types.AppInfo, err error) {
|
||||
packageInfo, err := ad.runShellCommand(
|
||||
"CLASSPATH=/data/local/tmp/evalite", "app_process", "/",
|
||||
"com.bytedance.iesqa.eval_process.PackageService", "2>/dev/null")
|
||||
@@ -768,14 +746,14 @@ func (ad *ADBDriver) SetIme(imeRegx string) error {
|
||||
time.Sleep(1 * time.Second)
|
||||
pid, _ := ad.runShellCommand("pidof", packageName)
|
||||
if strings.TrimSpace(pid) == "" {
|
||||
appInfo, err := ad.GetForegroundApp()
|
||||
appInfo, err := ad.ForegroundInfo()
|
||||
_ = ad.AppLaunch(packageName)
|
||||
if err == nil && packageName != option.UnicodeImePackageName {
|
||||
time.Sleep(10 * time.Second)
|
||||
nextAppInfo, err := ad.GetForegroundApp()
|
||||
nextAppInfo, err := ad.ForegroundInfo()
|
||||
log.Info().Str("beforeFocusedPackage", appInfo.PackageName).Str("afterFocusedPackage", nextAppInfo.PackageName).Msg("")
|
||||
if err == nil && nextAppInfo.PackageName != appInfo.PackageName {
|
||||
_ = ad.PressKeyCodes(KCBack, KMEmpty)
|
||||
_ = ad.PressKeyCode(KCBack, KMEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -799,102 +777,6 @@ func (ad *ADBDriver) GetIme() (ime string, err error) {
|
||||
return currentIme, nil
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) AssertForegroundApp(packageName string, activityType ...string) error {
|
||||
log.Debug().Str("package_name", packageName).
|
||||
Strs("activity_type", activityType).
|
||||
Msg("assert android foreground package and activity")
|
||||
|
||||
app, err := ad.GetForegroundApp()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("get foreground app failed, skip app/activity assertion")
|
||||
return nil // Notice: ignore error when get foreground app failed
|
||||
}
|
||||
|
||||
// assert package
|
||||
if app.PackageName != packageName {
|
||||
log.Error().
|
||||
Interface("foreground_app", app.AppBaseInfo).
|
||||
Str("expected_package", packageName).
|
||||
Msg("assert package failed")
|
||||
return errors.Wrap(code.MobileUIAssertForegroundAppError,
|
||||
"assert foreground package failed")
|
||||
}
|
||||
|
||||
if len(activityType) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// assert activity
|
||||
expectActivityType := activityType[0]
|
||||
activities, ok := androidActivities[packageName]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("activities not configured for package %s", packageName)
|
||||
log.Error().Msg(msg)
|
||||
return errors.Wrap(code.MobileUIAssertForegroundActivityError, msg)
|
||||
}
|
||||
|
||||
expectActivities, ok := activities[expectActivityType]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("activity type %s not configured for package %s",
|
||||
expectActivityType, packageName)
|
||||
log.Error().Msg(msg)
|
||||
return errors.Wrap(code.MobileUIAssertForegroundActivityError, msg)
|
||||
}
|
||||
|
||||
// assertion
|
||||
for _, expectActivity := range expectActivities {
|
||||
if strings.HasSuffix(app.Activity, expectActivity) {
|
||||
// assert activity success
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// assert activity failed
|
||||
log.Error().
|
||||
Interface("foreground_app", app.AppBaseInfo).
|
||||
Str("expected_activity_type", expectActivityType).
|
||||
Strs("expected_activities", expectActivities).
|
||||
Msg("assert activity failed")
|
||||
return errors.Wrap(code.MobileUIAssertForegroundActivityError,
|
||||
"assert foreground activity failed")
|
||||
}
|
||||
|
||||
var androidActivities = map[string]map[string][]string{
|
||||
// DY
|
||||
"com.ss.android.ugc.aweme": {
|
||||
"feed": []string{".splash.SplashActivity"},
|
||||
"live": []string{".live.LivePlayActivity"},
|
||||
},
|
||||
// DY lite
|
||||
"com.ss.android.ugc.aweme.lite": {
|
||||
"feed": []string{".splash.SplashActivity"},
|
||||
"live": []string{".live.LivePlayActivity"},
|
||||
},
|
||||
// KS
|
||||
"com.smile.gifmaker": {
|
||||
"feed": []string{
|
||||
"com.yxcorp.gifshow.HomeActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
"live": []string{
|
||||
"com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
},
|
||||
// KS lite
|
||||
"com.kuaishou.nebula": {
|
||||
"feed": []string{
|
||||
"com.yxcorp.gifshow.HomeActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
"live": []string{
|
||||
"com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
},
|
||||
// TODO: SPH, XHS
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
|
||||
timestamp := time.Now().Format("20060102_150405") + fmt.Sprintf("_%03d", time.Now().UnixNano()/1e6%1000)
|
||||
fileName := filepath.Join(config.ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
|
||||
|
||||
@@ -201,21 +201,13 @@ func (ud *UIA2Driver) WindowSize() (size types.Size, err error) {
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// PressBack simulates a short press on the BACK button.
|
||||
func (ud *UIA2Driver) PressBack(opts ...option.ActionOption) (err error) {
|
||||
// Back simulates a short press on the BACK button.
|
||||
func (ud *UIA2Driver) Back() (err error) {
|
||||
// register(postHandler, new PressBack("/wd/hub/session/:sessionId/back"))
|
||||
_, err = ud.httpPOST(nil, "/session", ud.Session.ID, "back")
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) Homescreen() (err error) {
|
||||
return ud.PressKeyCodes(KCHome, KMEmpty)
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) PressKeyCode(keyCode KeyCode) (err error) {
|
||||
return ud.PressKeyCodes(keyCode, KMEmpty)
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta, flags ...KeyFlag) (err error) {
|
||||
// register(postHandler, new PressKeyCodeAsync("/wd/hub/session/:sessionId/appium/device/press_keycode"))
|
||||
data := map[string]interface{}{
|
||||
@@ -449,7 +441,7 @@ func (ud *UIA2Driver) GetPasteboard(contentType types.PasteboardType) (raw *byte
|
||||
}
|
||||
|
||||
// SendKeys Android input does not support setting frequency.
|
||||
func (ud *UIA2Driver) SendKeys(text string, opts ...option.ActionOption) (err error) {
|
||||
func (ud *UIA2Driver) Input(text string, opts ...option.ActionOption) (err error) {
|
||||
// register(postHandler, new SendKeysToElement("/wd/hub/session/:sessionId/keys"))
|
||||
// https://github.com/appium/appium-uiautomator2-server/blob/master/app/src/main/java/io/appium/uiautomator2/handler/SendKeysToElement.java#L76-L85
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
@@ -524,10 +516,6 @@ func (ud *UIA2Driver) SendActionKey(text string, opts ...option.ActionOption) (e
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) Input(text string, opts ...option.ActionOption) (err error) {
|
||||
return ud.SendKeys(text, opts...)
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) Rotation() (rotation types.Rotation, err error) {
|
||||
// register(getHandler, new GetRotation("/wd/hub/session/:sessionId/rotation"))
|
||||
var rawResp DriverRawResponse
|
||||
|
||||
@@ -186,7 +186,7 @@ func TestDriver_Drag(t *testing.T) {
|
||||
func TestDriver_SendKeys(t *testing.T) {
|
||||
setupAndroidUIA2Driver(t)
|
||||
|
||||
err := driverExt.SendKeys("辽宁省沈阳市新民市民族街36-4",
|
||||
err := driverExt.Input("辽宁省沈阳市新民市民族街36-4",
|
||||
option.WithIdentifier("test"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -208,7 +208,7 @@ func TestDriver_SendKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriver_PressBack(t *testing.T) {
|
||||
err := driver.PressBack()
|
||||
err := driver.Back()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -227,7 +227,7 @@ func TestDriver_GetOrientation(t *testing.T) {
|
||||
_, _ = driverExt.AppTerminate("com.quark.browser")
|
||||
_ = driverExt.AppLaunch("com.quark.browser")
|
||||
time.Sleep(2 * time.Second)
|
||||
_ = driverExt.Homescreen()
|
||||
_ = driverExt.Home()
|
||||
}
|
||||
|
||||
func Test_getFreePort(t *testing.T) {
|
||||
@@ -265,7 +265,7 @@ func TestDriver_IsAppInForeground(t *testing.T) {
|
||||
err := driverExt.AppLaunch("com.android.settings")
|
||||
checkErr(t, err)
|
||||
|
||||
app, err := driverExt.GetForegroundApp()
|
||||
app, err := driverExt.ForegroundInfo()
|
||||
checkErr(t, err)
|
||||
if app.PackageName != "com.android.settings" {
|
||||
t.FailNow()
|
||||
@@ -273,22 +273,6 @@ func TestDriver_IsAppInForeground(t *testing.T) {
|
||||
if app.Activity != ".Settings" {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
err = driverExt.AssertForegroundApp("com.android.settings")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
_, err = driverExt.AppTerminate("com.android.settings")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driverExt.AssertForegroundApp("com.android.settings")
|
||||
if err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_KeepAlive(t *testing.T) {
|
||||
@@ -345,7 +329,7 @@ func TestDriver_ShellInputUnicode(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.SendKeys("test中文输入&")
|
||||
err = driver.Input("test中文输入&")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -41,48 +41,42 @@ type IDriver interface {
|
||||
Status() (types.DeviceStatus, error)
|
||||
DeviceInfo() (types.DeviceInfo, error)
|
||||
BatteryInfo() (types.BatteryInfo, error)
|
||||
ForegroundInfo() (app types.AppInfo, err error)
|
||||
WindowSize() (types.Size, error)
|
||||
Scale() (float64, error)
|
||||
ScreenShot() (*bytes.Buffer, error)
|
||||
ScreenRecord(duration time.Duration) (videoPath string, err error)
|
||||
Source(srcOpt ...option.SourceOption) (string, error)
|
||||
Orientation() (orientation types.Orientation, err error)
|
||||
Rotation() (rotation types.Rotation, err error)
|
||||
|
||||
// config
|
||||
SetRotation(rotation types.Rotation) error
|
||||
SetIme(ime string) error
|
||||
|
||||
// actions
|
||||
Homescreen() error
|
||||
Unlock() (err error)
|
||||
Home() error
|
||||
Unlock() error
|
||||
Back() error
|
||||
// tap
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error
|
||||
DoubleTapXY(x, y float64, opts ...option.ActionOption) error
|
||||
TouchAndHold(x, y float64, opts ...option.ActionOption) error
|
||||
TapByText(text string, opts ...option.ActionOption) error // TODO: remove
|
||||
TapByTexts(actions ...TapTextAction) error // TODO: remove
|
||||
// swipe
|
||||
Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error
|
||||
Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error
|
||||
TouchAndHold(x, y float64, opts ...option.ActionOption) error
|
||||
// input
|
||||
SendKeys(text string, opts ...option.ActionOption) error
|
||||
Input(text string, opts ...option.ActionOption) error
|
||||
// press key
|
||||
PressButton(devBtn types.DeviceButton) error
|
||||
PressBack(opts ...option.ActionOption) error
|
||||
PressKeyCode(keyCode KeyCode) (err error)
|
||||
Backspace(count int, opts ...option.ActionOption) (err error)
|
||||
Backspace(count int, opts ...option.ActionOption) error
|
||||
|
||||
// app related
|
||||
AppLaunch(packageName string) error
|
||||
AppTerminate(packageName string) (bool, error)
|
||||
AppClear(packageName string) error
|
||||
GetForegroundApp() (app types.AppInfo, err error)
|
||||
AssertForegroundApp(packageName string, activityType ...string) error // TODO: remove
|
||||
|
||||
Orientation() (orientation types.Orientation, err error)
|
||||
SetRotation(rotation types.Rotation) (err error)
|
||||
Rotation() (rotation types.Rotation, err error)
|
||||
|
||||
SetIme(ime string) error
|
||||
|
||||
// triggers the log capture and returns the log entries
|
||||
StartCaptureLog(identifier ...string) (err error)
|
||||
StartCaptureLog(identifier ...string) error
|
||||
StopCaptureLog() (result interface{}, err error)
|
||||
}
|
||||
|
||||
@@ -108,7 +102,7 @@ type IDriverExt interface {
|
||||
TapByOCR(ocrText string, opts ...option.ActionOption) error
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error
|
||||
TapAbsXY(x, y float64, opts ...option.ActionOption) error
|
||||
TapOffset(param string, xOffset, yOffset float64, opts ...option.ActionOption) (err error)
|
||||
TapOffset(param string, xOffset, yOffset float64, opts ...option.ActionOption) error
|
||||
TapByUIDetection(opts ...option.ActionOption) error
|
||||
|
||||
// swipe
|
||||
@@ -123,8 +117,8 @@ type IDriverExt interface {
|
||||
CheckPopup() (popup *PopupInfo, err error)
|
||||
ClosePopupsHandler() error
|
||||
|
||||
DoAction(action MobileAction) (err error)
|
||||
DoValidation(check, assert, expected string, message ...string) (err error)
|
||||
DoAction(action MobileAction) error
|
||||
DoValidation(check, assert, expected string, message ...string) error
|
||||
}
|
||||
|
||||
type XTDriver struct {
|
||||
@@ -177,15 +171,20 @@ func (dExt *XTDriver) assertOCR(text, assert string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dExt *XTDriver) assertForegroundApp(appName, assert string) (err error) {
|
||||
err = dExt.AssertForegroundApp(appName)
|
||||
func (dExt *XTDriver) assertForegroundApp(appName, assert string) error {
|
||||
app, err := dExt.ForegroundInfo()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("get foreground app failed, skip app assertion")
|
||||
return nil // Notice: ignore error when get foreground app failed
|
||||
}
|
||||
|
||||
switch assert {
|
||||
case AssertionEqual:
|
||||
if err != nil {
|
||||
if app.PackageName != appName {
|
||||
return errors.Wrap(err, "assert foreground app equal failed")
|
||||
}
|
||||
case AssertionNotEqual:
|
||||
if err == nil {
|
||||
if app.PackageName == appName {
|
||||
return errors.New("assert foreground app not equal failed")
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -196,7 +196,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
|
||||
}
|
||||
return fmt.Errorf("app_terminate params should be bundleId(string), got %v", action.Params)
|
||||
case ACTION_Home:
|
||||
return dExt.Homescreen()
|
||||
return dExt.Home()
|
||||
case ACTION_SetIme:
|
||||
if ime, ok := action.Params.(string); ok {
|
||||
err = dExt.SetIme(ime)
|
||||
@@ -276,7 +276,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
|
||||
param := fmt.Sprintf("%v", action.Params)
|
||||
return dExt.Input(param)
|
||||
case ACTION_Back:
|
||||
return dExt.PressBack()
|
||||
return dExt.Back()
|
||||
case ACTION_Sleep:
|
||||
if param, ok := action.Params.(json.Number); ok {
|
||||
seconds, _ := param.Float64()
|
||||
|
||||
@@ -109,7 +109,7 @@ func (sad *ShootsAndroidDriver) close() error {
|
||||
}
|
||||
|
||||
func (sad *ShootsAndroidDriver) Status() (types.DeviceStatus, error) {
|
||||
app, err := sad.GetForegroundApp()
|
||||
app, err := sad.ForegroundInfo()
|
||||
if err != nil {
|
||||
return types.DeviceStatus{}, err
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func (sad *ShootsAndroidDriver) Status() (types.DeviceStatus, error) {
|
||||
}
|
||||
|
||||
func (sad *ShootsAndroidDriver) Source(srcOpt ...option.SourceOption) (source string, err error) {
|
||||
app, err := sad.GetForegroundApp()
|
||||
app, err := sad.ForegroundInfo()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestIOSSource(t *testing.T) {
|
||||
|
||||
func TestIOSForeground(t *testing.T) {
|
||||
setupShootsIOSDriver(t)
|
||||
app, err := shootsIOSDriver.GetForegroundApp()
|
||||
app, err := shootsIOSDriver.ForegroundInfo()
|
||||
checkErr(t, err)
|
||||
t.Log(app)
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ func (dExt *XTDriver) swipeToTapTexts(texts []string, opts ...option.ActionOptio
|
||||
|
||||
func (dExt *XTDriver) SwipeToTapApp(appName string, opts ...option.ActionOption) error {
|
||||
// go to home screen
|
||||
if err := dExt.Homescreen(); err != nil {
|
||||
if err := dExt.Home(); err != nil {
|
||||
return errors.Wrap(err, "go to home screen failed")
|
||||
}
|
||||
|
||||
|
||||
@@ -80,11 +80,7 @@ func (hd *HDCDriver) WindowSize() (size types.Size, err error) {
|
||||
return size, err
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) Scale() (float64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) Homescreen() error {
|
||||
func (hd *HDCDriver) Home() error {
|
||||
return hd.uiDriver.PressKey(ghdc.KEYCODE_HOME)
|
||||
}
|
||||
|
||||
@@ -132,16 +128,11 @@ func (hd *HDCDriver) AppTerminate(packageName string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) GetForegroundApp() (app types.AppInfo, err error) {
|
||||
func (hd *HDCDriver) ForegroundInfo() (app types.AppInfo, err error) {
|
||||
// Todo
|
||||
return types.AppInfo{}, types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) AssertForegroundApp(packageName string, activityType ...string) error {
|
||||
// Todo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) Orientation() (orientation types.Orientation, err error) {
|
||||
return types.OrientationPortrait, nil
|
||||
}
|
||||
@@ -204,10 +195,6 @@ func (hd *HDCDriver) SetIme(ime string) error {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) SendKeys(text string, opts ...option.ActionOption) error {
|
||||
return hd.uiDriver.InputText(text)
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) Input(text string, opts ...option.ActionOption) error {
|
||||
return hd.uiDriver.InputText(text)
|
||||
}
|
||||
@@ -216,11 +203,7 @@ func (hd *HDCDriver) AppClear(packageName string) error {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) PressButton(devBtn types.DeviceButton) error {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) PressBack(opts ...option.ActionOption) error {
|
||||
func (hd *HDCDriver) Back() error {
|
||||
return hd.uiDriver.PressBack()
|
||||
}
|
||||
|
||||
@@ -228,10 +211,6 @@ func (hd *HDCDriver) Backspace(count int, opts ...option.ActionOption) (err erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) PressKeyCode(keyCode KeyCode) (err error) {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) PressHarmonyKeyCode(keyCode ghdc.KeyCode) (err error) {
|
||||
return hd.uiDriver.PressKey(keyCode)
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestHarmonyInput(t *testing.T) {
|
||||
|
||||
func TestHomeScreen(t *testing.T) {
|
||||
setupHarmonyDevice(t)
|
||||
err := driver.Homescreen()
|
||||
err := driver.Home()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func TestUnlock(t *testing.T) {
|
||||
|
||||
func TestPressBack(t *testing.T) {
|
||||
setupHarmonyDevice(t)
|
||||
err := driver.PressBack()
|
||||
err := driver.Back()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestLaunch(t *testing.T) {
|
||||
|
||||
func TestForegroundApp(t *testing.T) {
|
||||
setupHarmonyDevice(t)
|
||||
appInfo, err := driver.GetForegroundApp()
|
||||
appInfo, err := driver.ForegroundInfo()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ func (dev *IOSDevice) NewDriver() (driver IDriver, err error) {
|
||||
|
||||
if dev.Options.ResetHomeOnStartup {
|
||||
log.Info().Msg("go back to home screen")
|
||||
if err = wdaDriver.Homescreen(); err != nil {
|
||||
if err = wdaDriver.Home(); err != nil {
|
||||
return nil, errors.Wrap(code.MobileUIDriverError,
|
||||
fmt.Sprintf("go back to home screen failed: %v", err))
|
||||
}
|
||||
|
||||
@@ -254,6 +254,18 @@ func (wd *WDADriver) WindowSize() (size types.Size, err error) {
|
||||
return wd.Session.windowSize, nil
|
||||
}
|
||||
|
||||
func (wd *WDADriver) Scale() (float64, error) {
|
||||
if !builtin.IsZeroFloat64(wd.Session.scale) {
|
||||
return wd.Session.scale, nil
|
||||
}
|
||||
screen, err := wd.Screen()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(code.MobileUIDriverError,
|
||||
fmt.Sprintf("get screen info failed: %v", err))
|
||||
}
|
||||
return screen.Scale, nil
|
||||
}
|
||||
|
||||
func (wd *WDADriver) Screen() (screen ai.Screen, err error) {
|
||||
// [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)]
|
||||
var rawResp DriverRawResponse
|
||||
@@ -284,18 +296,6 @@ func (wd *WDADriver) ScreenShot() (raw *bytes.Buffer, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *WDADriver) Scale() (float64, error) {
|
||||
if !builtin.IsZeroFloat64(wd.Session.scale) {
|
||||
return wd.Session.scale, nil
|
||||
}
|
||||
screen, err := wd.Screen()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(code.MobileUIDriverError,
|
||||
fmt.Sprintf("get screen info failed: %v", err))
|
||||
}
|
||||
return screen.Scale, nil
|
||||
}
|
||||
|
||||
func (wd *WDADriver) toScale(x float64) float64 {
|
||||
return x / wd.Session.scale
|
||||
}
|
||||
@@ -372,7 +372,7 @@ func (wd *WDADriver) Lock() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *WDADriver) Homescreen() (err error) {
|
||||
func (wd *WDADriver) Home() (err error) {
|
||||
// [[FBRoute POST:@"/wda/homescreen"].withoutSession respondWithTarget:self action:@selector(handleHomescreenCommand:)]
|
||||
_, err = wd.httpPOST(nil, "/wda/homescreen")
|
||||
return
|
||||
@@ -490,7 +490,7 @@ func (wd *WDADriver) AppDeactivate(second float64) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *WDADriver) GetForegroundApp() (appInfo types.AppInfo, err error) {
|
||||
func (wd *WDADriver) ForegroundInfo() (appInfo types.AppInfo, err error) {
|
||||
activeAppInfo, err := wd.ActiveAppInfo()
|
||||
appInfo.BundleId = activeAppInfo.BundleId
|
||||
if err != nil {
|
||||
@@ -514,29 +514,6 @@ func (wd *WDADriver) GetForegroundApp() (appInfo types.AppInfo, err error) {
|
||||
return appInfo, err
|
||||
}
|
||||
|
||||
func (wd *WDADriver) AssertForegroundApp(bundleId string, viewControllerType ...string) error {
|
||||
log.Debug().Str("bundleId", bundleId).
|
||||
Strs("viewControllerType", viewControllerType).
|
||||
Msg("assert ios foreground bundleId")
|
||||
|
||||
app, err := wd.GetForegroundApp()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("get foreground app failed, skip bundleId assertion")
|
||||
return nil // Notice: ignore error when get foreground app failed
|
||||
}
|
||||
|
||||
// assert package
|
||||
if app.BundleId != bundleId {
|
||||
log.Error().
|
||||
Interface("foreground_app", app.AppBaseInfo).
|
||||
Str("expected_package", bundleId).
|
||||
Msg("assert package failed")
|
||||
return errors.Wrap(code.MobileUIAssertForegroundAppError,
|
||||
"assert foreground package failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) (err error) {
|
||||
// [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)]
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
@@ -657,11 +634,7 @@ func (wd *WDADriver) SetIme(ime string) error {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (wd *WDADriver) PressKeyCode(keyCode KeyCode) (err error) {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
func (wd *WDADriver) SendKeys(text string, opts ...option.ActionOption) (err error) {
|
||||
func (wd *WDADriver) Input(text string, opts ...option.ActionOption) (err error) {
|
||||
// [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)]
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
data := map[string]interface{}{"value": strings.Split(text, "")}
|
||||
@@ -687,18 +660,12 @@ func (wd *WDADriver) Backspace(count int, opts ...option.ActionOption) (err erro
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *WDADriver) Input(text string, opts ...option.ActionOption) (err error) {
|
||||
return wd.SendKeys(text, opts...)
|
||||
}
|
||||
|
||||
func (wd *WDADriver) AppClear(packageName string) error {
|
||||
return types.ErrDriverNotImplemented
|
||||
}
|
||||
|
||||
// PressBack simulates a short press on the BACK button.
|
||||
func (wd *WDADriver) PressBack(opts ...option.ActionOption) (err error) {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
// Back simulates a short press on the BACK button.
|
||||
func (wd *WDADriver) Back() (err error) {
|
||||
windowSize, err := wd.WindowSize()
|
||||
if err != nil {
|
||||
return
|
||||
@@ -707,16 +674,6 @@ func (wd *WDADriver) PressBack(opts ...option.ActionOption) (err error) {
|
||||
fromY := wd.toScale(float64(windowSize.Height) * 0.5)
|
||||
toX := wd.toScale(float64(windowSize.Width) * 0.6)
|
||||
toY := wd.toScale(float64(windowSize.Height) * 0.5)
|
||||
if len(actionOptions.Offset) == 4 {
|
||||
fromX += float64(actionOptions.Offset[0])
|
||||
fromY += float64(actionOptions.Offset[1])
|
||||
toX += float64(actionOptions.Offset[2])
|
||||
toY += float64(actionOptions.Offset[3])
|
||||
}
|
||||
fromX += actionOptions.GetRandomOffset()
|
||||
fromY += actionOptions.GetRandomOffset()
|
||||
toX += actionOptions.GetRandomOffset()
|
||||
toY += actionOptions.GetRandomOffset()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"fromX": fromX,
|
||||
@@ -725,9 +682,6 @@ func (wd *WDADriver) PressBack(opts ...option.ActionOption) (err error) {
|
||||
"toY": toY,
|
||||
}
|
||||
|
||||
// update data options in post data for extra WDA configurations
|
||||
actionOptions.UpdateData(data)
|
||||
|
||||
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/dragfromtoforduration")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestNewUSBDriver(t *testing.T) {
|
||||
func TestDriver_DeviceScaleRatio(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
scaleRatio, err := driver.Scale()
|
||||
scaleRatio, err := driver.(*WDADriver).Scale()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func Test_remoteWD_Screen(t *testing.T) {
|
||||
func Test_remoteWD_Homescreen(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
err := driver.Homescreen()
|
||||
err := driver.Home()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func Test_Relative_Drag(t *testing.T) {
|
||||
func Test_remoteWD_SendKeys(t *testing.T) {
|
||||
setup(t)
|
||||
// driver.StartCaptureLog("hrp_wda_log")
|
||||
err := driver.SendKeys("test", option.WithIdentifier("test"))
|
||||
err := driver.Input("test", option.WithIdentifier("test"))
|
||||
// result, _ := driver.StopCaptureLog()
|
||||
// err := driver.SendKeys("App Store", WithFrequency(3))
|
||||
if err != nil {
|
||||
@@ -329,17 +329,17 @@ func Test_remoteWD_SendKeys(t *testing.T) {
|
||||
func Test_remoteWD_PressButton(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
err := driver.PressButton(types.DeviceButtonVolumeUp)
|
||||
err := driver.(*WDADriver).PressButton(types.DeviceButtonVolumeUp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second * 1)
|
||||
err = driver.PressButton(types.DeviceButtonVolumeDown)
|
||||
err = driver.(*WDADriver).PressButton(types.DeviceButtonVolumeDown)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second * 1)
|
||||
err = driver.PressButton(types.DeviceButtonHome)
|
||||
err = driver.(*WDADriver).PressButton(types.DeviceButtonHome)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -413,7 +413,7 @@ func Test_remoteWD_Source(t *testing.T) {
|
||||
|
||||
func TestGetForegroundApp(t *testing.T) {
|
||||
setup(t)
|
||||
app, err := driver.GetForegroundApp()
|
||||
app, err := driver.ForegroundInfo()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user