Robot Framework

From Torben's Wiki

Basics

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

Library Documentation:

Variables

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)


Strings

${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

Lists

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

Dictionaries

&{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

accessing

${USER 1}[name]

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}

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}

Libraries

Buildin

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  
...  '${arg_test_data.host}'=='prod'  1234
...  '${arg_test_data.host}'=='test'  5678
...  12345

While Loop

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

Random

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

Selenium

Navigate to Page

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

Go To  <URL>
Sleep  1s
${url} =  Get Location
or
${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

Checks

# 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}