Adding screenshot in test output for find failed error

Asked by Nidhi Bhushan

I am using HTMLTestRunner to generate html output reports for my tests. I want screenshot in my output report for find failed errors.
My FindElement function looks like below:

def findElement( element ):
        print "Finding element: " + str(element)
        try:

                find(element)
        except FindFailed:
                stack = traceback.extract_stack(limit = 2)
                print "Unable to find element: " + Constants.BaselineFolder + str(element) + "\nBelow are exception details:\n" + str(sys.exc_info()[0]) + " -- line no. " + str(stack[0][1])
                raise

But if any element is not found during execution, then screenshot is not getting displayed in output report.
Although, screenshot appears properly in case of assert failures for below assert function:

def assertElementExists( element ):
        print "Asserting whether element exists: " + str(element)
        try:

                assert(exists(element))
        except AssertionError:
                stack = traceback.extract_stack(limit = 2)
                print "Unable to assert image exists: " + Constants.BaselineFolder + str(element) + "\nBelow are exception details:\n" + str(sys.exc_info()[0]) + " -- line no. " + str(stack[0][1])
                raise

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

The screenshot feature is only available for failed asserts.

hence you have to use
assert false

instead of
raise

BTW:
       try:

                assert(exists(element))
        except AssertionError:
                stack = traceback.extract_stack(limit = 2)
                print "Unable to assert image exists: " + Constants.BaselineFolder + str(element) + "\nBelow are exception details:\n" + str(sys.exc_info()[0]) + " -- line no. " + str(stack[0][1])
                raise

does not make sense, since the assertion exceptions are caught by unitttest anyways

if !exists(element):
                stack = traceback.extract_stack(limit = 2)
                print "Unable to assert image exists: " + Constants.BaselineFolder + str(element) + "\nBelow are exception details:\n" + str(sys.exc_info()[0]) + " -- line no. " + str(stack[0][1])
                assert false

Revision history for this message
Nidhi Bhushan (nidhibhushan) said :
#2

Thank you very much Raiman. Appreciate the quick response. This answered my question. I have made the mentioned changes in my script.