Robot Framework

From Torben's Wiki
Revision as of 14:11, 23 January 2020 by Torben (talk | contribs) (Basics)


Keywords are separated by (at least) 2 spaces or (at least) 1 tab

Library Documentation:


Variables, see RobotFrameworkUserGuide -> variables Robot Framework has its own variables that can be used as scalars, lists or dictionaries using syntax ${SCALAR}, @{LIST} and &{DICT}, respectively. In addition to this, environment variables can be used directly with syntax %{ENV_VAR}.

${STR}   This is a String
@{LIST}  ['robot', 'secret']
&{DICT}  {'name': 'robot', 'password': 'secret'}
&{DICT}  name=menke  pass=password

(an '=' can be used, but only 1 space before it, so better just leave it out)


${NAME}       Robot Framework
${VERSION}    2.0
${ROBOT}      ${NAME} ${VERSION}

${EXAMPLE}    This value is joined    together with a space
${MULTILINE}  SEPARATOR=\n    First line
...             Second line     Third line


@{NAMES}      Matti       Teppo
@{NAMES2}     @{NAMES}    Seppo
@{MANY}       one         two      three      four
...           five        six      seven


&{USER 1}     name=Matti    address=xxx         phone=123
&{USER 2}     name=Teppo    address=yyy         phone=456
&{MANY}       first=1       second=${2}         ${3}=third
&{EVEN MORE}  &{MANY}       first=override      empty=
...           =empty        key\=here=value


${USER 1}[name]


$var  ${TRUE}
$var  ${FALSE}

Join Variables to String (in Varaible section only, in keyword section use Catenate)

${text1}  Generate Random String  1  [UPPER]
${text2}  Generate Random String  5  [NUMBERS]
${text} =  ${text1}  -  ${text2}
# or
${text}  Catenate  SEPARATOR=-  ${text1}  ${text2}

Strings vs Integers

Numbers entered in .robot code are strings by default. Data fetched from database might be of integer type. To ensure integer comparison, use Should Be Equal As Integers

Should Be Equal As Integers  ${queryResults[0][0]}  3
Should Be Equal As Numbers   ${queryResults[0][0]}  3
Should Be Equal As Strings   ${queryResults[0][0]}  3

Sting -> Int

Convert To Integer  123
Convert To Number  42.512  1  -> 42.5

Int -> String

str (3)

Suite Setup and Teardown

Suite Setup       Run Keywords  
...  Open Browser	    ${URL_LOGIN}    ${BROWSER} 
...  AND  Maximize Browser Window
Suite Teardown    Close Browser
Test Setup        Go To	            ${URL_LOGIN}
Test Teardown     Go To	            ${URL_LOGOUT}



Variables in Keyword Section

Set Variable

${text}   Set Variable  A  B  C  D

same as

${text}  Catenate   Connecting using  A  B  C  D


If Else

${found} =  Run Keyword And Return Status  Element Should Be Visible  ${ALL_PAGES.BUTTON_LOGOUT}
Run Keyword If  ${found}  
...  Run Keywords
...    Click Button  ${ALL_PAGES.BUTTON_LOGOUT}
...    AND  Sleep  1s
...  ELSE  
...    log  no logout button found

Set Variable If / Else

${DATABASE.port}  Set Variable If  
...  '${}'=='prod'  1234
...  '${}'=='test'  5678
...  12345

For Loop

FOR  ${index}  IN RANGE  0  ${arg_num_checkboxes}
    Click Element  id=rad-${index}-isOk

While Loop

Wait Until Keyword Succeeds  12 secs  2 secs
...  Check If Exists In Database  SELECT id FROM db.table WHERE order = '${orderNo}'


Generate Random String

${text1}  Generate Random String  1  [UPPER]
${text2}  Generate Random String  4  [NUMBERS]
${text2}  Generate Random String  4  [NUMBERS]
${text}   Catenate  SEPARATOR=-  ${text1}  ${text2}  ${text3}

Random element from list

${text}   Evaluate  random.choice(@{MYLIST})  random

Calculate / Evaluate

${text}  Get Text ${LOCATOR}
${count}  Convert To Integer  ${text}
${count}  Evaluate  ${count} + 10


Navigate to Page

Open Browser	    ${URL}    ${BROWSER}  
Maximize Browser Window

Go To  <URL>
Sleep  1s
${url} =  Get Location
${url} =  Execute Javascript  return window.location.href;

Get Text

${issue_id}  Get Text  ${PAGE_PDF_VIEW.TEXT_ISSUE_ID}

Send Enter Key to Input field

Press Key 	${field} 	\\13  # \\13 = ASCII code for enter key


# Wait Until Location Contains  /some/path  timeout=10 s

${field}  Set Variable  ${BUTTON_LOGOUT}
Wait Until Page Contains Element  ${field}  timeout=10 s
Set Focus To Element  ${field}
Click Button  ${field}

# above is much better than:
# Wait Until Element Is Visible    ${BUTTON_LOGOUT}

# also handy
${url}  Get Location
Should End With    ${url}    /login
Page Should Contain  <Some Text>
Click  Button  ${BTN_SUBMIT}
Wait Until Page Does Not Contain  ${BTN_SUBMIT}

Wait and Click

Wait And Click
    [Arguments]  ${loc}
    Wait Until Page Contains Element  ${loc}
    Set Focus To Element              ${loc}
    Wait Until Element Is Visible     ${loc}
    Click Element                     ${loc}

Wait And Enter Text
    [Arguments]  ${loc}  ${text}
    Wait Until Page Contains Element         ${loc}
    Set Focus To Element                     ${loc}
    Check If Element Is Visible And Enabled  ${loc}
    Input Text                               ${loc}  ${text}
Wait And Get Text
    [Arguments]  ${loc}
    Wait Until Page Contains Element  ${loc}
    Set Focus To Element              ${loc}
    Wait Until Element Is Visible     ${loc}
    ${text}  Get Text                 ${loc}
    [Return]  ${text}

Scolling / Set Focus

Input and Clicking does not work if element is not visible, use this to scroll to the element

Set Focus To Element  ${FIELD}

If the element just moved out of the visual page, a

Sleep  0.25 s
Set Focus To Element  ${FIELD}

before setting the focus helps

Date and Time

Library    DateTime
${date_now}                     Get Current Date
Set Focus To Element            ${PAGE_CREATE.INPUT_ERROR_DESCR}
Input Text                      ${PAGE_CREATE.INPUT_ERROR_DESCR}  Robotframework Test at ${date_now}