【问题标题】:How to capture screenshot on test case failure for Python Unittest如何为 Python Unittest 捕获测试用例失败的屏幕截图
【发布时间】:2018-07-20 16:05:02
【问题描述】:

我正在使用带有以下库的 Python 3.6.5:

  • Appium-Python-Client==0.26
  • unittest2==1.1.0
  • 硒==3.5.0
  • pytest==3.6.3

现在我需要截图以防测试失败,所以我故意放了一个错误的陈述 self.driver.find_element_by_css_selector('test').

我正在使用sys.exc_info()。但是当我使用命令执行以下代码时:py.test untitled.pypython3 -m unittest untitled.py 它不会捕获它。

代码:

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 (untitled.FB360)


Traceback(最近一次调用最后一次): 文件“/Volumes/Harry/Projects/pythonScreenshots/untitled.py”,第 11 行,在 test_user_can_login self.driver.find_element_by_css_selector('test') 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”,第 498 行,在 find_element_by_css_selector return 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:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“测试”} (会话信息:chrome=67.0.3396.99) (驱动信息:chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64)

我的创立:

  • print(sys.exc_info()) 打印:(无,无,无)。这意味着即使我收到以下异常,我的测试用例也通过了:

selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"test"}

  • sys.exc_info()) 返回 (None, None, None) 在if 条件下不会执行

所以我主要关心的是为什么 sys.exc_info()) 会返回 (None, None, None) 即使我收到 NoSuchElementException ?

【问题讨论】:

  • sys.exc_info() 技巧在 Python 3 中不起作用,因此请跳过链接问题中接受的答案并通读其余部分。使用pytests 固定装置可以获得更好的解决方案,但看起来您正在编写纯unittest 测试并且仅使用pytest 作为测试运行器。
  • 谢谢。是的,正是我编写了纯单元测试并使用 pytest 作为测试运行程序。你能给我推荐一个最好的 Python3 库来用 selenium 制作 UI 测试用例吗?
  • 我认为任何库都不会取代selenium;大多数围绕测试的例行代码都过于特定于项目。

标签: python selenium-webdriver python-3.6 pytest python-unittest


【解决方案1】:

我用它来截取失败测试的屏幕截图

@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
    )

【讨论】:

  • 测试必须使用相同的夹具进行浏览,对吧?否则我会得到所有空白屏幕截图。
猜你喜欢
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多