- 🍓学习目标:通过有道云项目的实践来熟悉appium的使用
- 💘如有不足,还望指出
- 💞若觉不错,希望不吝小手点赞、关注支持一波
目录
一、使用面向对象的思想实现一个模拟器安装卸载的自化测试v1.0
一、使用面向对象的思想实现一个模拟器安装卸载的自化测试v1.0
环境:
- Appium Desktop:1.22.2
- 模拟器:Genymotion 3.2.1
- 有道云app:youdaoyunbiji_84.apk(Note:版本不能过高,因为安卓版本只有6.0,apk版本过高会导致不兼容。)
- 模拟型号:Android
- 模拟版本:6.0
1.1、定义初始化方法
将appium的基础参数设置放在初始化方法init中。
- automationName:自动化测试引擎,默认为UiAutomator2
- platformName及platformVersion:为我们创建的模拟器型号及版本
- deviceName:连接模拟器之后用'adb device'命令获取
- appPackage及appActivity:由于还未装app,所以我们可以停留在手机的主界面然后使用'adb shell dumpsys activity activities | findstr mFocusedActivity' 获取。
初始化代码如下:
def __init__(self):
# 定义一个字典类型,存放参数设置
self.caps = {
'automationName': 'UiAutomator2',
'platformName': 'Android',
'platformVersion': '6.0',
'deviceName': '192.168.23.101:5555',
'appPackage': 'com.android.launcher3',
'appActivity': '.Launcher'}
# 连接appium服务器,并传入参数
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
1.2、定义安装卸载方法
- 安装app可以使用driver.install_app('apk路径')
- 卸载app可以使用driver.remove_app('app包名')
- 卸载之前还应该判断一下app是否已安装,如果已安装就卸载,如果未安装就不用执行卸载的程序了,可以使用driver.is_app_installed('app包名'),该方法当已安装时返回值为True.
安装卸载代码如下:
def test_install_remove(self):
# 卸载有道云app
if self.driver.is_app_installed('com.youdao.note'): # 判断是否已安装,若安装返回True
self.driver.remove_app('com.youdao.note')
# 安装有道云app
self.driver.install_app(r'D:\PythonTest\AppiumTest\YouDaoYun\youdaoyunbiji_84.apk')
1.3、定义检查安装是否成功方法
- 安装好有道云app之后,启动有道云app进到它的主界面使用'adb shell dumpsys activity activities | findstr mFocusedActivity'首先获取它的包名及当前所处活动界面。
- 首次启动有道云的时候会有一个权限提示窗口,我们可以通过抓取上面的元素来判断有道云app是否被成功安装上了
- 第一种方法是使用元素是否存在方法driver.find_element(By.ID, '元素ID').is_enabled(),当元素存在时,它的返回值是True。有加载问题,可在前面加上时间等待time.sleep(3)方法。
- 第二种方法是使用显式等待WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, '元素ID')) 。最大等待时间10s,找到指定元素才会进行下一步操作。
检查代码如下:
def check_install(self):
# S1:安装后用程序启动有道云
self.caps['appPackage'] = 'com.youdao.note'
self.caps['appActivity'] = '.activity2.MainActivity'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
# S2:弹出界面上抓取’拒绝‘链接
# el = self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_deny_button').is_enabled()
# 添加显式等待
el = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_deny_button'))
print(el)
# S3:如果链接存在,那么打印“安装成功”
# S4:否则打印安装失败
if el:
print('安装成功')
else:
print('安装失败')
1.4、调用方法
# 用面向对象的思想实现一个模拟器安装卸载的自动化测试
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
class yd_install_removeV1:
def __init__(self):
# 定义一个字典类型,存放参数设置
self.caps = {
'automationName': 'UiAutomator2',
'platformName': 'Android',
'platformVersion': '6.0',
'deviceName': '192.168.23.101:5555',
'appPackage': 'com.android.launcher3',
'appActivity': '.Launcher'}
# 连接appium服务器,并传入参数
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
def test_install_remove(self):
# 卸载有道云app
if self.driver.is_app_installed('com.youdao.note'): # 判断是否已安装,若安装返回True
self.driver.remove_app('com.youdao.note')
# 安装有道云app
self.driver.install_app(r'D:\PythonTest\AppiumTest\YouDaoYun\youdaoyunbiji_84.apk')
def check_install(self):
# S1:安装后用程序启动有道云
self.caps['appPackage'] = 'com.youdao.note'
self.caps['appActivity'] = '.activity2.MainActivity'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
# S2:弹出界面上抓取’拒绝‘链接
# el = self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_deny_button').is_enabled()
# 添加显式等待
el = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_deny_button'))
print(el)
# S3:如果链接存在,那么打印“安装成功”
# S4:否则打印安装失败
if el:
print('安装成功')
else:
print('安装失败')
if __name__ == '__main__':
# 初始化类的对象
yd_install_removeV1_obj = yd_install_removeV1()
# 调用卸载安装
yd_install_removeV1_obj.test_install_remove()
# 调用检查安装
yd_install_removeV1_obj.check_install()
二、实现两个模拟器的安装卸载的兼容性测试v2.0
可以继承1.0的安装卸载方法以及检查方法,本文运用的还是串联方法,没有用到并行测试。
2.1、定义安卓6.0的参数
6.0的参数和上面没变化,只是把6.0独有的参数单独写到方法中了,而共有的参数还是放在初始化方法中,代码如下:
# 安卓6.0的参数
def setdevice_6(self):
self.caps['platformVersion'] = '6.0'
self.caps['deviceName'] = '192.168.23.101:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
2.2、定义安卓7.1的参数
模拟器上再添加一个安卓7.1的手机,然后启动使用命令获取参数,代码如下:
# 安卓7.1的参数
def setdevice_7(self):
self.caps['platformVersion'] = '7.1'
self.caps['deviceName'] = '192.168.23.103:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
2.3、调用方法
# V2.0 实现两个模拟器设置安装卸载的兼容性测试
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
# 继承导入v1.0类
from AppiumTest.YouDaoYun.work1_nstall_remove import yd_install_removeV1
class yd_install_removeV2(yd_install_removeV1):
def __init__(self):
self.caps = {
'automationName': 'UiAutomator2',
'platformName': 'Android'}
# 安卓6.0的参数
def setdevice_6(self):
self.caps['platformVersion'] = '6.0'
self.caps['deviceName'] = '192.168.23.101:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
# 安卓7.1的参数
def setdevice_7(self):
self.caps['platformVersion'] = '7.1'
self.caps['deviceName'] = '192.168.23.103:5555'
self.caps['appPackage'] = 'com.android.launcher3'
self.caps['appActivity'] = '.Launcher'
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
if __name__ == '__main__':
yd_install_removeV2_obj = yd_install_removeV2()
# 6.0版本的测试
yd_install_removeV2_obj.setdevice_6()
yd_install_removeV2_obj.test_install_remove()
yd_install_removeV2_obj.check_install()
# 7.1版本的测试
yd_install_removeV2_obj.setdevice_7()
yd_install_removeV2_obj.test_install_remove()
yd_install_removeV2_obj.check_install()
三、通过文件读取参数实现任意设备的兼容性测试v3.0
由2.0的方法可以看出来不同模拟器只是参数值不同,但是参数名是相同的,所以可以将参数放入csv文件,通过读取csv文件来实现任意设备的测试。
csv文件如下:
由于不需要添加新方法,所以我们可以直接写main函数调用v1.0的类.
代码如下:
# V3.0 实现任意设备的兼容性测试
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver
import csv
# 导入V1.0相关内容
from AppiumTest.YouDaoYun.work1_nstall_remove import yd_install_removeV1
if __name__ == '__main__':
yd_install_removeV3_obj = yd_install_removeV1()
file1 = open('testpara.csv', 'r')
file2 = open('testpara_result.csv', 'w', newline='')
table = csv.reader(file1)
writer = csv.writer(file2)
yd_install_removeV3_obj.caps = {
'automationName': 'UiAutomator2',
'platformName': 'Android'}
for row in table:
yd_install_removeV3_obj.caps[row[0]] = row[1]
yd_install_removeV3_obj.caps[row[2]] = row[3]
yd_install_removeV3_obj.caps[row[4]] = row[5]
yd_install_removeV3_obj.caps[row[6]] = row[7]
yd_install_removeV3_obj.driver = WebDriver('http://127.0.0.1:4723/wd/hub', yd_install_removeV3_obj.caps)
yd_install_removeV3_obj.test_install_remove()
yd_install_removeV3_obj.check_install()
writer.writerow([row[3], '安装成功'])
file1.close()
file2.close()