[英]How to capture screenshot on test case failure for Python Unittest
I am using Python 3.6.5 with the following libraries: 我正在将Python 3.6.5与以下库一起使用:
Now I need to capture the screenshot in case of test failure, So intentionally I did put a false statement self.driver.find_element_by_css_selector('test')
. 现在,在测试失败的情况下,我需要捕获屏幕快照,因此,我故意放置了一条错误的语句
self.driver.find_element_by_css_selector('test')
。
I am using sys.exc_info()
. 我正在使用
sys.exc_info()
。 But when I executing below code using a command: py.test untitled.py
or python3 -m unittest untitled.py
it does not capturing it. 但是,当我使用以下命令执行以下代码时:
py.test untitled.py
或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: O / P:
(None, None, None)
(无,无,无)
E
Ë
====================================================================== ERROR: test_user_can_login (untitled.FB360)
================================================== ===================错误:test_user_can_login(untitled.FB360)
Traceback (most recent call last): File "/Volumes/Harry/Projects/pythonScreenshots/untitled.py", line 11, in test_user_can_login self.driver.find_element_by_css_selector('test') File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 498, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element 'value': value})['value'] File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.
追溯(最近一次通话):文件“ /Volumes/Harry/Projects/pythonScreenshots/untitled.py”,第11行,位于test_user_can_login self.driver.find_element_by_css_selector('test')文件中,“ / Library / Frameworks / Python.framework /在find_element_by_css_selector中的版本/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py“中,行498返回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'value':value})['value'] File” /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py“,第297行,在执行self.error_handler.check_response(response)File” / Library / Frameworks / Python.framework / Versions / 3.6 / lib / python3.6 / site-packages / selenium / webdriver / remote / errorhandler.py“,第194行,在check_response中引发exception_class(消息,屏幕,stacktrace)selenium.common 。 exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"test"} (Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64)
exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“测试”}(会话信息:chrome = 67.0.3396.99)(驱动程序信息:chromedriver = 2.40 .565386(45a059dc425e08165f9a10324bd1380cc13ca363),平台= Mac OS X 10.13.5 x86_64)
My Foundings: 我的创立:
print(sys.exc_info())
prints: (None, None, None). print(sys.exc_info())
打印:(无,无,无)。 That means my test case is passed even if I am receiving below exception: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"test"}
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“测试”}
sys.exc_info())
returns (None, None, None) execution won't done under if
condition sys.exc_info())
返回(None,None,None)在if
条件下不会执行 So my major concern is why sys.exc_info())
returns (None, None, None) even if I am receiving NoSuchElementException ? 因此,我主要担心的是,即使我收到NoSuchElementException,为什么
sys.exc_info())
)也会返回(无,无,无)?
I use this to take screenshots of failed tests 我用它来拍摄失败的测试的屏幕截图
@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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.