| 1 |
*** Settings *** |
| 2 |
Documentation Robot Framework 4 syntax recipes cheat sheet robot. |
| 3 |
... Demonstrates Robot Framework syntax in a concise format. |
| 4 |
Library MyLibrary |
| 5 |
Library MyLibrary WITH NAME HelloLibrary |
| 6 |
Library MyLibrary greeting=Howdy! WITH NAME HowdyLibrary |
| 7 |
Resource keywords.robot |
| 8 |
Suite Setup Log Suite Setup! |
| 9 |
Suite Teardown Log Suite Teardown! |
| 10 |
Test Setup Log Test Setup! |
| 11 |
Test Teardown Log Test Teardown! |
| 12 |
Test Timeout 2 minutes |
| 13 |
Variables variables.py |
| 14 |
|
| 15 |
*** Variables *** |
| 16 |
${STRING}= cat |
| 17 |
${NUMBER}= ${1} |
| 18 |
@{LIST}= one two three |
| 19 |
&{DICTIONARY}= string=${STRING} number=${NUMBER} list=@{LIST} |
| 20 |
${ENVIRONMENT_VARIABLE}= %{PATH=Default value} |
| 21 |
|
| 22 |
*** Keywords *** |
| 23 |
A keyword without arguments |
| 24 |
Log No arguments. |
| 25 |
|
| 26 |
A keyword with a required argument |
| 27 |
[Arguments] ${argument} |
| 28 |
Log Required argument: ${argument} |
| 29 |
|
| 30 |
A keyword with an optional argument |
| 31 |
[Arguments] ${argument}=Default value |
| 32 |
Log Optional argument: ${argument} |
| 33 |
... Another arg |
| 34 |
|
| 35 |
A keyword with any number of arguments |
| 36 |
[Arguments] @{varargs} |
| 37 |
Log Any number of arguments: @{varargs} |
| 38 |
|
| 39 |
A keyword with one or more arguments |
| 40 |
[Arguments] ${argument} @{varargs} |
| 41 |
Log One or more arguments: ${argument} @{varargs} |
| 42 |
|
| 43 |
A keyword that returns a value |
| 44 |
[Return] Return value |
| 45 |
|
| 46 |
A keyword with documentation |
| 47 |
[Documentation] This is keyword documentation. |
| 48 |
No Operation |
| 49 |
|
| 50 |
*** Test Cases *** |
| 51 |
Call keywords with a varying number of arguments |
| 52 |
[Tags] Test Another Tag |
| 53 |
A keyword without arguments |
| 54 |
A keyword with a required argument Argument |
| 55 |
A keyword with a required argument argument=Argument |
| 56 |
A keyword with an optional argument |
| 57 |
A keyword with an optional argument Argument |
| 58 |
A keyword with an optional argument argument=Argument |
| 59 |
A keyword with any number of arguments |
| 60 |
A keyword with any number of arguments arg1 arg2 arg3 arg4 arg5 |
| 61 |
A keyword with one or more arguments arg1 |
| 62 |
A keyword with one or more arguments arg1 arg2 arg3 |
| 63 |
|
| 64 |
Call a keyword that returns a value |
| 65 |
${value}= A keyword that returns a value |
| 66 |
Log ${value} # Return value |
| 67 |
|
| 68 |
Do conditional IF - ELSE IF - ELSE execution |
| 69 |
IF ${NUMBER} > 1 |
| 70 |
Log Greater than one. |
| 71 |
ELSE IF "${STRING}" == "dog" |
| 72 |
Log It's a dog! |
| 73 |
ELSE |
| 74 |
Log Probably a cat. 🤔 |
| 75 |
END |
| 76 |
|
| 77 |
Loop a list |
| 78 |
Log ${LIST} # ['one', 'two', 'three'] |
| 79 |
FOR ${item} IN @{LIST} |
| 80 |
Log ${item} # one, two, three |
| 81 |
END |
| 82 |
FOR ${item} IN one two three |
| 83 |
Log ${item} # one, two, three |
| 84 |
END |
| 85 |
|
| 86 |
Loop a dictionary |
| 87 |
Log ${DICTIONARY} |
| 88 |
# {'string': 'cat', 'number': 1, 'list': ['one', 'two', 'three']} |
| 89 |
FOR ${key_value_tuple} IN &{DICTIONARY} |
| 90 |
Log ${key_value_tuple} |
| 91 |
# ('string', 'cat'), ('number', 1), ('list', ['one', 'two', 'three']) |
| 92 |
END |
| 93 |
FOR ${key} IN @{DICTIONARY} |
| 94 |
Log ${key}=${DICTIONARY}[${key}] |
| 95 |
# string=cat, number=1, list=['one', 'two', 'three'] |
| 96 |
END |
| 97 |
|
| 98 |
Loop a range from 0 to end index |
| 99 |
FOR ${index} IN RANGE 10 |
| 100 |
Log ${index} # 0-9 |
| 101 |
END |
| 102 |
|
| 103 |
Loop a range from start to end index |
| 104 |
FOR ${index} IN RANGE 1 10 |
| 105 |
Log ${index} # 1-9 |
| 106 |
END |
| 107 |
|
| 108 |
Loop a range from start to end index with steps |
| 109 |
FOR ${index} IN RANGE 0 10 2 |
| 110 |
Log ${index} # 0, 2, 4, 6, 8 |
| 111 |
END |
| 112 |
|
| 113 |
Nest loops |
| 114 |
@{alphabets}= Create List a b c |
| 115 |
Log ${alphabets} # ['a', 'b', 'c'] |
| 116 |
@{numbers}= Create List ${1} ${2} ${3} |
| 117 |
Log ${numbers} # [1, 2, 3] |
| 118 |
FOR ${alphabet} IN @{alphabets} |
| 119 |
FOR ${number} IN @{numbers} |
| 120 |
Log ${alphabet}${number} |
| 121 |
# a1, a2, a3, b1, b2, b3, c1, c2, c3 |
| 122 |
END |
| 123 |
END |
| 124 |
|
| 125 |
Exit a loop on condition |
| 126 |
FOR ${i} IN RANGE 5 |
| 127 |
Exit For Loop If ${i} == 2 |
| 128 |
Log ${i} # 0, 1 |
| 129 |
END |
| 130 |
|
| 131 |
Continue a loop from the next iteration on condition |
| 132 |
FOR ${i} IN RANGE 3 |
| 133 |
Continue For Loop If ${i} == 1 |
| 134 |
Log ${i} # 0, 2 |
| 135 |
END |
| 136 |
|
| 137 |
Create a scalar variable |
| 138 |
${animal}= Set Variable dog |
| 139 |
Log ${animal} # dog |
| 140 |
Log ${animal}[0] # d |
| 141 |
Log ${animal}[-1] # g |
| 142 |
|
| 143 |
Create a number variable |
| 144 |
${Ï€}= Set Variable ${3.14} |
| 145 |
Log ${Ï€} # 3.14 |
| 146 |
|
| 147 |
Create a list variable |
| 148 |
@{animals}= Create List dog cat bear |
| 149 |
Log ${animals} # ['dog', 'cat', 'bear'] |
| 150 |
Log ${animals}[0] # dog |
| 151 |
Log ${animals}[-1] # bear |
| 152 |
|
| 153 |
Create a dictionary variable |
| 154 |
&{dictionary}= Create Dictionary key1=value1 key2=value2 |
| 155 |
Log ${dictionary} # {'key1': 'value1', 'key2': 'value2'} |
| 156 |
Log ${dictionary}[key1] # value1 |
| 157 |
Log ${dictionary.key2} # value2 |
| 158 |
|
| 159 |
Access the items in a sequence (list, string) |
| 160 |
${string}= Set Variable Hello world! |
| 161 |
Log ${string}[0] # H |
| 162 |
Log ${string}[:5] # Hello |
| 163 |
Log ${string}[6:] # world! |
| 164 |
Log ${string}[-1] # ! |
| 165 |
@{list}= Create List one two three four five |
| 166 |
Log ${list} # ['one', 'two', 'three', 'four', 'five'] |
| 167 |
Log ${list}[0:6:2] # ['one', 'three', 'five'] |
| 168 |
|
| 169 |
Call a custom Python library |
| 170 |
${greeting}= MyLibrary.Get Greeting |
| 171 |
Log ${greeting} # Hello! |
| 172 |
${greeting}= HelloLibrary.Get Greeting |
| 173 |
Log ${greeting} # Hello! |
| 174 |
${greeting}= HowdyLibrary.Get Greeting |
| 175 |
Log ${greeting} # Howdy! |
| 176 |
|
| 177 |
Call a keyword from a separate resource file |
| 178 |
My keyword in a separate resource file |
| 179 |
|
| 180 |
Access a variable in a separate variable file |
| 181 |
Log ${MY_VARIABLE_FROM_A_SEPARATE_VARIABLE_FILE} |
| 182 |
|
| 183 |
Split arguments to multiple lines |
| 184 |
A keyword with any number of arguments |
| 185 |
... arg1 |
| 186 |
... arg2 |
| 187 |
... arg3 |
| 188 |
|
| 189 |
Log available variables |
| 190 |
Log Variables |
| 191 |
# ${/} = / |
| 192 |
# &{DICTIONARY} = { string=cat | number=1 | list=['one', 'two', 'three'] } |
| 193 |
# ${OUTPUT_DIR} = /Users/<username>/... |
| 194 |
# ... |
| 195 |
|
| 196 |
Evaluate Python expressions |
| 197 |
${path}= Evaluate os.environ.get("PATH") |
| 198 |
${path}= Set Variable ${{os.environ.get("PATH")}} |
| 199 |
|
| 200 |
Use special variables |
| 201 |
Log ${EMPTY} # Like the ${SPACE}, but without the space. |
| 202 |
Log ${False} # Boolean False. |
| 203 |
Log ${None} # Python None |
| 204 |
Log ${null} # Java null. |
| 205 |
Log ${SPACE} # ASCII space (\x20). |
| 206 |
Log ${SPACE * 4} # Four spaces. |
| 207 |
Log "${SPACE}" # Quoted space (" "). |
| 208 |
Log ${True} # Boolean True. |
| 209 |
LOG ${CURDIR} # Current directory |