import os,re,subprocess
'''执行cmd命令——方法1:使用OS模块os.sysytem(),无返回值'''
'''执行cmd命令——方法2:使用OS模块os.popen()'''
'''使用使用OS模块os.sysytem() 与monkey启动应用
android.intent.action.MAIN 决定应用程序最先启动的Activity
android.intent.category.LAUNCHER 决定应用程序是否显示在程序列表里
'''
class AppTest():
'''adb命令获取app布局文件xml'''
def get_xml(self,deviceID):
os.system('adb -s {} shell /system/bin/uiautomator dump --compressed /data/local/tmp/dump.xml'.format(deviceID))
os.system('adb -s {} pull /data/local/tmp/dump.xml'.format(deviceID))
'''返回手机屏幕大小[Xvalue,Yvalue]'''
def get_screen_size(self,deviceID):
sizeStr = os.popen('adb -s {} shell wm size'.format(deviceID)).read()
if not sizeStr:#Physical size: 720x1520
print('获取手机屏幕大小命令adb shell wm size无返回')
reValue = re.search(r'(\d+)x(\d+)',sizeStr)
if reValue:
Xvalue = int(reValue.group(1))
Yvalue = int(reValue.group(2))
return [Xvalue,Yvalue]
'''启动UIAUTOMATOR,避免手动操作'''
def open_ATX(self, devices_id, packageName):
os.system('adb -s {} shell /data/local/tmp/atx-agent server -d'.format(devices_id))
# os.system('adb shell monkey -p {} -c android.intent.category.LAUNCHER 1'.format( packageName))
os.system('adb -s {0} shell am start -n {1}/.MainActivity'.format(devices_id, packageName))
# os.system('adb shell am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n {}/.MainActivity'.format(packageName))
# 点击启动UIAUTOMATOR
screenSIze = self.get_screen_size(devices_id)
os.system('adb shell input tap {0} {1}'.format(0.25 * screenSIze[0], 0.2 * screenSIze[1]))
'''执行cmd命令,传入命令,返回输出值'''
def command(self,cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
s = str(output, encoding='utf-8')
return s
if __name__ == '__main__':
devices_id = 'c2913bcb' # adb devices 自由更改
devices_id_6p = '8XV7N16310003265' # adb devices 自由更改
pkg_APP = 'tencent.dobbysemanticofflineapp' # 包名根据自己测试应用来更改
pkg_ATX = 'com.github.uiautomator'
appTest = AppTest()
appTest.open_ATX(devices_id,pkg_ATX)
安卓APP-public(获取布局文件+返回手机屏幕大小+启动UIAUTOMATOR-ATX +cmd执行并返回值)
最新推荐文章于 2023-02-21 11:02:55 发布