Feat/yuhongzheng/pre auto install

* fix: fix rotate tap swipe error
* fix: default input frequency from 60 to 10
* fix: error getting window size during screen rotation
* fix: kuake input unicode error
* feat: android input by appium ime
* feat: android swipe and tap with duration
* fix: format import file
* fix: format import file
* feat: 新增按控件点击,获取设备应用,修改日志获取
* feat: 新增ui2控件点击
* fix: format file
* fix: format file
* Merge branch 'video-release' into 'feat/yuhongzheng/pre_auto_install'
* merge
* Merge branch 'feat/yuhongzheng/pre_auto_install' of…
* fix: close reader
* Merge branch 'video-release' into 'feat/yuhongzheng/pre_auto_install'
* fix: format code
* Merge branch 'feat/yuhongzheng/pre_auto_install' of…
* fix: test send key

https://code.byted.org/iesqa/httprunner/merge_requests/34
This commit is contained in:
余泓铮
2024-05-10 07:02:41 +00:00
parent 912b4b943d
commit 3b4367cac4
20 changed files with 1108 additions and 134 deletions

View File

@@ -107,25 +107,38 @@ func (d *Device) features() (features Features, err error) {
return features, nil
}
func (d Device) HasAttribute(key string) bool {
func (d *Device) HasAttribute(key string) bool {
_, ok := d.attrs[key]
return ok
}
func (d Device) Product() (string, error) {
func (d *Device) Product() (string, error) {
if d.HasAttribute("product") {
return d.attrs["product"], nil
}
return "", errors.New("does not have attribute: product")
}
func (d Device) Model() (string, error) {
func (d *Device) Model() (string, error) {
if d.HasAttribute("model") {
return d.attrs["model"], nil
}
return "", errors.New("does not have attribute: model")
}
func (d *Device) Brand() (string, error) {
if d.HasAttribute("brand") {
return d.attrs["brand"], nil
}
brand, err := d.RunShellCommand("getprop", "ro.product.brand")
brand = strings.TrimSpace(brand)
if err != nil {
return "", errors.New("does not have attribute: brand")
}
d.attrs["brand"] = brand
return brand, nil
}
func (d *Device) Usb() (string, error) {
if d.HasAttribute("usb") {
return d.attrs["usb"], nil
@@ -133,7 +146,7 @@ func (d *Device) Usb() (string, error) {
return "", errors.New("does not have attribute: usb")
}
func (d Device) transportId() (string, error) {
func (d *Device) transportId() (string, error) {
if d.HasAttribute("transport_id") {
return d.attrs["transport_id"], nil
}
@@ -524,7 +537,7 @@ func (d *Device) Pull(remotePath string, dest io.Writer) (err error) {
return
}
func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byte, err error) {
var (
tp transport
filesize int64
@@ -537,8 +550,11 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
return nil, err
}
defer func() { _ = tp.Close() }()
cmd := fmt.Sprintf("abb_exec:package\x00install\x00-t\x00-S\x00%d", filesize)
cmd := "abb_exec:package\x00install\x00-t"
for _, arg := range args {
cmd += "\x00" + arg
}
cmd += fmt.Sprintf("\x00-S\x00%d", filesize)
if err = tp.SendWithCheck(cmd); err != nil {
return nil, err
}
@@ -555,7 +571,7 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
return
}
func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
func (d *Device) InstallAPK(apk io.ReadSeeker, args ...string) (string, error) {
haserr := func(ret string) bool {
return strings.Contains(ret, "Failure")
}
@@ -575,8 +591,9 @@ func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
if err != nil {
return "", fmt.Errorf("error pushing: %v", err)
}
res, err := d.RunShellCommand("pm", "install", "-f", remote)
args = append([]string{"install"}, args...)
args = append(args, "-f", remote)
res, err := d.RunShellCommand("pm", args...)
if err != nil {
return "", errors.Wrap(err, "install apk failed")
}
@@ -591,7 +608,7 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
if len(keepData) == 0 {
keepData = []bool{false}
}
packageName = strings.ReplaceAll(packageName, " ", "")
packageName = strings.TrimSpace(packageName)
if len(packageName) == 0 {
return "", fmt.Errorf("invalid package name")
}
@@ -603,6 +620,33 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
return d.RunShellCommand("pm", args...)
}
func (d *Device) ListPackages() ([]string, error) {
args := []string{"list", "packages"}
resRaw, err := d.RunShellCommand("pm", args...)
if err != nil {
return []string{}, err
}
lines := strings.Split(resRaw, "\n")
var packages []string
for _, line := range lines {
packageName := strings.TrimPrefix(line, "package:")
packages = append(packages, packageName)
}
return packages, nil
}
func (d *Device) IsPackagesInstalled(packageName string) bool {
packages, err := d.ListPackages()
if err != nil {
return false
}
packageName = strings.TrimSpace(packageName)
if len(packageName) == 0 {
return false
}
return builtin.Contains(packages, packageName)
}
func (d *Device) ScreenCap() ([]byte, error) {
if d.HasFeature(FeatShellV2) {
return d.RunShellCommandV2WithBytes("screencap", "-p")

View File

@@ -60,7 +60,10 @@ func TestDevice_Product(t *testing.T) {
for i := range devices {
dev := devices[i]
product := dev.Product()
product, err := dev.Product()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), product)
}
}
@@ -70,7 +73,24 @@ func TestDevice_Model(t *testing.T) {
for i := range devices {
dev := devices[i]
t.Log(dev.Serial(), dev.Model())
model, err := dev.Model()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), model)
}
}
func TestDevice_Brand(t *testing.T) {
setupDevices(t)
for i := range devices {
dev := devices[i]
brand, err := dev.Brand()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), brand)
}
}
@@ -79,7 +99,15 @@ func TestDevice_Usb(t *testing.T) {
for i := range devices {
dev := devices[i]
t.Log(dev.Serial(), dev.Usb(), dev.IsUsb())
usb, err := dev.Usb()
if err != nil {
t.Fatal(err)
}
isUsb, err := dev.IsUsb()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), usb, isUsb)
}
}
@@ -315,6 +343,22 @@ func TestDevice_InstallAPK(t *testing.T) {
}
}
func TestDevice_ListPackages(t *testing.T) {
setupDevices(t)
for _, dev := range devices {
res, err := dev.ListPackages()
if err != nil {
t.Fatal(err)
}
t.Log(res)
installed := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
if err != nil {
t.Fatal(err)
}
t.Log(installed)
}
}
func TestDevice_HasFeature(t *testing.T) {
setupDevices(t)