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

时间:2022-07-11 19:18:11

I am running selenium webdriver tests with nosetests. 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?

我用nosetests运行selenium webdriver测试。我想在鼻子测试失败时捕获屏幕截图。如何通过使用webdriver,python或nosetests功能以最有效的方式完成?

4 个解决方案

#1


8  

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

#2


5  

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框架(它类似,但不如鼻子恕我直言)。

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. Inside I analyze call.excinfo and create the screenshot if necessary.

在py.test中,我创建了我的插件,实现了def pytest_runtest_makereport(item,call):hook。在内部我分析call.excinfo并在必要时创建屏幕截图。

#3


0  

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. I've written the following function to allow me to tackle this type of thing:

也许你已经以不同的方式设置了测试,但根据我的经验,你需要手动构建这种类型的功能并在失败时重复它。如果你正在进行硒测试,很可能像我一样,你使用了很多find_element_by_something。我已经编写了以下函数来允许我处理这类事情:

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

#4


0  

In Python you can use below code:

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

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

#1


8  

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

#2


5  

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框架(它类似,但不如鼻子恕我直言)。

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. Inside I analyze call.excinfo and create the screenshot if necessary.

在py.test中,我创建了我的插件,实现了def pytest_runtest_makereport(item,call):hook。在内部我分析call.excinfo并在必要时创建屏幕截图。

#3


0  

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. I've written the following function to allow me to tackle this type of thing:

也许你已经以不同的方式设置了测试,但根据我的经验,你需要手动构建这种类型的功能并在失败时重复它。如果你正在进行硒测试,很可能像我一样,你使用了很多find_element_by_something。我已经编写了以下函数来允许我处理这类事情:

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

#4


0  

In Python you can use below code:

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

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