我开始学习自动化测试。 当我运行测试时,我想在 selenium NoSuchElementException 错误时获取特定文本(“无添加到购物车按钮”)
我正在尝试:
class TestCart:
def test_check_btn(self, browser):
browser.get(link)
browser.implicitly_wait(5)
assert browser.find_element(by=By.CSS_SELECTOR, value='.no-such-btn') is not None, 'No Add to Cart button'
这是真实的日志
=============================================================================== short test summary info ===============================================================================
FAILED test_cart/test_items.py::TestCart::test_check_btn - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css sele...
如何将错误消息替换为我想要的内容?
里面
conftest.py
。
import typing
if typing.TYPE_CHECKING:
import pytest
import _pytest._code.code
def pytest_runtest_logreport(report: 'pytest.TestReport'):
if report.failed:
try:
chain: '_pytest._code.code.ExceptionChainRepr' = report.longrepr.chain
repr_file_location: '_pytest._code.code.ReprFileLocation' = chain[-1][1]
repr_file_location.message = 'My Message: ' + repr_file_location.message[:32].title()
except:
import traceback
import sys
traceback.print_exc(file=sys.stderr)