How to Catch Assertion Error in Try & Except Block

Hi All,

I am receiving an error: AssertionError: Elements states ‘[‘attached’, ‘defocused’, ‘editable’, ‘enabled’, ‘visible’]’ (list) should be ‘[‘detached’]’ (list)

In this case, I’m waiting for an element state to become detached but sometimes it is not and the Assertion Error appears in the Log which causes the Test Execution to stop immediately.
I’ve tried to catch this assertion error by using the below Try & Except:
TRY
Browser.Wait For Condition Element States ${Fetching_Quote_carrier} == detached timeout=01:00
EXCEPT AssertionError: * type=GLOB AS ${Error}
Log ${Error}
EXCEPT TimeoutError: * type=GLOB AS ${Error}
Log ${Error}
END
But this is not working.
Can anyone suggest this?

I had a problem catching the exact timeout error and ended up using GLOB with * on both sides of expected error message. Like this:

TRY
    Click    locator
EXCEPT    *Timeout 10000ms exceeded.*    type=GLOB
    Log    Timeout error caught, continuing the test
END

Source

Maybe you could try this in your test:

TRY
    Browser.Wait For Condition Element States ${Fetching_Quote_carrier} == detached timeout=01:00
EXCEPT    *AssertionError:*    type=GLOB    AS    ${Error}
    Log ${Error}
EXCEPT    *TimeoutError:*     type=GLOB    AS     ${Error}
    Log ${Error}
END
3 Likes

Thanks, @Ivbot but I have already tried this approach and observed that in the case of AssertionError, the Test Case execution is getting terminated immediately.