keskiviikko 25. maaliskuuta 2015

Robot Framework : Installation on local Linux and VPS. XML + Selenium2Library. Jenkins script.
Robot framework is generic test automation framework for acceptance testing, ATDD.

In this tutorial I am going to use Selenium2Library. Robot framework is based on libraries. If there is no library for your needs you can just create your own.

Test cases are written in a .txt or .robot files. Test cases are keyword based and can be parameterized. Language is Python, so think creating a test case like row of tables. Tabulator or two spaces between code.

Keyword Argument Data
Click Button for example locator (id, name etc.)
Insert text name Jack

Selenium library documentation:

To install Robot Framework on Xubuntu:

$ sudo apt-get install python-pip
$sudo pip install robotframework
$sudo pip install robotframework-selenium2library
$ python
>> import Selenium2Library

And now you should have working Robot Framework including Selenium2Library. 

Create new test case:

$mkdir test
$cd tests
$nano test.robot

*** Settings ***
Library Selenium2Library
*** Variables ***
*** Test Cases ***
User must be able to open
[Documentation] Type in Laptop and search
[Tags] Smoke
Open Browser ${PAGE} ff
Input Text twotabsearchtextbox Laptop
Click Button Go
Close Browser
*** Keywords ***

Under settings we have imported the selenium library. In the variables I have inserted one variable that can be used anywhere in the test case. Under test cases the first one is test name, documentation is about the test what will be done etc and with tags tests can be linked together and it is easier to found them in report files. So "Open Browser" is selenium library keyword, page is my variable and ff means firefox.

And now to run your test:
$pybot test.robot

My computer opened browser and went to amazon OK! Everything working. Robot Framework created some log and report files to the same folder that our test case lies. 

Green is good!

pybot -d /some/path will move logfiles elsewhere. $pybot -d results tutorial.robot

To run multiple test sets at one, do $pybot foldername

example and results in a tree view: pybot -d /some/path tests (this will run all test sets under tests folder and put results to results folder, which keeps things separate. It will be easier to maintain things.

Check my example script:

Install on VPS / Vagrant

To run robotframework on a vagrantbox or VPS you need to run browser headless. This means robotframework can't run without faking screen. In order to do this install firefox:

$sudo apt-get install firefox

Install software to fake screen: $sudo apt-get install xvfb

Add a "display": $sudo Xvfb :10 -ac

Run firefox on the fake display:  $export DISPLAY=:10 firefox

And now you should be able to run robotframework tests in your vagrant / vps.

Closer look to Robot files

Robot framework should be installed to CI server as well as for developer computer. This means developer can run test sets and when CI build is triggered it will also run the tests. 

Create file resource.robot to the tests folder containing all test sets. This file will act as general settings file, which contains all basic variables, urls and tasks like open browser, maximize it etc. 

Then you can use those keywords in a test file. Here is my example:

Robot Framework doesnt create very sleek reports, so it would be useful to use some test case management software for project reporting and test case planning, like Quality Center or TestLink. Of course good old excel sheet will also do. Some people use only robot framework test files because they are pretty easy to understand if you understand robot and programming basics.

Reading XML files

Robotframework has built in library for reading xml. The principles are same than in the selenium2library, but you dont have to import it in python console beforehand. It is already there. 

XML documentation can be found here where all the keywords are explained:

XML .robot file looks like this:

*** Settings ***
Library           XML

*** Variables ***
${XmlFile}        lol.xml

*** Test Cases ***
    ${root}=    Parse XML    ${XmlFile}
    Should Be Equal    ${root.tag}    example
    ${first}  Get Element  ${root} first
    Should Be Equal  ${first.text}  text

And xml file is the same than in xml documentation:

  <first id="1">text</first>
  <second id="2">
    <child>more text</child>
    <second id="child"/>
      Text with <b>bold</b> and <i>italics</i>.

It goes through the xml file, checks that the xml root tag is "example" and first element text is "text" simple isn't it?


I created small shellscript to do Jenkins build. My system has user called "jenkka" the only thing Jenkins does is that it first goes to jenkkas home folder where the test.robot file lies, does all tests and puts results to var/www. Simple as that.

cd /home/jenkka
pybot -d /var/www test.robot

Best Practices

RobotFramework best practises are to create ShellScript, that will trigger wanted tests. For example we could do a shell script named "" which has code:


Then we would have script named "" which has a function named runQAAcceptanceTests.

# a  shell script to run robot tests

function runQAAcceptanceTests() {
  pybot some code here -d results .

Trigger the first script with command: ./ which will then run our function. The dot . after the pybot command will run all tests recursive under our main robot test folder. By doing this, we can run tests in our local environment and test server. We can define parameters for both cases and set jenkins properly.

To run a single test use shell command:  pybot -d results --test "testname" . Which will recursively find test case named testname. Last dot does the recursive search.

To add a variable file, which may contain for example test data numbers etc, you can give parameter like: --variablefile ./resources/ and the python file contains data which then can be used as variable ${variable}

When doing ATDD (Acceptance Test Driven Development) or BDD (Behaviour Driven Development), the following grammar is widely spread to formulate pre- and postconditions of a test:
  1. Given: The static preconditions
  2. When: The behaviour under test (or that, which should be specified)
  3. Then: The expected results of the behaviour under the given preconditions

Tutorial end

Notice for myself: There is syntax highlighter for ATOM editor:

Ride is testdata editor for robotframework. Install it with: pip install robotframework-ride

Read more about creating test case: