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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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:
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')
My solution
First of all, webdriver has the command:
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 overnose
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 ofdef pytest_runtest_makereport(item, call):
hook. Inside I analyzecall.excinfo
and create the screenshot if necessary.In Python you can use below code: