feat: GetPackageInfo for android

This commit is contained in:
lilong.129
2024-09-13 21:59:48 +08:00
parent f8c2c3c7fb
commit b8e1dda6fc
4 changed files with 34 additions and 1 deletions

View File

@@ -438,6 +438,26 @@ func (dev *AndroidDevice) installCommon(app io.ReadSeeker, args ...string) error
return err
}
func (dev *AndroidDevice) GetPackageInfo(packageName string) (AppInfo, error) {
appInfo := AppInfo{
Name: packageName,
}
output, err := dev.d.RunShellCommand("dumpsys", "package", packageName, "|", "grep", "versionName")
if err != nil {
return appInfo, err
}
re := regexp.MustCompile(`versionName=(.+)`)
matches := re.FindStringSubmatch(output)
if len(matches) > 1 {
appInfo.AppBaseInfo = AppBaseInfo{
PackageName: packageName,
VersionName: matches[1],
}
return appInfo, nil
}
return appInfo, errors.New("failed to get package version")
}
type LineCallback func(string)
type AdbLogcat struct {

View File

@@ -34,6 +34,14 @@ func setupAndroidUIA2Driver(t *testing.T) {
checkErr(t, err)
}
func TestAndroidDevice_GetPackageInfo(t *testing.T) {
device, err := NewAndroidDevice()
checkErr(t, err)
appInfo, err := device.GetPackageInfo("com.android.settings")
checkErr(t, err)
t.Log(appInfo)
}
func TestDriver_NewSession(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {

View File

@@ -487,9 +487,10 @@ type IDevice interface {
StartPcap() error
StopPcap() string
Install(appPath string, opts *InstallOptions) error
Uninstall(packageName string) error
Install(appPath string, opts *InstallOptions) error
GetPackageInfo(packageName string) (AppInfo, error)
}
type ForegroundApp struct {

View File

@@ -733,3 +733,7 @@ func (dev *IOSDevice) RunXCTest(bundleID string) (cancel context.CancelFunc, err
return cancel, nil
}
func (dev *IOSDevice) GetPackageInfo(packageName string) (AppInfo, error) {
return AppInfo{}, nil
}