我们用的 pytest,在 conftest 里先初始化 driver
@pytest.fixture(scope='function')
def driver():
# setUp
get_driver = DriverConfig().driver_config()
yield get_driver
# tearDown
LoginPage().logout(get_driver)
get_driver.quit()
然后在测试用例里传入 driver
class TestAppEngineDF001:
@user2e
@pytest.mark.needUI
@allure.feature("AppEngine")
@allure.story("AppEngine.DF")
def test_df_001(self, driver):
# 用例名称
case_name = '/autotest/appengine/DF/001'
用例里再调用 page 类,driver 是传递下去的
with allure.step("2、点击添加按钮"):
ViewPage().view_click_operation_button(driver, '添加')
一直传到 selenium 的二次封装那部分.
def element_click(self, driver, locate_type, locator_expression, wait_for_locate_type=None,
wait_for_locator_expression=None, wait_for_disappear_locate_type=None,
wait_for_disappear_locator_expression=None, timeout=30):
"""
元素点击
:param driver: 浏览器驱动
:param locate_type: 定位方式类型
:param locator_expression: 定位表达式
:param wait_for_locate_type: 等待元素出现的元素定位方式类型
:param wait_for_locator_expression: 等待元素出现的元素定位表达式
:param wait_for_disappear_locate_type:等待元素消失的元素定位方式类型
:param wait_for_disappear_locator_expression:等待元素消失的元素定位表达式
:param timeout:超时时间
:return:
"""
# 元素要可见
element = self.element_visibility_appear(driver=driver, locate_type=locate_type,
locator_expression=locator_expression,
timeout=timeout)
try:
# 点击元素
element.click()
except(StaleElementReferenceException, ENI_Exception):
self.wait_for_ready_state_complete(driver=driver)
time.sleep(0.05)
element = self.element_visibility_appear(driver=driver, locate_type=locate_type,
locator_expression=locator_expression,
timeout=timeout)
element.click()
except Exception as e:
print("页面出现异常,元素不可点击", e)
return False
try:
# 点击元素后的元素出现或元素消失
self.element_visibility_appear(driver, wait_for_locate_type, wait_for_locator_expression)
self.element_visibility_disappear(driver, wait_for_disappear_locate_type,
wait_for_disappear_locator_expression)
except Exception as e:
print("等待元素消失或出现失败", e)
return False
return True