有朋友提示用uiaotumator,多线程,一边安装一边检测是否有弹窗,亲测可行,完美解决。
# encoding:UTF8import pexpect, sys, os.path, subprocessfrom uiautomator import Devicefrom time import sleepimport threadingdef install_apk(device, path_to_apk):
print 'Installing apk: {0} to device: {1}'.format(path_to_apk, device)
P = subprocess.Popen("adb -s {0} install {1}".format(device, path_to_apk),stdout=subprocess.PIPE, shell=True)
data = P.stdout.read()
if "Success" in str(data):
print 'Installing apk: {0} for device: {1} Success'.format(path_to_apk, device)
exit(1)
if "Failure" in str(data):
print 'Installing apk: {0} for device: {1} Failure'.format(path_to_apk, device)
exit(0)def uninstall_apk(device, package):
print 'Uninstalling apk: {0} for device: {1}'.format(package, device)
result = str(pexpect.run('adb -s {0} uninstall {1}'.format(device, package))).strip().lower()
if 'failure' == result:
print 'WARN: Uninstall apk: {0} for device: {1} has error, maybe not exists.'.format(package, device);
exit(0)
if 'success' != result:
print 'Uninstall apk: {0} for device: {1} failed, msg: {2}'.format(package, device, result)
exit(1)def protect(nub,device):
sleep(2)
for i in range(nub):
d = Device(device)
el1 = d(text="安装")
el2 = d(text="确定")
if el1.exists:
el1.click()
if el2.exists:
el2.click()
sleep(2)if __name__ == '__main__':
if len(sys.argv) < 4:
print 'Arguments error, usage: python install_app <install | uninstall> <device> <apk | package>'
exit(1)
op = sys.argv[1]
device = sys.argv[2]
apk = sys.argv[3]
if op == 'install':
threads = []
install = threading.Thread(target=install_apk, args=(device,apk))
protect = threading.Thread(target=protect, args=(30,device))
threads.append(install)
threads.append(protect)
for t in threads:
t.setDaemon(True)
t.start()
t.join()
else :
package = sys.argv[3]
uninstall_apk(device, package)
最近在moto x上(5.0的,不确定是不是所有5.0都这样)跑case遇到一个问题:每次在appium安装应用的时候系统会弹窗提示是否确认安装。需要手动点击确认才能继续,否则就只能卡在这里。
翻看源码发现appium使用的是adb install
来安装应用的,手动执行adb install
确实会弹窗
度娘那里得来还有一种安装方法pm install
手动尝试先把包push到手机上,再pm install
,没有弹窗,可以解决问题。
于是注释掉appium安装,顺手也注释掉unlock和app setting的安装。
文件路径:/usr/local/lib/node_modules/appium/lib/devices/android/android.js ,如果使用的是selendroid模式,则需要注释掉selendroid.js里的相关内容
//this.uninstallApp.bind(this),//this.installAppForTest.bind(this),//this.pushSettingsApp.bind(this),//this.pushUnlock.bind(this),
手动写了一个package、unlock、app setting的安装方法,每次执行case集前调用一次,即解决了个别机器弹窗的问题,又避免了appium每执行一个case都要安装三个应用,提高了执行速度。
# encoding:UTF8import pexpect, sys, os.path, subprocessdef install_apk(device, path_to_apk):
print 'Installing apk: {0} for device: {1}'.format(path_to_apk, device)
file_name = os.path.basename(path_to_apk)
pexpect.run('adb -s {0} push {1} /sdcard/{2}'.format(device, path_to_apk, file_name))
p = pexpect.spawn('adb -s {0} shell'.format(device))
p.logfile = sys.stdout
p.expect('.*shell@.*', 20)
p.sendline('pm install /sdcard/{0}'.format(file_name))
index = p.expect(['Success', '.*shell@.*'], 120)
p.sendline('rm /sdcard/{0}'.format(file_name))
if index == 1:
print 'Intall apk: {0} for device: {1} failed.'.format(path_to_apk, device)
exit(1)def uninstall_apk(device, package):
print 'Uninstalling apk: {0} for device: {1}'.format(package, device)
result = str(pexpect.run('adb -s {0} uninstall {1}'.format(device, package))).strip().lower()
if 'failure' == result:
print 'WARN: Uninstall apk: {0} for device: {1} has error, maybe not exists.'.format(package, device);
exit(0)
if 'success' != result:
print 'Uninstall apk: {0} for device: {1} failed, msg: {2}'.format(package, device, result)
exit(1)if __name__ == '__main__':
if len(sys.argv) < 4:
print 'Arguments error, usage: python install_app <install | uninstall> <device> <apk | package>'
exit(1)
op = sys.argv[1]
device = sys.argv[2]
if op == 'install':
apk = sys.argv[3]
install_apk(device, apk)
else :
package = sys.argv[3]
uninstall_apk(device, package)
另外小米的机器不管使用什么方法,仍然会有弹窗,我还是没能解决,不知道各位高手有什么方法。
联系客服