一、使用方法
1.查找App的包名和进程名称
adb shell dumpsys window windows | findstr mCurrentFocus
图示2.输入内容链接到app
图示3.点击开启录制
图示4.录制需要的步骤即可
图示5.录制的部分自动生成代码
图示6.如果需要压测,把相关代码copy出来,新建python文件,将其写入到for循环中即可。
二、常用的一些方法
def screen_shot(self, appPath):
"""
获取当前设备的截图,导出到指定目录"""
self.shelltimeout("/system/bin/screencap -p /sdcard/temp.png", 10)
self.pull('/sdcard/temp.png', appPath)
self.shelltimeout("rm /sdcard/temp.png", 5)
def get_wifi_name(self):
"""
查询连接wifi名称"""
for packagein self.shell_output('dumpsys wifi').splitlines():
if package.startswith('mWifiInfo'):
wifi_name = re.findall(r'SSID:([^"]+), BSSID', package)
if not wifi_name:
return ''
else:
return wifi_name[0].strip()
def touch_by_element(self, element):
"""
点击元素 usage: touchByElement(Element().findElementByName(u"计算器"))
"""
self.shelltimeout("input tap %s %s" % (str(element[0]), str(element[1])), 30)
time.sleep(0.5)
def get_focused_package_xml(self, save_path):
file_name = random.randint(1, 999)
self.shelltimeout(
'uiautomator dump /data/local/tmp/{}.xml'.format(file_name), 10)
self.pull('/data/local/tmp/%s.xml' % file_name, save_path)
def get_focused_package_and_activity(self):
"""
获取当前应用界面的包名和Activity,返回的字符串格式为:packageName/activityName
"""
output =self.shell_output('dumpsys window windows | grep mCurrentFocus')
output = output.strip().split()
if output[-1].endswith('}'):
return output[-1][:-1]
else:
return output[-1]
def get_current_package_name(self):
"""
获取当前运行的应用的包名"""
pkg =self.get_focused_package_and_activity().split("/")
if len(pkg) >0:
return pkg[0]
else:
return ''
def get_current_activity(self):
"""
获取当前运行应用的activity
"""
pkg =self.get_focused_package_and_activity().split("/")
if len(pkg) >0:
return pkg[-1]
else:
return ''
def get_matching_app_list(self, keyword):
"""
模糊查询与keyword匹配的应用包名列表usage: getMatchingAppList("com.gitvdemo.video")
"""
matApp = []
for packagein self.shell_output('pm list packages %s' % keyword).strip().splitlines():
package_info = package.split(":")
if len(package_info) >0:
if '=' in package_info[1]:
matApp.append(package_info[1].split('=')[1])
else:
matApp.append(package.split(":")[-1])
return matApp
def list_dir(self, _dir, find_str=None):
dirs =self.shell_output('ls ' + _dir)
if find_str:
grep_cmd = [find_util, find_str]
p =self.Popen(grep_cmd, stdin=subprocess.PIPE)
out, err = p.communicate(dirs)
return out.strip()
else:
return dirs
def is_install(self, packageName):
"""
判断应用是否安装,已安装返回True,否则返回False
usage: isInstall("com.gitvdemo.video")
"""
if self.get_matching_app_list(packageName):
return True
else:
return False
def specifies_app_version_name(self, package):
"""
获取指定应用的versionName
:param package:应用包名 :return: 包名,versionName
"""
versionName =""
versionCode =""
for packagein self.shell_output('dumpsys package %s' % package).splitlines():
if 'versionName' in package:
versionName = package.split('=', 2)[1].strip()
if 'versionCode' in package:
versionCode = package.split()[0].split('=', 2)[1].strip()
return versionName +"-" + versionCode
def get_app_version_name(self, package):
"""
获取指定应用的versionName
:param package:应用包名 :return: 包名"""
versionName =""
for packagein self.shell_output('dumpsys package %s' % package).splitlines():
if 'versionName' in package:
versionName = package.split('=', 2)[1].strip()
return versionName
网友评论