Appium系列分享
Appium自动化(七)通过脚本自动化获取apk的包名和对应启动activity
Appium自动化(八)通过脚本自动化获取设备deviceName和platformVersion
Appium自动化(十二)Appium+unittest组织测试用例
Appium系列(十七)将Appium服务端口号通过参数传递给测试用例
前言
在之前分享的复盘中,我们找到了框架的优缺点,那么在后续的分享中呢,会对现在存在的问题,进行逐一的优化,打造一个高适配性的测试框架。
正文
我们把要执行的的apk通过配置文件来管理apk。那么我们可以去创建一个config.py来管理,配置如下
apk_path = "apk/iBiliPlayer-bili.apk"
那么接下来,就是我们在代码中去引用即可。
from config import apk_path
同样的,我们也可以把元素定位的这么维护起来。
login_element='data/element/loginelement.yaml'
代码中的修改入下
element=getyaml(login_element)
因为后期要考虑到拓展到对多类型的兼容,我们这里也需要改写desired_caps。
我们将这里的提出来,然后在run文件中传入,修改方式如下。
from common.adbtool import *
from common.packageparse import get_apk_lautc,get_apkname
from multiprocessing import Pool
import unittest,random
from common.appiumserveruntil import start,stop_server
from case.logintestcase import testCase
from config import apk_path
def runnerPool(getDevices):
'''
根据链接的设备生成不同的dict
然后放到设备的list里面
设备list的长度产生进程池大小
'''
devices_Pool = []
for i in range(0, len(getDevices)):
_pool = []
_initApp = {}
device=getDevices[i]
apkname=get_apkname(apk_path)
desired_caps = {
'platformName': 'Android',
'deviceName':device['devices'] , # adb deivces
'platformVersion': getPlatForm(getDevices[i]['devices']), # 从设置中可以获取
'appPackage': apkname , # 包名
'udid': getDevices[i]['devices'],
'appActivity': get_apk_lautc(apk_path), # apk的launcherActivity
# 'skipServerInstallation': True
}
_initApp['deviceName']=device['devices']
_initApp["port"] = device["port"]
_initApp['appPackage']=apkname
_initApp['desired_caps']=desired_caps
_pool.append(_initApp)
devices_Pool.append(_initApp)
pool = Pool(len(devices_Pool))
pool.map_async(runnerCaseApp,devices_Pool)
pool.close()
pool.join()
对应的,我们还要处理logintestcase.py,优化后的文件如下
from time import sleep
from common.adbtool import *
from ddt import ddt,file_data
from appium import webdriver
from common.pyyaml import getyaml
from common.parame import Parmer
from selenium.common.exceptions import NoSuchElementException
from config import apk_path,login_element
element=getyaml(login_element)
@ddt
class testCase(Parmer):
def setUp(self) -> None:
print(self.parme)
is_first_install = False
# 1.判断是否安装app
is_install = isinstallapk(self.parme['appPackage'], self.parme['deviceName'])
if is_install is False:
# 2.如果没有安装,则安装
installapk(apk_path, self.parme['deviceName'])
is_first_install = True
desired_caps=self.parme['desired_caps']
self.driver = webdriver.Remote('http://0.0.0.0:%s/wd/hub'%str(self.parme['port']), desired_caps)
sleep(10)
# 启动同意用户协议
agree=element['agreeelement']
self.driver.find_element_by_id(agree).click()
if is_first_install:
# 首次安装需要加载文件
sleep(50)
sleep(11)
self.driver.find_element_by_id(element['opearelement']).click()
sleep(5)
try:
self.driver.find_element_by_xpath(element['logintextclik']).click()
except NoSuchElementException as e:
print(e)
def tearDown(self) -> None:
self.driver.quit()
sleep(20)
@file_data("../data/casedata/logincase.yaml")
def testlogin(self,user,user_password,msg):
sleep(10)
username = self.driver.find_element_by_id(element['usernameedit'])
username.clear()
username.send_keys(user)
password = self.driver.find_element_by_id(element['passwordedit'])
password.clear()
password.send_keys(user_password)
login = self.driver.find_element_by_id(element['loginbtn'])
login.click()
try:
self.driver.find_element_by_id(element['assertelement'])
self.assertTrue(False,msg=msg)
except:
self.assertTrue(True,msg=msg)
如果觉得这篇文章还不错,来个【分享、点赞、在看】三连吧,让更多的人也看到~