Adding screenshot in test output for find failed error

Asked by Nidhi Bhushan on 2018-09-26

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:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2018-09-26
Last query:
2018-09-26
Last reply:
2018-09-26
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

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.