pytest+allure用例执行失败后截图
时间: 2023-10-06 17:04:41 浏览: 157
Airtest+Pytest+Allure框架demo
5星 · 资源好评率100%
作为AI语言模型,我无法在计算机上执行任何操作,因此无法提供pytest allure用例执行失败后截图。但是,以下是如何在使用pytest allure时自动截取用例执行失败截图的示例代码:
```python
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope="function")
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
@allure.step("Open website '{url}'")
def open_website(browser, url):
browser.get(url)
@allure.step("Click on element '{locator}'")
def click_element(browser, locator):
element = browser.find_element(*locator)
element.click()
@allure.step("Verify element '{locator}' is present")
def verify_element_present(browser, locator):
assert browser.find_element(*locator).is_displayed()
@allure.step("Enter text '{text}' into element '{locator}'")
def enter_text(browser, locator, text):
element = browser.find_element(*locator)
element.send_keys(text)
@allure.step("Verify text '{text}' is present in element '{locator}'")
def verify_text_present(browser, locator, text):
assert text in browser.find_element(*locator).text
def test_login_failure(browser):
with allure.step("Given the user is on the login page"):
open_website(browser, "https://www.example.com/login")
with allure.step("When the user enters invalid credentials"):
enter_text(browser, (By.ID, "username"), "invalid_username")
enter_text(browser, (By.ID, "password"), "invalid_password")
click_element(browser, (By.NAME, "login_button"))
with allure.step("Then an error message is displayed"):
verify_element_present(browser, (By.CSS_SELECTOR, ".error_message"))
verify_text_present(browser, (By.CSS_SELECTOR, ".error_message"), "Invalid username or password")
# Take screenshot on failure
allure.attach(
browser.get_screenshot_as_png(),
name="Screenshot",
attachment_type=allure.attachment_type.PNG,
)
```
在测试用例失败时,pytest allure将自动捕获失败的截图并将其添加到测试报告中。
阅读全文