Robot Framework Cheat Sheet

Basic initial configuration

set selenium timeout  30 seconds
Open Browser  about:blank  ${BROWSER}
Set window size    1920    1080

List variables

@{ENC_PAIR_DUAL}    0.0.0.0    10.11.114.4     0.0.0.0     10.11.114.5
input text    xpath://*[@id="gh-ac"]     @{ENC_PAIR_DUAL}[0] #
0.0.0.0
@{search_text}   books    travel    robot    gifts
console to log    @{search_text}[1] #travel
input text    xpath://*[@id="gh-ac"]    @{search_text}[1]

Diccionarys variables

&{BASE_URL} =      stage=http://10.11.99.110/    qa=http://10.11.99.112/

${ENV} =        qa
${SignInUrl} =     ${BASE_URL.${ENV}}

Awaiting forms

Form 1

Open Browser        http://www.google.com/    gc
    ${result}    ${condition}=    Run Keyword And Ignore Error    Wait Until Page Contains    Stackoverflow    timeout=2   error=false
    Run Keyword if  '${condition}'=='false'       Log    clicking
    Close All Browsers

Form 2

${status}   Run Keyword And Ignore Error   Close Popup Alerts
    Run Keyword If    '${status}[0]' == 'FAIL'    Log to Console    No Popup alerts found    ELSE    Popup alerts found

    ${result}    ${condition} =    Wait Until Page Contains Element    ${NEG_Name}    timeout=2    error=false
    Run Keyword if    '${condition}'=='false'    Log    There is no NEG ${result}    ELSE    Log    NEG already exists

Form 3

Wait Until Element Is Enabled assumes that the element exists on the page, and will wait until the element is enabled (not readonly, and not disabled). If the element does not exist, it will fail immediately (or after a timeout if you have an implicit wait)

Wait Until Element is Visible assumes that the element exists on the page, and will wait until the element is visible. If the element does not exist, it will fail immediately (or after a timeout if you have an implicit wait)

Wait Until Page Contains Element makes no assumptions about the element. It waits until the element is actually on the page, regardless if it is visible, invisible, enabled, or disabled. It does not require an implicit wait, since this keyword is an explicit wait.

Working with tests in the page

Wait Until Page Contains  Cart subtotal
Wait Until Page Contains  Hello
Wait Until Page Contains  Back to results
Wait Until Page Contains  results for "${SEARCH_TERM}"

Element Text Should Be  ${SIGNIN_MAIN_HEADING}    Sign-In
element text should be  ${TOPNAV_HEADING}  Register.

Get Text From Locator and Evaluate it

Validate Page Contents
    # this fails in chrome but passes in Edge
    #element text should be  ${TEAM_HEADER_LABEL}  Our Amazing Team
    ${ElementText} =  get text  ${TEAM_HEADER_LABEL}
    should be equal as strings  ${ElementText}  Our Amazing Team  ignore_case=true

IF Syntaxs and examples

Testing a true IF statement
    Run Keyword If  ${MY_VALUE} > 50  Keyword 1

Testing a false IF statement
    Run Keyword If  ${MY_VALUE} > 200  Keyword 1

Testing an IF/ELSE statement
    Run Keyword If  ${MY_VALUE} > 200  Keyword 1
    ...  ELSE  Keyword 2

Testing an IF/ElSE IF/ELSE statement
    Run Keyword If  ${MY_VALUE} > 200  Keyword 1
    ...  ELSE IF  ${MY_VALUE} == 10  Keyword 2
    ...  ELSE  Keyword 3


AND operator in: Run Keyword IF
 Run Keyword If    ${serv}==3 and ${plad}==3    set to dictionary    ${values}    ${item}    Unencrypted (PLAD Unencrypted)
        Run Keyword If    ${serv}==2 and ${plad}==2    set to dictionary    ${values}    ${item}    Fixed Key (PLAD Fixed PK)
        Run Keyword If    ${serv}==0 and ${plad}==3    set to dictionary    ${values}    ${item}    Full Encryption (PLAD Unencrypted)

OR Operator:
Run Keyword If    ${serv}==0 or ${plad}==3

