目的:实现安卓手机app安装卸载,并且兼容性测试
#父类
#注意点:类初始化、is_app_installed、remove_app、insall_app、is_enable、智能等待
from appium.webdriver.webdriver import WebDriver
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
class YD_install_remove():
def __init__(self):
self.caps = {}
self.caps['automationName'] = 'UiAutomator2'
self.caps['platformName'] = 'Android'
self.caps['platformVersion'] = '6.0'
self.caps['deviceName'] = '192.168.111.101:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver=WebDriver('htp://127.0.0.1:4723/wd/hub',self.caps)
def test_install_remove(self):
#is_app_installed验证是否存在app,存在返回true
if self.driver.is_app_installed('com.youdao.note'):
self.driver.remove_app('com.youdao.note')
self.driver.install_app(r'F:\python\app\youdaoyunbiji_84.apk')
def check_install(self):
self.caps['appPackage'] = 'com.youdao.note'
self.caps['appActivity'] = '.activity2.MainActivity'
self.driver = WebDriver('http://127.0.0.1:4723', self.caps)
#time.sleep(3)
#elresult = self.driver.find_element_by_id('com.android.packageinstaller:id/permission_deny_button').is_enabled()
#智能等待
# lambda匿名函数x,内容x.find_element_by_id('com.android.packageinstaller:id/permission_deny_button'),函数作用判断元素是否存在
elresult=+WebDriverWait(self.driver,10).until(lambda x:x.find_element_by_id('com.android.packageinstaller:id/permission_deny_button'))
if elresult:
print('ok')
else:
print('bad')
if __name__ == '__main__':
workobj=YD_install_remove()
workobj.test_install_remove()
workobj.check_install()
方法一:子类继承父类,修改属性
#兼容性测试,继承父类
from appium.webdriver.webdriver import WebDriver
import time
from youdaoproject.work1_install_remove import YD_install_remove
class YD_install_removeV2(YD_install_remove):
def __init__(self):
self.caps = {}
self.caps['automationName'] = 'UiAutomator2'
self.caps['platformName'] = 'Android'
def setdevice_6(self):
self.caps['platformVersion'] = '6.0'
self.caps['deviceName'] = '192.168.111.101:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver = WebDriver('htp://127.0.0.1:4723/wd/hub', self.caps)
def setdevice_7(self):
self.caps['platformVersion'] = '7.0'
self.caps['deviceName'] = '192.168.111.102:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher t2'
self.driver = WebDriver('htp://127.0.0.1:4723/wd/hub', self.caps)
if __name__ == '__main__':
YD_install_removeV2obj=YD_install_removeV2()
#验证6.0版本测试
YD_install_removeV2.setdevice_6()
YD_install_removeV2.test_install_remove()
YD_install_removeV2.check_install()
#验证7.0版本测试
YD_install_removeV2.setdevice_7()
YD_install_removeV2.test_install_remove()
YD_install_removeV2.check_install()
方法二:从csv文件读取属性,重新赋值对象属性
import csv
from appium.webdriver.webdriver import WebDriver
from youdaoproject.work1_install_remove import YD_install_remove
if __name__ == '__main__':
YD_install_remove_obj=YD_install_remove()
file=open('testpara','r')
table=csv.reader(file)
#对象的属性赋值
YD_install_remove_obj.caps={}
YD_install_remove_obj.caps['automationName']='UiAutomator2'
YD_install_remove_obj.caps['platformName'] = 'Android'
for row in table:
YD_install_remove_obj.caps[row[0]]=row[1]
YD_install_remove_obj.caps[row[2]] = row[3]
YD_install_remove_obj.caps[row[4]] = row[5]
YD_install_remove_obj.driver=WebDriver('http://127.0.0.1',YD_install_remove_obj.caps)
YD_install_remove_obj.test_install_remove()
YD_install_remove_obj.check_install()