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 ***
${google_url} http://www.google.fr/
*** 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 http://seleniumhq.org/
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
- robot framework run keyword if
- robotframework example
- selenium framework tutorial
- robot framework variables
- robot framework qtp
Very nice tutorial! I’m already looking forward for the next part with SSHLibrary!
Instead of verifying the returned text with `Run Keyword If` you could use another BuiltIn keyword `Should Be Equal` like below. Passing the error message is optional and you can also control are the compared values shown in the error or not (by default they are).
Should Be Equal ${boldText} ${myWord} The search text is not bold
`Should Be Equal` and various other `Should …` keywords (e.g. `Should Start With`, `Should Match Regexp`, …) are in BuiltIn library to ease verifying values returned by other keywords.
Thx for you comment. I didn’t use the keyword “should be equals” yet. In this case, the keyword “should be …” are most appropriate, but i often use the keyword “run keyword if … ” in order to execute another keyword, like for example, in this case “Exit For Loop”.
Thx for your tips
Great tutorial!
One question in general about Robot Framework. Where do you recommend to store all object locators and in which format? All commercial tools, like QTP and WinRunner, have a special file for object repository (GUI Map). If a locator changes, there is one file to update it. Any suggestions here?
Thanks,
Vladimir
Hi Vladimir,
Thx for your comment.
I already used commercial tools like QTP. With Robot Framework, this is aproxymately the same thing. In our first tuto, we explain that. You can store all your object locators in differents files. You can organize your project like a QTP project (which contain, module and classes). So, you can store this kind of informations in a text file, html, rst etc … For example, at Wallix, we separe common variables, Test Case and Object locators in a separate directories and files. So when a locator change, we can easily update our tests scenaries.
Mbu.
Thank you, Mathieu for your suggestion.
Vladimir
Hello I am a student and am new to the world of robotframework. My guide has asked me to do the following task. Please help me with it.
You should make a python program with the following spec.
- full python, no use of python library except the one in the official python installation and Robotframework
- a main program (M1) start a thread T2 and then start a loop where it reads messages from T2
- the thread T2 start robotframework, to execute a robotframework test.
The test is a small sequence
print xx
pause
print xy
pause
print xz
pause
Each pause must generate an exchange between T2 and M1. The exchange is T2 send a message (with Queue python module) to M1. In M1 the reading message loop, prints a text, and send a message back to T2.
After having send a message to M1, T2 was waiting from the message back (on another Queue). When it gets the message, that is the end of the pause instruction implementation
The pause instruction is implemented in python
I want to know how to use variables
now I have test1.html,In this html ,it has ${a}
then I create a new html test2.html, it has ${b}
how can I use ${a} like this
the variable ${a}’s value is ${b}
you know?
should we talk with e-mail?
liangrt@neusoft.com
Hello just stumbled upon your blog via Yahoo after I typed in, “How to use RobotFramework (part 2)” or
perhaps something similar (can’t quite remember exactly). In any case, I’m grateful I found it simply because your subject material is exactly what I’m searching for (writing a university paper) and I hope you don’t mind if I gather some information from here
and I will of course credit you as the source. Thanks for your time.
Piece of writing writing is also a fun, if you know then you can write otherwise
it is difficult to write.
Wow, аwesomе weblоg ѕtructure!
How lengthy have you been running a blοg for? you make blоggіng glance еasy.
Тhе total glance of your site is excellent, as
smartly as thе content mаterial!
I’m truly enjoying the design and layout of your blog. It’s a
very easy on the eyes which makes it much more enjoyable for me to come here and visit more often.
Did you hire out a designer to create your theme?
Fantastic work!
Great Information, Thanks