How do I capture a screenshot if my nosetests fail?
Asked Answered
S

4

6

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?

Sugihara answered 20/2, 2013 at 22:35 Comment(1)
Similar, but for unittest: How to execute code only on test failures with python unittest2? at SOPhotoneutron
M
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()
Muddle answered 27/2, 2013 at 16:58 Comment(8)
The question is about the nose framework, not the default unittest.Trubow
"by using webdriver, python or nosetests features"Muddle
I don't think that "python" implies "using another framework (like unittest)", however I might be wrong.Trubow
Anyway, AFAIK in nose framework setUp and tearDown methods could be used to. I just showed the structure of test suit.Muddle
Nose will run tests written with the unittest package so there is no problem with this answerNosing
Doesn't work, in my case sys.exc_info()[0] returns <class 'Queue.Empty'> and so it still takes screenshot as it is not None. Value of sys.exc_info() --> (<class 'Queue.Empty'>, Empty(), <traceback object at 0x0000000002DEDA08>)Frye
@Deep, the answer is old enough. I use py.test for my tests now and capturing screenshot on fail is a little bit more tricky here. Provide some info on your tests and I'll try to help youMuddle
@Deep, let the necroposting begin! using sys.exc_info() is OS sensitive. The example provided works perfectly on Ubuntu, for exampleMuddle
T
6

First of all, webdriver has the command:

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

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".

In this addFailure(test, err) you can get the test name from Test object and generate the path for the file.

After that call 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.

Trubow answered 21/2, 2013 at 15:9 Comment(1)
I've tried this, but I can't get the instance of the TestCase in addFailure(). Could you share how that is possible (just by knowing the test name, which only points me to the appropriate class, not an instance)Brabazon
C
1

In Python you can use below code:

driver.save_screenshot('/file/screenshot.png')
Cropeared answered 21/2, 2013 at 4:44 Comment(0)
K
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:

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.

With this code, every time you want to interact with an element, you would call self.findelement('selector', 'element name')

Keener answered 9/2, 2015 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.