简体   繁体   English

如果我的测试失败,如何捕获屏幕截图?

[英]How do I capture a screenshot if my nosetests fail?

I am running selenium webdriver tests with nosetests. 我用nosetests运行selenium webdriver测试。 I want to capture a screenshot whenever nosetests fail. 我想在鼻子测试失败时捕获截图。 How can I do it in the most effective way, either by using webdriver, python or nosetests features? 如何通过使用webdriver,python或nosetests功能以最有效的方式完成?

My solution 我的解决方案

import sys, unittest
from datetime import datetime

class TestCase(unittest.TestCase):

    def setUp(self):
        some_code

    def test_case(self):
        blah-blah-blah

    def tearDown(self):
        if sys.exc_info()[0]:  # Returns the info of exception being handled 
            fail_url = self.driver.current_url
            print fail_url
            now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')
            self.driver.get_screenshot_as_file('/path/to/file/%s.png' % now) # my tests work in parallel, so I need uniqe file names
            fail_screenshot_url = 'http://debugtool/screenshots/%s.png' % now
            print fail_screenshot_url
        self.driver.quit()

First of all, webdriver has the command: 首先,webdriver有以下命令:

driver.get_screenshot_as_file(screenshot_file_path)

I'm not an expert in nose (actually this is the first time I've looked into it), however I use py.test framework (which is similar, however superior over nose IMHO). 我不是鼻子方面的专家(实际上这是我第一次看到它),但是我使用了py.test框架(它类似于nose恕我直言)。

Mostly likely you'll have to create the "plugin" for nose where you'll have to implement the hook addFailure(test, err) which is "Called when a test fails". 很可能你必须为nose创建“插件” ,你必须实现钩子addFailure(test, err) ,这是“当测试失败时调用”。

In this addFailure(test, err) you can get the test name from Test object and generate the path for the file. 在这个addFailure(test, err)您可以从Test对象获取测试名称并生成文件的路径。

After that call driver.get_screenshot_as_file(screenshot_file_path) . 之后调用driver.get_screenshot_as_file(screenshot_file_path)

In py.test I create my plugin with implementation of def pytest_runtest_makereport(item, call): hook. py.test我使用def pytest_runtest_makereport(item, call):实现来创建我的插件def pytest_runtest_makereport(item, call): hook。 Inside I analyze call.excinfo and create the screenshot if necessary. 在内部我分析call.excinfo并在必要时创建屏幕截图。

在Python中,您可以使用以下代码:

driver.save_screenshot('/file/screenshot.png')

Perhaps you have set up your tests differently, but in my experience you need to manually build in this type of functionality and repeat it at the point of failure. 也许您已经以不同的方式设置了测试,但根据我的经验,您需要手动构建此类功能并在失败时重复它。 If you're performing selenium tests, chances are that like me, you're using a lot of find_element_by_ something . 如果执行Selenium测试,有机会,像我一样,你用了很多的东西 find_element_by_。 I've written the following function to allow me to tackle this type of thing: 我编写了以下函数来允许我处理这类事情:

def findelement(self, selector, name, keys='', click=False):

    if keys:
        try:
            self.driver.find_element_by_css_selector(selector).send_keys(keys)
        except NoSuchElementException:
            self.fail("Tried to send %s into element %s but did not find the element." % (keys, name))
    elif click:
        try:
            self.driver.find_element_by_css_selector(selector).click()
        except NoSuchElementException:
            self.fail("Tried to click element %s but did not find it." % name)
    else:
        try:
            self.driver.find_element_by_css_selector(selector)
        except NoSuchElementException:
            self.fail("Expected to find element %s but did not find it." % name)

In your case, the screenshot code (self.driver.get_screenshot_as_file(screenshot_file_path)) would go before the self.fail. 在您的情况下,屏幕截图代码(self.driver.get_screenshot_as_file(screenshot_file_path))将在self.fail之前。

With this code, every time you want to interact with an element, you would call self.findelement('selector', 'element name') 使用此代码,每次要与元素交互时,都会调用self.findelement('selector','element name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM