APP自动化--异常截图1-利用作用域
pytest提供了异常抛出的接口,可以通过pytest抛出异常的时候截图
在conftest中,添加如下代码:
import pytest
from appium.webdriver import Remote # 导入远程包
from appium.webdriver.common.appiumby import AppiumBy as By # 引用appium的驱动常规AppiumBy 引入By模块,别名为BY
from selenium.webdriver.support.wait import WebDriverWait # 引入Selenium的WebDriverWait服务
from selenium.webdriver.support import expected_conditions as EC # 引入Selenium的expected_conditions服务别名为ec
from appium.webdriver.common.touch_action import TouchAction # 引入触摸事件方法导包为TouchAction
@pytest.fixture() # pytest夹具使用,pytest中构建供用例使用的数据
def driver():
caps = {}
caps["platformName"] = "Android"
caps["appium:deviceName"] = "emulator-5554"
caps["appium:platformVersion"] = "7.1.2"
caps["appium:appPackage"] = "com.zhao.myreader"
caps["appium:appActivity"] = "com.zhao.myreader.ui.home.MainActivity"
caps["appium:notReset"] = "True"
url = "127.0.0.1:4723/wd/hub"
global driver # 第一种,使用提升作用域完成(局限是当这个driver和下面蓝色部分不在一个文件中时是无法通用的)
driver = Remote(url, caps)
wait = WebDriverWait(driver, 2)
return driver, wait
@pytest.fixture() # pytest夹具使用,pytest中构建供用例使用的数据
def by():
return By # 这里的By是上面引入的AppiumBy包,为了避免循环递归,函数改为小写by
@pytest.fixture() # pytest夹具使用,pytest中构建供用例使用的数据
def ec():
return EC # 这里的By是上面引入的expected_conditions AS EC包,为了避免循环递归,函数改为小写by
@pytest.hookimpl(hookwrapper=True) # 写死的
def pytest_runtest_makereport(item, call): # 写死的
# pytest会将找到的用例存在迭代器中
outcome = yield # 通过这个方法接受所有用例的结果
result = outcome.get_result()
# print("用例的标识数据:", result)
if result.when == "call":
print("用例的执行结果:", result)
if result.outcome == "failed":
"""进行截图"""
driver.save_screenshot("异常截图.png") # driver可以全局变量driver来
def pytest_runtest_makereport(item, call): # 写死的
# pytest会将找到的用例存在迭代器中
outcome = yield # 通过这个方法接受所有用例的结果
result = outcome.get_result()
# print("用例的标识数据:", result)
if result.when == "call":
print("用例的执行结果:", result)
if result.outcome == "failed":
"""进行截图"""
driver.save_screenshot("异常截图.png") # driver可以全局变量driver来