-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
While loops and try/except examples #3
Open
falk7
wants to merge
7
commits into
robotframework:gh-pages
Choose a base branch
from
imbus:gh-pages
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
afe923c
fixed 2_lists.robot
falk7 ef388a9
fixed for loops
falk7 bdd9e0f
While Examples
falk7 ae7eaed
added while example to index.json
falk7 6bab3ec
Try Except
falk7 fef02b2
Typos
falk7 aadb272
implemented renes comments
falk7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
*** Settings *** | ||
Library Collections | ||
|
||
*** Variables *** | ||
@{First Name} Bruce Steve Peter | ||
@{Last Name} Banner Rogers Parker | ||
@{first_name} Bruce Steve Peter | ||
@{last_name} Banner Rogers Parker | ||
|
||
*** Test Cases *** | ||
Combines two loops | ||
Combines two lists | ||
Log To Console Full Names: | ||
FOR ${first} ${last} IN ZIP ${First Name} ${Last Name} | ||
FOR ${first} ${last} IN ZIP ${first_name} ${last_name} | ||
Log To Console ${first} ${last} | ||
END | ||
|
||
Combines two lists of different lengths | ||
Append To List ${first_name} Wade | ||
Log To Console Full Names: | ||
FOR ${first} ${last} IN ZIP ${first_name} ${last_name} | ||
Log To Console ${first} ${last} | ||
END | ||
|
||
Append To List ${last_name} Wilson | ||
Log To Console Full Names: | ||
FOR ${first} ${last} IN ZIP ${first_name} ${last_name} | ||
Log To Console ${first} ${last} | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
*** Test Cases *** | ||
Error Catched | ||
TRY | ||
Fail expected error | ||
EXCEPT expected error | ||
Log To Console \nCatched! | ||
END | ||
Log To Console I am also being executed | ||
|
||
Error Not Catched | ||
TRY | ||
Fail surprising error | ||
EXCEPT expected error | ||
Log To Console \nCatched! | ||
END | ||
Log To Console I am not being executed | ||
|
||
Multiple EXCEPT branches | ||
${message} Set Variable Error 3 | ||
TRY | ||
Some Keyword | ||
EXCEPT Error 1 # Try matching this first. | ||
Log To Console \nError 1 got catched | ||
EXCEPT Error 2 # Try this if above did not match. | ||
Log To Console \nError 2 got catched | ||
EXCEPT ${message} # Last match attempt, this time using a variable. | ||
Log To Console \n${message} got catched | ||
END | ||
|
||
Multiple messages with one EXCEPT | ||
${message} Set Variable Error 3 | ||
TRY | ||
Some Keyword | ||
EXCEPT Error 1 Error 2 ${message} # Match any of these. | ||
Log To Console Some Error has been catched! | ||
END | ||
|
||
Match any error | ||
TRY | ||
Some Keyword | ||
EXCEPT # Match any error. | ||
Log To Console Something went wrong | ||
END | ||
|
||
Match any after testing more specific errors | ||
TRY | ||
Some Keyword | ||
EXCEPT Error 1 # Try matching this first | ||
Log To Console Error 1 occurred | ||
EXCEPT # Match any that did not match the above. | ||
Log To Console Something else went wrong | ||
END | ||
|
||
*** Keywords *** | ||
|
||
Some Keyword | ||
Fail Error ${{random.randint(1, 3)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
*** Variables *** | ||
${MATCH TYPE} regexp | ||
|
||
*** Test Cases *** | ||
Glob pattern 1 | ||
TRY | ||
Some Keyword | ||
EXCEPT Error * type=GLOB | ||
Log To Console \nCatched! | ||
END | ||
|
||
Glob pattern 2 | ||
TRY | ||
Some Keyword | ||
EXCEPT [Ee]rror ? type=glob | ||
Log To Console \nCatched again! | ||
END | ||
|
||
Regular expression 1 | ||
TRY | ||
Some Keyword | ||
EXCEPT Error .* type=${MATCH TYPE} | ||
Log To Console \nCatched! | ||
END | ||
|
||
Regular expression 2 | ||
TRY | ||
Some Keyword | ||
EXCEPT [Ee]rror \\d+ type=Regexp # Backslash needs to be escaped. | ||
Log To Console \nCatched! | ||
END | ||
|
||
Match start | ||
TRY | ||
Some Keyword | ||
EXCEPT Error type=start | ||
Log To Console \nCatched! | ||
END | ||
|
||
Explicit exact match | ||
${message} Set Variable Error 3 | ||
TRY | ||
Some Keyword | ||
EXCEPT Error 1 type=LITERAL | ||
Log To Console \nError 1 got catched | ||
EXCEPT Error 2 type=LITERAL | ||
Log To Console \nError 2 got catched | ||
EXCEPT ${message} type=LITERAL | ||
Log To Console \n${message} got catched | ||
END | ||
|
||
*** Keywords *** | ||
|
||
Some Keyword | ||
Fail Error ${{random.randint(1, 3)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
*** Test Cases *** | ||
Capture error | ||
TRY | ||
Some Keyword | ||
EXCEPT Error * type=GLOB AS ${error} | ||
Log To Console \nCatched ${error} | ||
END | ||
|
||
Capture error 2 | ||
TRY | ||
Some Keyword | ||
EXCEPT AS ${error} | ||
Log To Console \nCatched ${error} | ||
END | ||
|
||
|
||
*** Keywords *** | ||
|
||
Some Keyword | ||
Fail Error ${{random.randint(1, 3)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
*** Test Cases *** | ||
Handle everything | ||
TRY | ||
Some Keyword | ||
EXCEPT AS ${err} | ||
Log To Console Error occurred: ${err} | ||
ELSE | ||
Log To Console No error occurred! | ||
END | ||
|
||
TRY/EXCEPT/ELSE/FINALLY | ||
TRY | ||
Some keyword | ||
EXCEPT AS ${err} | ||
Log To Console \nError occurred: ${err} | ||
ELSE | ||
Log To Console \nNo error occurred. | ||
FINALLY | ||
Log To Console \nAlways executed. | ||
END | ||
|
||
TRY/FINALLY | ||
Log To Console \nOpen Connection | ||
TRY | ||
Log To Console \nUse Connection | ||
Fail Connection lost | ||
FINALLY | ||
Log To Console \nClose Connection | ||
END | ||
|
||
*** Keywords *** | ||
Some Keyword | ||
${rc} Set Variable ${{random.randint(0, 1)}} | ||
IF ${rc} == 0 | ||
RETURN ${rc} | ||
ELSE | ||
Fail Error ${rc} | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "TRY/EXCEPT Syntax", | ||
"description": "readme.md", | ||
"files": [ | ||
{ | ||
"fileName": "1_Basic.robot" | ||
}, | ||
{ | ||
"fileName": "2_Patterns.robot" | ||
}, | ||
{ | ||
"fileName": "3_Capture.robot" | ||
}, | ||
{ | ||
"fileName": "4_Else_Finally.robot" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## TRY / EXCEPT Syntax | ||
|
||
When a keyword fails, Robot Framework's default behavior is to stop the current test and executes its possible teardown. There can, however, be needs to handle these failures during execution as well. Robot Framework 5.0 introduces native **TRY/EXCEPT** syntax for this purpose, but there also other ways to handle errors. | ||
|
||
Robot Framework's **TRY/EXCEPT** syntax is inspired by Python's exception handling syntax. It has same **TRY**, **EXCEPT**, **ELSE** and ****FINALLY**** branches as Python and they also mostly work the same way. A difference is that Python uses lower case try, except, etc. but with Robot Framework all this kind of syntax must use upper case letters. A bigger difference is that with Python exceptions are objects and with Robot Framework you are dealing with error messages as strings. | ||
|
||
### Matching errors using patterns | ||
|
||
By default matching an error using **EXCEPT** requires an exact match. That can be changed using a configuration option type= as an argument to the except clause. Valid values for the option are **GLOB**, **REGEXP** or **START** (case-insensitive) to make the match a [glob pattern match](https://en.wikipedia.org/wiki/Glob_(programming)), a [regular expression match](https://en.wikipedia.org/wiki/Regular_expression), or to match only the beginning of the error, respectively. Using value **LITERAL** has the same effect as the default behavior. If an **EXCEPT** has multiple messages, this option applies to all of them. The value of the option can be defined with a variable as well. | ||
|
||
### Capturing error message | ||
|
||
When matching errors using patterns and when using **EXCEPT** without any messages to match any error, it is often useful to know the actual error that occurred. Robot Framework supports that by making it possible to capture the error message into a variable by adding AS ${var} at the end of the **EXCEPT** statement. | ||
|
||
### Using ELSE to execute keywords when there are no errors | ||
Optional **ELSE** branches make it possible to execute keywords if there is no error. There can be only one **ELSE** branch and it is allowed only after one or more EXCEPT branches: | ||
|
||
### Using FINALLY to execute keywords regardless are there errors or not | ||
Optional **FINALLY** branches make it possible to execute keywords both when there is an error and when there is not. They are thus suitable for cleaning up after a keyword execution somewhat similarly as teardowns. There can be only one **FINALLY** branch and it must always be last. They can be used in combination with **EXCEPT** and **ELSE** branches and having also **TRY/FINALLY** structure is possible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
*** Variables *** | ||
${fuel} 100 | ||
${max_drinks} 10 | ||
|
||
|
||
*** Test Cases *** | ||
Rocket mission | ||
WHILE ${fuel} | ||
Log To Console 🚀 Still going: ${fuel} / 100 | ||
${fuel} Evaluate ${fuel} -5 | ||
Sleep 200ms | ||
END | ||
|
||
Log To Console 🪂 fuel empty | ||
|
||
Lonkero hangover | ||
${drinks} Set Variable 0 | ||
WHILE ${drinks} < ${max_drinks} | ||
Log To Console Another one! 🥫 | ||
${drinks} Evaluate ${drinks} + 1 | ||
Sleep 200ms | ||
END | ||
|
||
Log To Console Let's go home 🛌🏼🥴 | ||
|
||
Actual example | ||
${rc}= Set Variable 1 | ||
WHILE ${rc} != 0 | ||
${rc}= Keyword that returns zero on success | ||
END | ||
|
||
*** Keywords *** | ||
Keyword that returns zero on success | ||
RETURN ${{random.randint(0, 10)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*** Test Cases *** | ||
|
||
Limit as iteration count | ||
WHILE True limit=100 | ||
Log This is run 100 times. | ||
END | ||
|
||
Limit as time | ||
WHILE True limit=10 seconds | ||
Log This is run 10 seconds. | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
*** Test Cases *** | ||
Nesting WHILE | ||
${x} = Set Variable 10 | ||
WHILE ${x} > 0 | ||
${y} = Set Variable ${x} | ||
WHILE ${y} > 0 | ||
${y} = Evaluate ${y} - 1 | ||
END | ||
${x} = Evaluate ${x} - 2 | ||
END | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "WHILE-Loops", | ||
"description": "readme.md", | ||
"files": [ | ||
{ | ||
"fileName": "1_Basic.robot" | ||
}, | ||
{ | ||
"fileName": "2_Limits.robot" | ||
}, | ||
{ | ||
"fileName": "3_Nesting.robot" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wenn du es bei 0 enden lässt, kommt die 0 nie.
Die Idee hier war auch das Exit bzw BREAK zu zeigen.
Ich würde es so ändern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kannst ja in die [Documentation] kurz reinschreiben, dass eine FOR Loop auch abgebrochen werden kann. " can conditionally be exited"