Handling alerts based on errors

Hi all,
I’m using RF 7.0 and Browser-library 18.5.1 and have the following issue:
When I make a wrong entry in a field and click on a button afterwards an alert windows appears.
With
${promise} = Promise To Wait For Alert action=accept
Click ${OM_REFRESH_BTN}
${text} = Wait For ${promise}
I can get the error message and pass it through to fail.
In case the entry in that field is correct there wouldn’t be an alert window but what would I have to do in this case?

Thanks a lot in advance for help!

Since you’re using RF7 maybe TRY/EXCEPT syntax is what you want? Something like this:

${promise} =    Promise To Wait For Alert    action=accept
Click    ${OM_REFRESH_BTN}
TRY
    ${text} =    Wait For    ${promise}
    Fail    ${text}
EXCEPT
    Log    No alert received
END

Dave.

2 Likes

Hi Dave,

thanks a lot for your help! I was expecting that the keyword “Fail” would stop the test case but it gone through the except branch as well and ended with “PASS”. What am I missing here?

Fail throw’s exception. But that Fail is inside try except block so the fail gets “caught” and you print out that “no alert received” so its a logic issue in your code.

Hi Dave,

I changed it now to
${promise} = Promise To Wait For Alert action=accept
TRY
Click ${OM_REFRESH_BTN}
Wait For Elements State ${VISIBLE}
EXCEPT
${text} = Wait For ${promise}
Fail ${text}
END

and it works now. Maybe you have an idea for a better solution which I would appreciate.
Thanks a lot in advance!

1 Like

Looks like you have the solution :+1: sorry I partially misled you with the fail inside the try block I am new to the try/except syntax in RF too. I’m happy your’ve resolved your issue though.

Dave.

Hi Dave,

I found a new little issue and hope you can help. Going through the TRY block successfully I get the warning that the “promise” is unresolved at the end of the test. Is there a way to stop this promise in the background?

I’m not really all that knowledgeable on Browser Library’s Promises, I only know what I read in the documentation.

I don’t see any keyword to stop a promise, only keywords to wait for them,

I would suggest trying to add Wait For All Promises as the last step in your test case, or even better in a teardown, but this will fail if a promise fails, if you just want to clean up and exit nicely maybe combine it with Run Keyword And Ignore Error or wrap it in it’s own try / except block

Dave.