FOR Syntaxys and examples

For Loop with Upper Bounds Range
    [Documentation]  This gives us a 0 based range
    FOR  ${Index}  IN RANGE  5
      Do Something  ${Index}
      ${RANDOM_STRING} =  Generate Random String  ${Index}
      Log  ${RANDOM_STRING}
    END

FOR Loop with Start and Finish Range
    [Documentation]  No longer a 0 based range because I provided start
    FOR  ${Index}  IN RANGE  1  4
      Do Something  ${Index}
      ${RANDOM_STRING} =  Generate Random String  ${Index}
      Log  ${RANDOM_STRING}
    END

FOR Loop with Start, Finish, and Step Range
    [Documentation]  The counter will jump by 2 each time ("step" value = 2)
    FOR  ${Index}  IN RANGE  1  10  2
       Do Something  ${Index}
       ${RANDOM_STRING} =  Generate Random String  ${Index}
       Log  ${RANDOM_STRING}
    END

Index for elements in for

${index} =    Set Variable    0
    FOR    ${col}    IN    @{cols}
       ${colum}    Format String    css:div[class='v-widget v-has-caption v-caption-on-top'] table[aria-rowcount='{0}'] tbody tr:nth-of-type({1}) td:nth-of-type(10)    ${r_count}    ${col}
        Click element    ${colum}
        press Keys    none    ${net_config.broadcast}
        Press Keys    none    TAB
        Press Keys    none    ${net_config.${index}}
        ${index}=    Evaluate    ${index} + 1
    END

FOR Loop with List

@{ITEMS} =  Create List  Item 1  Item 2  Item 3

    FOR  ${MyItem}  IN  @{ITEMS}
       Log  ${MyItem}
    END

FOR loop with Dictionary

&{serverInfo}    A=1    B=2   C=3
    FOR    ${key}    IN    &{serverInfo}
        log to console    ${key[0]}:\t\t${key[1]}
    END

Exit a FOR Loop

@{ITEMS} =  Create List  Item 1  Item 2  Item 3  Item 4

    FOR  ${MyItem}  IN  @{ITEMS}
       Log  ${MyItem}
       Run Keyword If  "${MyItem}" == "Item 3"  Exit For Loop
       Log  Didn't exit yet
    END

    Log  Now we're out of the loop

Repeat a keyword many times

Repeat Keyword  5  A Repeatable Keyword
    Repeat Keyword  2 times  A Repeatable Keyword
    Repeat Keyword  3 s  A Repeatable Keyword

Working with lists of elements in UI

Get List Items - Returns all labels or values of selection list locator
Get Selected List Label - Returns the label of selected option from selection list locator
Get Selected List Value - Returns the value of selected option from selection list locator

Select From List By Index - Selects options from selection list locator by indexes
Select From List By Label - Selects options from selection list locator by labels
Select From List By Value - Selects options from selection list locator by values
Select All From List - Selects all options from multi-selection list locator

Get Selected List Labels - Returns labels of selected options from selection list locator
Get Selected List Values - Returns values of selected options from selection list locator

Unselect From List By Index - Unselects options from selection list locator by indexes
Unselect From List By Label - Unselects options from selection list locator by labels
Unselect From List By Value - Unselects options from selection list locator by values
Unselect All From List - Unselects all options from multi-selection list locator

List Selection Should Be - Verifies selection list locator has expected options selected
List Should Have No Selections - Verifies selection list locator has no options selected

Page Should Contain List - Verifies selection list locator is found from current page
Page Should Not Contain List - Verifies selection list locator is not found from current page

WORKING with WEB ELEMENTS

Get Element Attribute    -    Returns the value of attribute from the element locator
    ${attr}    Get element attribute   xpath:/div/center/       class

Get Element Count    -    Returns the number of elements matching locator
    ${count}    Get element count       xpath:

Get Element Size    -    Returns width and height of the element identified by locator
    ${withd}    ${height}   Get element size    xpath:

Get WebElement    -    Returns the first WebElement matching the given locator


