You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The plan is to add support for native IF/ELSE syntax in RF 3.3 4.0. See the discussion in #3074 for background information why we think adding this kind of programmatic features is a good idea. Another similar and commonly needed feature is TRY/EXCEPT functionality to ease handling errors. Currently it is possible to use Run Keyword And Expect Error and Run Keyword And Ignore error but they are awkward similarly as Run Keyword If is an awkward replacement for IF/ELSE syntax.
Exact semantics how TRY/EXCEPT will work are not yet clear. They will be decided later before implementation starts. A precondition is a new parser, similarly as for IF/ELSE, and we also consider IF/ELSE more important and will start implementing it first.
Reacted by Brian Tsao, axi92, Antti Karjalainen, Janne Aukia, Kari Harju, Mikko Korpela, Cosmin Poieana, Markus and Ossi
At the moment we have already TRY/FINALLY with teststeps and Teardown.
Teardown can be on every level.
What is missing is only a [On-Error] setting.
If we would have this [On-Error] we could also do stuff like capture screenshots there and closing connections an so on.
I personally would not like to see a real TRY/EXCEPT like IF/ELSE, because the we start re-implementing Python.
[On-Error] is also understandable for non programmers.
*** Test Cases ***
Test
[Setup] Open Browser. http://example.com
Do Keyword
Some Other Test
[On-Error] Save error log and capture Screenshot
[Teardown] Close All Browsers
Same could work on Suite and on Keyword Level.
*** Settings ***
Suite Setup Open Browser
Error Handler Save error log and capture Screenshot
Suite Teardown Close All Browsers
I've been thinking about adding an on-error handler but nowadays I think TRY/EXCEPT would probably be better. Some of the problems I see with on-error handler:
Unless on-error can also ignore the error, it's not really different from teardown with Run Keyword If Test Failed.
If on-error should be able to ignore errors, it needed to get exception information as argument to be able to do decisions based on that.
One test (or keyword) could only have one on-error handler. If there were multiple possible errors to handle, the handler would get complicated.
I got a feeling TRY/EXCEPT would be a lot more generic solution. In simple cases on-error might be easier to understand, but in more complex scenarios TRY/EXCEPT would be a lot more flexible.
I also don't see problems in Robot's syntax moving towards a general very high level programming language. After replacing Run Keyword If with IF/ELSE and Run Keyword And Expect/Ignore Error with TRY/EXCEPT, we can look at replacing Exit/Continue For Loop with BREAK/CONTINUE, [Return] and Return From Keyword with RETURN, and so on.
Reacted by René, Ville Salonen and Antti Karjalainen
How does this relate to expecting an error to happen? In otherwords many cases in tests the situation is so that an error must happen as expected, test should fail if not.
One of the strongest selling points of Robot Framework is the simplicity for people coming from a non-IT-background. I think we should consider how easy a new core concept is to learn and how easy it is to teach.
TRY/EXCEPT goes some steps towards more complex concepts.
I do see usecases for Try/Except but these are mostly better done in python.
I always hate it when i see testspecs with try catch in it. People try to implement execution logic into test design.
@pekkaklarck you mentioned the "Run Keyword If Test Failed" this imho does not work on keyword level. But there are imho regular cases where you want to clean up or do stuff if something fails. And using TRY EXCEPT for this would imho be too technical. 🤷🏻♂️
This feature has been developed in a branch and is getting ready to be merged. The basic functionality works like this:
TRY
Some keyword
EXCEPT Error message
Error handler keyword
END
If Some keyword passes, the EXCEPT branch isn't run. If it fails with message Error message, the EXCEPT branch is executed. If it fails any other way, error is not handled. Any branch could naturally have more than one keyword.
There can be more than one EXCEPT branches. In that case they are matched one by one and the first one matching is executed. One EXCEPT can also have multiple messages to match. If it has no messages to match, it matches any:
TRY
Some keyword
EXCEPT Error message
Error handler 1
EXCEPT Alternative error
Error handler 2
EXCEPT Another error One more error # Match either of these
Error handler 3
EXCEPT # Match anything
Error handler 3
END
By default matching the error requires an exact match. That can be changed by prefixing the message with GLOB:, REGEXP: or STARTS: to make the match a glob pattern match, regular expression match, or to match only the beginning of the error, respectively. If you are interested in the actual error message, you can assign it to a variable similarly as you can use except Exception as err in Python:
TRY
Some keyword
EXCEPT GLOB: Error * AS ${err}
Log to console Ignoring error: ${err}
END
Assigning to a variable works also if you don't specify the message at all i.e. when all messages match:
TRY
Some keyword
EXCEPT AS ${message}
Log to console Error occurred: ${message}
END
It's possible to handle the case when there's no error using an ELSE branch like in Python:
TRY
Some keyword
EXCEPT Message
Error handler keyword
ELSE
Log to console No error!
END
Using ELSE without any EXCEPT branches is a syntax error.
Finally, it's possible to execute code both when there's an error and when there isn't by using a FINALLY branch:
TRY
Some keyword
EXCEPT AS ${err}
Log to console Error occurred: ${err}
ELSE
Log to console No error occurred.
FINALLY
Log to console Finally we are in FINALLY.
END
It is allowed to use FINALLY without EXCEPT branches. Such TRY/FINALLY structures can be used for cleaning up resources regardless are there failures or not:
Open connection
TRY
Use connection
FINALLY
Close connection
END
Although this feature is mostly ready, there are still some design decisions that are still somewhat open. I'm going to write about them in separate comments.
I reopen this due to a bug in parsing EXCEPT with AS with data_only=False. This bug was initially reported as #4208 by @d-biehl and there's PR #4209 by him to fix the problem as well.
We decided to add a support to limit how many times WHILE loops are executed to avoid endless loops. This limit is specified using limit=<value> like this:
WHILE ${condition} limit=1 minute
# ...
END
Now that we use option=value syntax with WHILE, it would be a good idea to use same syntax also with EXCEPT when controlling the pattern type. This is very late in RF 5.0 release cycle and we should already have the release out, but I believe it's better to change syntax still before the first release where TRY/EXCEPT is introduced than changing it later.
My thinking is that the pattern type would be specified using pattern option. If should follow the actual pattern or patterns, but be before the AS part. If there are multiple patterns, the pattern configuration would apply to them all. In practice it would work like this:
TRY
Keyword
EXCEPT Error: * pattern=glob
Handler 1
EXCEPT (Another|Second) Error Second [Pp]attern pattern=regexp AS ${error}
Handler 2
END
Things to decide:
Should we use pattern or something else like type?
Do we need to support matching only the start of the error message? We do that now, but just adding * at the end and using glob patterns is easy as well.
What exact value to use with regular expressions? Possible we could support both regex, and regexp.
What about case insensitivity? Should it be supported like pattern=case-insensitive or should we add another option that could be used like case_insensitive=True?
Activity
Snooz82 commentedon Apr 29, 2020
Just an idea:
At the moment we have already TRY/FINALLY with teststeps and Teardown.
Teardown can be on every level.
What is missing is only a [On-Error] setting.
If we would have this [On-Error] we could also do stuff like capture screenshots there and closing connections an so on.
I personally would not like to see a real TRY/EXCEPT like IF/ELSE, because the we start re-implementing Python.
[On-Error] is also understandable for non programmers.
Same could work on Suite and on Keyword Level.
pekkaklarck commentedon Apr 30, 2020
I've been thinking about adding an on-error handler but nowadays I think TRY/EXCEPT would probably be better. Some of the problems I see with on-error handler:
Run Keyword If Test Failed.I got a feeling TRY/EXCEPT would be a lot more generic solution. In simple cases on-error might be easier to understand, but in more complex scenarios TRY/EXCEPT would be a lot more flexible.
I also don't see problems in Robot's syntax moving towards a general very high level programming language. After replacing Run Keyword If with IF/ELSE and Run Keyword And Expect/Ignore Error with TRY/EXCEPT, we can look at replacing Exit/Continue For Loop with BREAK/CONTINUE, [Return] and Return From Keyword with RETURN, and so on.
mkorpela commentedon Sep 12, 2020
How does this relate to expecting an error to happen? In otherwords many cases in tests the situation is so that an error must happen as expected, test should fail if not.
pekkaklarck commentedon Sep 12, 2020
We could support
TRY/EXCEPT/ELSElike Python to support that use case.mkorpela commentedon Sep 12, 2020
One of the strongest selling points of Robot Framework is the simplicity for people coming from a non-IT-background. I think we should consider how easy a new core concept is to learn and how easy it is to teach.
TRY/EXCEPT goes some steps towards more complex concepts.
Snooz82 commentedon Sep 12, 2020
I do see usecases for Try/Except but these are mostly better done in python.
I always hate it when i see testspecs with try catch in it. People try to implement execution logic into test design.
@pekkaklarck you mentioned the "Run Keyword If Test Failed" this imho does not work on keyword level. But there are imho regular cases where you want to clean up or do stuff if something fails. And using TRY EXCEPT for this would imho be too technical. 🤷🏻♂️
aikarjal commentedon Sep 10, 2021
+1 for this feature. There are valid use-cases for this in RPA. Right now the only way to handle errors is awkward
Run Keyword And Expect Errorkariharju commentedon Sep 10, 2021
Definitely coming more and more of an issue, so nice to see this as critical v5 content.
pekkaklarck commentedon Dec 11, 2021
This feature has been developed in a branch and is getting ready to be merged. The basic functionality works like this:
If
Some keywordpasses, theEXCEPTbranch isn't run. If it fails with messageError message, theEXCEPTbranch is executed. If it fails any other way, error is not handled. Any branch could naturally have more than one keyword.There can be more than one
EXCEPTbranches. In that case they are matched one by one and the first one matching is executed. OneEXCEPTcan also have multiple messages to match. If it has no messages to match, it matches any:By default matching the error requires an exact match. That can be changed by prefixing the message with
GLOB:,REGEXP:orSTARTS:to make the match a glob pattern match, regular expression match, or to match only the beginning of the error, respectively. If you are interested in the actual error message, you can assign it to a variable similarly as you can useexcept Exception as errin Python:Assigning to a variable works also if you don't specify the message at all i.e. when all messages match:
It's possible to handle the case when there's no error using an
ELSEbranch like in Python:Using
ELSEwithout anyEXCEPTbranches is a syntax error.Finally, it's possible to execute code both when there's an error and when there isn't by using a
FINALLYbranch:It is allowed to use
FINALLYwithoutEXCEPTbranches. SuchTRY/FINALLYstructures can be used for cleaning up resources regardless are there failures or not:Although this feature is mostly ready, there are still some design decisions that are still somewhat open. I'm going to write about them in separate comments.
35 remaining items
pekkaklarck commentedon Jan 30, 2022
I reopen this due to a bug in parsing EXCEPT with AS with
data_only=False. This bug was initially reported as #4208 by @d-biehl and there's PR #4209 by him to fix the problem as well.pekkaklarck commentedon Jan 31, 2022
The reported bug has now been fixed.
pekkaklarck commentedon Mar 6, 2022
We decided to add a support to limit how many times WHILE loops are executed to avoid endless loops. This limit is specified using
limit=<value>like this:Now that we use
option=valuesyntax with WHILE, it would be a good idea to use same syntax also with EXCEPT when controlling the pattern type. This is very late in RF 5.0 release cycle and we should already have the release out, but I believe it's better to change syntax still before the first release where TRY/EXCEPT is introduced than changing it later.pekkaklarck commentedon Mar 6, 2022
My thinking is that the pattern type would be specified using
patternoption. If should follow the actual pattern or patterns, but be before theASpart. If there are multiple patterns, thepatternconfiguration would apply to them all. In practice it would work like this:Things to decide:
patternor something else liketype?*at the end and using glob patterns is easy as well.regex, andregexp.pattern=case-insensitiveor should we add another option that could be used likecase_insensitive=True?pekkaklarck commentedon Mar 9, 2022
Decisions related to the new syntax to specify the pattern type:
type.start.regexp. There's no alias.pekkaklarck commentedon Mar 9, 2022
New pattern type syntax was implemented in bb4b01e. Documentation still needs to be updated but otherwise this ought to be done (again).
doc: changes to try-except patterns
Smallish EXCEPT pattern fixes (#3075).
pekkaklarck commentedon Mar 9, 2022
Documentation and few more smallish fixes done in above commit. Closing.