我使用 Python 3.6.5 和以下库:
- Appium-Python-客户端==0.26
- 单元测试2==1.1.0
- 硒==3.5.0
- pytest==3.6.3
现在我需要截图以防测试失败,所以我故意做了一个错误的陈述self.driver.find_element_by_css_selector('test')
.
我在用sys.exc_info()
。但是当我使用命令执行以下代码时:py.test untitled.py
or python3 -m unittest untitled.py
它不捕获它。
Code:
import sys, time, unittest2
from selenium import webdriver
class FB360(unittest2.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_user_can_login(self):
self.driver.get('https://www.google.co.in')
self.driver.find_element_by_css_selector('test')
def tearDown(self):
print(sys.exc_info())
if sys.exc_info()[0]:
test_method_name = self._testMethodName
self.driver.save_screenshot(test_method_name + str(time.time()) + '.png')
self.driver.quit()
if __name__ == '__main__':
unittest2.main()
O/P:
(无,无,无)
E
=================================================== ===================
错误:test_user_can_login(无标题.FB360)
回溯(最近一次调用最后一次):
文件“/Volumes/Harry/Projects/pythonScreenshots/untitled.py”,第 11 行,位于 test_user_can_login
self.driver.find_element_by_css_selector('测试')
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”,第 498 行,位于 find_element_by_css_selector 中
返回 self.find_element(by=By.CSS_SELECTOR, value=css_selector)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”,第 832 行,在 find_element 中
'值': 值})['值']
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”,第297行,执行中
self.error_handler.check_response(响应)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py”,第 194 行,在 check_response 中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“css选择器”,“selector”:“test”}
(会话信息:chrome=67.0.3396.99)
(驱动程序信息:chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),平台=Mac OS X 10.13.5 x86_64)
我的立业之本:
-
print(sys.exc_info())
打印:(无,无,无)。这意味着即使我收到以下异常,我的测试用例也会通过:
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“css选择器”,“selector”:“test”}
- As
sys.exc_info())
返回 (None, None, None) 执行不会完成if
状况
所以我主要关心的是为什么sys.exc_info())
即使我收到 NoSuchElementException 也会返回 (None, None, None) ?
我用它来截取失败测试的屏幕截图
@pytest.fixture()
def browser(request):
options = Options()
options.add_argument("--headless") # Runs Chrome in headless mode.
_chrome = webdriver.Chrome(options=options)
failed_before = request.session.testsfailed
yield _chrome
if request.session.testsfailed != failed_before:
test_name = request.node.name
take_screenshot(_chrome, test_name)
_chrome.close()
def take_screenshot(browser, test_name):
screenshots_dir = "path_to/functional_tests/failure_screenshots"
screenshot_file_path = "{}/{}.png".format(screenshots_dir, test_name)
browser.save_screenshot(
screenshot_file_path
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)