Get WebElements    -    Returns a list of WebElement objects matching the locator
  @{programs}    Get web elements    css:g:nth-child(3) > g:nth-child(2) > g:nth-child(9) >g


Capture Element Screenshot    -    Captures a screenshot from the element and embeds in log file
    Capture Element Screenshot      xpath:


Assign Id To Element    -    Assigns a temporary id to the element specified by locator
    Assign Id To Element    xpath:    NameID 
    page should contain element   NameID

Clear Element Text    -    Clears the value of the text-input-element identified by locator


Double Click Element    -    Double clicks the element identified by locator

Cover Element    -    Will cover elements identified by locator with a blue div

Click Element At Coordinates    -    Click the element locator at xoffset/yoffset

ASERTIONS

Element Attribute Value Should Be    -    Verifies element identified by locator contains expected attribute value
Element Should Be Disabled    -    Verifies that element identified by locator is disabled
Element Should Be Visible    -    Verifies that the element identified by locator is visible
Element Should Not Be Visible    -    Verifies that the element identified by locator is NOT visible
Element Should Contain    -    Verifies that element locator contains text expected
Element Should Not Contain    -    Verifies that element locator does not contain text expected
Element Text Should Be    -    Verifies that element locator contains exact the text expected
Element Text Should Not Be    -    Verifies that element locator does not contain exact the text not_expected
Element Should Be Enabled    -    Verifies that element identified by locator is enabled
Element Should Be Focused    -    Verifies that element identified by locator is focused

Working with windows

Get Window Handles
Get Window Identifiers
Get Window Names
Get Window Titles
Set Window Position
Get Window Position
Set Window Size
Get Window Size
Switch Window
Close Window

How to handle Mouse Actions

Mouse Down - Simulates pressing the left mouse button on the element locator
Mouse Down On Image - Simulates a mouse down event on an image identified by locator
Mouse Down On Link - Simulates a mouse down event on a link identified by locator
Mouse Up - Simulates releasing the left mouse button on the element locator
Mouse Over - Simulates hovering the mouse over the element locator
Mouse Out - Simulates moving the mouse away from the element locator
Open Context Menu - Right Click Operations - Opens the context menu on the element identified by locator
    Open Context Menu    xpath:
Drag And Drop - Drags the element identified by locator into the target element
    Drag and drop    ${el1}    ${tar1}
Drag And Drop By Offset - Drags the element identified with locator by xoffset/yoffset.

click actions

Double Click Element - Double clicks the element identified by locator
    double click element    //input[@name='q']

Cover Element - Will cover elements identified by locator with a blue div
    Cover element text    //input[@name='q']

Click Element At Coordinates - Click the element locator at xoffset/yoffset
    Click element at coordinates    //input[@name='q']    15    10

working with Radio buttons

select radio button    sex    Female
select radio button     exp    5

Working with Checkbox

select checkbox    BlackTea
Select checkbox    RedTea
unselect checkbox    BlackTea

Run keyworkd in base a condiition

run keyword if     ${cond}    UserKeyword1    ELSE     UserKeyword2

Use keyword1
    log    This is function 1

UseKeyword2
  log    This is function 2

Working with Try catch

run an continue no fail

run keyword and continue on failure    SignIn.Enter Credentials  ${Credentials}

set variables value in base of a condition

set variable if
    ${cond} =    set variable    True
    ${a} =    set variable if    ${cond} == False    10    0
    Log    ${a}      #if true - 10
    Log    ${a}      #if false - 0

Run keyword Unless the condition is true

FOR   ${i}   IN RANGE    1    11
    Log    ${i}
    Run keyword unless    ${i}>5    Log   ----
    #si ${i} es mejor que 5    pinta el LOG
  END

force someting to fail

Failing forcefully failing

${a} = set variable    10
    pass execution if     ${a}>5    because a>5, therefore pass
    Fail    Forcefully failing    #stop test here

Return from keyword if return messages

Returnmessages
    ${a}    =     10
    return from keyword if  ${a} >= 10    Hello
    return from keyword if  ${a} < 10    Hi

Test message
    ${b} =    Returnmessages
     log    ${b}

44