I want ignore the failure of keyword based on condition and continue to next keyword to already existing huge set of test cases and suits

Viewed 171

I know we can write a listener to run the keyword when there is a failure in the keyword by checking the status of the keyword in the listener. But one of my requirements apart from looking at the Status, I want to look at the error message also before I run the keyword.

RunListner.py

def _end_keyword(self, name, attributes):
    if attributes['status'] == 'FAIL':

The above portion of code in the listeners will help me to check the status of Keyword, but no way I can check the reason for the failure of the keyword. Is there something like this possible?

def _end_keyword(self, name, attributes):
        if attributes['status'] == 'FAIL' and ErrorMessage==""something went wrong"" :

At present I don't have idea how to pass on the error information to listner .

RobotCode

*** Settings ***
    Documentation    Suite description
    Library          RunListner
    Test Template   Run Keyword And Ignore Error
    Suite Setup    Set Keyword To Run On Failure    Log Many    1    2   3
    
*** Test Cases ***
Test title
    log   step one
    log   step two
    fail  something went wrong
    fail  something else went wrong
    log   last step

enter image description here

As you can see in the screenshot the failure reason is "something went wrong" is one of failure and the other is "Something else went wrong". I want to call listener only when "something went wrong"

1 Answers

For exactly the case of the Fail keyword, you can get that from its 'args' key - this keyword does one thing, and that is to fail with a message - so it's argument will be what you can compare with.

But that (logic) will be applicable only for the Fail keyword; for example, in this:

Should Be True      1 == 2

, the value of the 'args' is ['1 == 2'] - not very usable.

There's an alternative, though - you may go after the log_message listener, and intercept the "FAIL" level:

    def _log_message(self, message):
        if message['level'] == 'FAIL':
            print(f'inside listener: {message}')

These are logged on failures (duh :), and there's a key holding the message itself; in the case of the "should be true" example below, it looks like this:
'message': "'1 == 2' should be true."

E.g. the final message one would see in the log - and more or less, what you are after in your approach.

Related