How to use RobotFramework (part 2)

  • Sharebar

RobotFramework logo

In our first tutorial, we explained how to use Robot Framework with the famous Selenium Library.

Robot Framework (RF) includes many libraries : In the RF package you have 8 standards test libraries (builtin, OS, Screenshot, Telnet, Collection, String, Dialogs and Remote), and many external test libraries, like Selenium Autoit, eclipse, Watir and more.

Objective of this tutorial

In this second tutorial, we’ll focus on the standard library called Builtin. This is one of the most important and powerfull library include with RF. This library contains many keywords for manage your test data and variables. It is automatically imported and always available.

You can find the details of the keywords here : builtin library keywords

The objective of this tutorial is to explain how to use some useful functionalities of the builtin library. Mainly we’ll explain how to make conditions (if else) and how to use for loop constructs.

Getting Started

We suppose that you’ve read our first tutorial and you have configure your environment in consequences. So, now we could re-use the previous tests files for this tutorial. However, we will change some parameters and add functions, but we’ll keep the previous tests.

So, we’ll use the same files for this tutorial, but instead of making a Google search and click on selenium link, our new test scenario will take a list of words in parameter and put them in the Google text area in order to verify that our words appear in bold on the result page.

Now we can create a new test file, called : mySecondTest.txt

List Variables

First we have to create a list variable. With RobotFramework you can use list variables and dictionaries instead of standard variables. List variable syntax is : @{myList} .

You have to set your list variable like that :

*** Variables ***

@{myWords}  Selenium  WALLIX  robotframework

To get value from your list you have to use this syntax :

Log  @{myWords}[1]

In this example, you get the second element of the list and this will log the words : WALLIX .

The file mySecondTest.txt :

*** Settings ***

Documentation  This is your second test

Resource  common.txt
Resource  google-search.txt

*** Variables ***

# List of searched words
@{myWords}  Selenium  WALLIX  robotframework

*** Test Cases ***

Go To Google Page [Documentation] Go to google home page
    Open Browser to Google Page

# This our new test case :
Test Google Result [Documentation] TestCase for test google Result
    Submit Loop Search  @{myWords}

    [Teardown]  Close Browser

Resource File : common.txt

Just for reminder, the ressource file common.txt, will be like this :

*** Settings ***

Documentation  This ressource file contain common and globals variables

Library        SeleniumLibrary  25  localhost  5556

*** Variables ***

${browser}       *chrome
${sel_speed}         0

(For your information, we have set the selenium default time out at 25, because sometimes, the network could be too slow and this example could fail)

Loop And Conditions

The loop and conditions are define in the google-search.txt ressource file.

We still use the older keywords. Our first test have to pass anyway!

*** Settings ***

Documentation  this ressource file contain spefic keywords for running 
...            Robotframework and Selenium Lib

*** Variables ***


*** Keywords ***

Open Browser To Google Page
    Open Browser  ${google_url}  ${browser}
    Maximize Browser Window
    Set Selenium Speed  ${sel_speed}
    Title Should Be  Google

Input Search  [Arguments]  ${search}
    Input Text  lst-ib  ${search}

Submit Search
    Click Button  btnG  don't wait

Click Selenium Link
    Click Link

Selenium Blog Should Open
    Title Should Be  Selenium - Web Browser Automation

We add to function in order to run our second test :

Go to google Home Page
    Go To  ${google_url}

Get Bold Text
    ${return} =   Get Text  css=em
    [Return]  ${return}

In the second function, we use a variable to get the result of the keyword and return them. Later we’ll have to use this result on our test. (This two keyword “go to” and “Get text” are define in the Selenium Library)

Then, we have the last function with the loop :

Here we pass the list in argument. The list must be the lastest parameter.

Example :

Example Function  [Arguments]  ${int}  @{myList}
# for loop search function
Submit Loop Search  [Arguments]  @{myWords}
    # Loop for verify bold text
    : FOR  ${myWord}  IN  @{myWords}

    # Here we call internal function for 
    # input and submit google search
    # Get text and return to google home page
    \  Input Search  ${myWord}
    \  Submit Search
    \  ${boldText} =   Get Bold Text
    \  Run Keyword If  '${boldText}' != '${myWord}'  Fail  The search text is not bold
    \  Go to google Home Page

All instruction in your loop should have in front of them “\” and two space between the backslash and the keyword.

Here we use the keyword : Run Keyword If . With this keyword you can make condition. Here you can traduce like that, if ${boldText} is not equal to ${myWord} then the test FAIL.

You can also use the keyword Exit For Loop instead of FAIL.

Now, you can write more complex test scenarios, with loops and conditions.

At Wallix, we often use RobotFramework SSHLibrary, we’ll explain in a next tutorial how we write our tests with the SSHLibrary on our products.

Article contributed by Mathieu Bultel, QA engineer at Wallix.

Incoming search terms:

  • robot framework examples
  • how to use robot framework
  • robot framework for loop
  • documentation robotframework
  • robot framework selenium example
  • robotframework example
  • robot framework run keyword if
  • selenium framework tutorial
  • robot framework variables
  • robot framework qtp
This entry was posted in test and tagged , , , , . Bookmark the permalink.