一、定义fixture方法
# -*- coding:utf-8 -*-
import pytest
from baseutil.DriverUtil import DriverConfig
@pytest.fixture
def driver_handle():
driver = DriverConfig().get_driver() # get_driver()中是设备建立连接的配置
yield driver
driver.close_app()
driver.quit()
二、测试方法中传入driver
@pytest.mark.flaky(reruns=2, reruns_delay=2)
@pytest.mark.usefixtures("driver_handle")
def test_user_register(self, driver_handle):
self.driver = driver_handle
此时使用self.driver.find_element(By.ID,"id").click() 就可以正常使用了
三、get_driver()内容
class DriverUtil:
def get_driver(self):
'''获取driver'''
try:
desired_caps = {}
desired_caps['platformName'] = 'Android' # 平台
desired_caps['Version'] = '9.0.0' # 系统版本
desired_caps['appPackage'] = 'aaa.bbb.ccc' # APK包名
desired_caps['appActivity'] = '.ui.StartActivity' # 被测程序启动时的Activity
desired_caps['unicodeKeyboard'] = 'true' # 是否支持unicode的键盘。如果需要输入中文,要设置为“true”
desired_caps['resetKeyboard'] = 'true' # 是否在测试结束后将键盘重轩为系统默认的输入法。
desired_caps['newCommandTimeout'] = '120' # Appium服务器待appium客户端发送新消息的时间。默认为60秒
desired_caps['deviceName'] = 'P7CDU1833333' # 手机ID
desired_caps['automationName'] = 'Uiautomator2' # 启动Uiautomator2获取toast
desired_caps['noReset'] = True # true:不重新安装APP,false:重新安装app
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
return driver
四、调用fixture方法中的变量
@pytest.fixture
def init_car_data():
fo = FileOperate()
car_info = fo.get_first_line_content("carInfo")
at = AppTools()
company_name_list = at.get_company(car_info[0])
return car_info,company_name_list
@pytest.mark.usefixtures("init_car_data")
def test_carinfo10(self,driver_handle,init_car_data):
"""用例描述:车辆信息10"""
car_info = init_car_data[0]
company_name_list = init_car_data[1]