feat: add GetCurrentWindow() for Android device

This commit is contained in:
lilong.129
2024-11-20 11:36:19 +08:00
parent 6e2f0188b3
commit 7d6b5d2405
6 changed files with 41 additions and 1 deletions

View File

@@ -465,6 +465,24 @@ func (dev *AndroidDevice) installCommon(apkPath string, args ...string) error {
return err
}
func (dev *AndroidDevice) GetCurrentWindow() (windowInfo WindowInfo, err error) {
output, err := dev.d.RunShellCommand("dumpsys", "window", "|", "grep", "mCurrentFocus")
if err != nil {
return WindowInfo{}, errors.Wrap(err, "get current window failed")
}
// mCurrentFocus=Window{a33bc55 u0 com.miui.home/com.miui.home.launcher.Launcher}
re := regexp.MustCompile(`mCurrentFocus=Window{.*? (\S+)/(\S+)}`)
matches := re.FindStringSubmatch(output)
if len(matches) != 3 {
return WindowInfo{}, errors.New("failed to extract current window")
}
windowInfo = WindowInfo{
PackageName: matches[1],
Activity: matches[2],
}
return windowInfo, nil
}
func (dev *AndroidDevice) GetPackageInfo(packageName string) (AppInfo, error) {
appInfo := AppInfo{
Name: packageName,

View File

@@ -42,6 +42,14 @@ func TestAndroidDevice_GetPackageInfo(t *testing.T) {
t.Log(appInfo)
}
func TestAndroidDevice_GetCurrentWindow(t *testing.T) {
device, err := NewAndroidDevice()
checkErr(t, err)
windowInfo, err := device.GetCurrentWindow()
checkErr(t, err)
t.Logf("packageName: %s\tactivityName: %s", windowInfo.PackageName, windowInfo.Activity)
}
func TestDriver_NewSession(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {

View File

@@ -172,3 +172,7 @@ func (dev *HarmonyDevice) Uninstall(packageName string) error {
func (dev *HarmonyDevice) GetPackageInfo(packageName string) (AppInfo, error) {
return AppInfo{}, nil
}
func (dev *HarmonyDevice) GetCurrentWindow() (WindowInfo, error) {
return WindowInfo{}, nil
}

View File

@@ -254,6 +254,11 @@ type AppInfo struct {
AppBaseInfo
}
type WindowInfo struct {
PackageName string `json:"packageName,omitempty"`
Activity string `json:"activity,omitempty"`
}
type AppBaseInfo struct {
Pid int `json:"pid,omitempty"`
BundleId string `json:"bundleId,omitempty"` // ios package name
@@ -495,6 +500,7 @@ type IDevice interface {
Uninstall(packageName string) error
GetPackageInfo(packageName string) (AppInfo, error)
GetCurrentWindow() (windowInfo WindowInfo, err error)
}
type ForegroundApp struct {

View File

@@ -771,3 +771,7 @@ func (dev *IOSDevice) GetPackageInfo(packageName string) (AppInfo, error) {
}
return appInfo, errors.New("failed to get package version")
}
func (dev *IOSDevice) GetCurrentWindow() (WindowInfo, error) {
return WindowInfo{}, nil
}