Skip to content
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
wants to merge 7 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Examples/FOR_Loops/3_in_range.robot
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@ Documentation IN RANGE can get between 1 and 3 arguments.

*** Test Cases ***
Loop 10 times
Log To Console start: 0 ,inc: 1 , end:10
Log To Console start: 0 ,inc: 1 , end:9
FOR ${num} IN RANGE 10
Log To Console ${num}
END

Loop 10 times starting with 1
Log To Console start: 1 ,inc: 1 , end:11
FOR ${num} IN RANGE 1 10
Log To Console start: 1 ,inc: 1 , end:10
FOR ${num} IN RANGE 1 11
Log To Console ${num}
END

Loop starting with 0 to 100 in steps of 5
Log To Console start: 1 ,inc: 1 , end:11
Log To Console start: 0 ,inc: 5 , end:100
FOR ${num} IN RANGE 0 101 5
Log To Console ${num}
END

Countdown from 10 to 0 in 10 seconds
Log To Console start: 1 ,inc: 1 , end:11
[Documentation] FOR Loops can conditionally be exited using BREAK
Log To Console start: 10 ,inc: -1 , end: -1
FOR ${num} IN RANGE 10 -1 -1
Log To Console ${num}
Exit For Loop If ${num} == 0
IF ${num} == 0 BREAK
Sleep 1sec
END
Log To Console 🎉🥳 HAPPY NEW YEAR !!! 🍾
Copy link
Member

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.

Countdown from 10 to 0 in 10 seconds
    Log To Console    start: 10 ,inc: -1 , end: -1
    FOR    ${num}    IN RANGE    10    -1    -1
        Log To Console    ${num}
        IF    ${num} == 0    BREAK
        Sleep   1sec
    END
    Log To Console    🎉🥳 HAPPY NEW YEAR !!! 🍾

Copy link
Member

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"

24 changes: 20 additions & 4 deletions Examples/FOR_Loops/4_in_zip.robot
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
57 changes: 57 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/1_Basic.robot
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)}}
55 changes: 55 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/2_Patterns.robot
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)}}
20 changes: 20 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/3_Capture.robot
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)}}
38 changes: 38 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/4_Else_Finally.robot
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
18 changes: 18 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/config.json
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"
}
]
}
19 changes: 19 additions & 0 deletions Examples/TRY_EXCEPT_Syntax/readme.md
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.
4 changes: 3 additions & 1 deletion Examples/Variables/2_Lists.robot
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Test with some Collections keywords
Append To List ${list} 4 5 6
${list2}= Create List 7 8 9
${new_list}= Combine Lists ${list} ${list2}
${reversed}= Reverse List ${new_list}
${reversed}= Copy List ${new_list}
Reverse List ${reversed}
Log To Console ${new_list}
Log To Console ${reversed}

Test to access list entries
Expand Down
34 changes: 34 additions & 0 deletions Examples/WHILE_Loops/1_Basic.robot
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)}}
11 changes: 11 additions & 0 deletions Examples/WHILE_Loops/2_Limits.robot
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
12 changes: 12 additions & 0 deletions Examples/WHILE_Loops/3_Nesting.robot
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


15 changes: 15 additions & 0 deletions Examples/WHILE_Loops/config.json
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"
}
]
}
Loading