How to use RobotFramework with the Selenium Library

  • Sharebar

RobotFramework logo

At Wallix we use RobotFramework to test our products AdminBastion and LogBox in a black box way mainly for exercising the web interface. In this blog post we’ll explain one way of using RobotFramework to test web interfaces either for web site testing or for web application testing.

What is RobotFramework ?

Robot Framework is a generic test automation framework for acceptance testing. It has an easy-to-use tabular test data syntax and utilizes a keyword-driven testing approach. Its testing capabilities can be extended by test libraries implemented either with Python or Java, and users can create new keywords from existing ones using the same syntax that is used for creating test cases.

 Robot Framework is an open source software released under the Apache License 2.0. It’s developed and owned by Nokia Siemens Networks.

For more information in order to install and setup RobotFramework, you should read the RobotFramework Wiki and the Setup and installation page.

What is the Selenium Library ?

The Selenium Library is a RobotFramework test library that uses the popular Selenium web testing tool internally. It provides a powerful combination of a simple test data syntax and support for different browsers.

Objective of this Tutorial

The objective of this tutorial is to explain how to use RobotFramework with Selenium and to detail some functionalities of RobotFramework by the way.

Pre-conditions

  • Install RobotFramework on your machine: ( python setup.py install ), for Windows environment you have to install python 2.7.
  • Then, you have to get the Java Runtime Environment (JRE) for running the Selenium Server.
  • Make sure you have Firefox in your path.
  • Install the Selenium Library using: python setup.py install

For Windows environment you can use the windows installer.

Getting started

First, you have to launch the Selenium server. In a terminal go to your directory where selenium-server.jar file are present and launch the server by typing :

[mbu@mbu2 poc]$ cd <my selenium server directory>
[mbu@mbu2 poc]$ java -jar selenium-server.jar

Your Selenium server should start …You can specify more options, like port, firefox profile etc…

Now you can write your first test. This test will open a browser to www.google.fr web site and type selenium in the search area.

Before writing your first test make sure that the command pybot –help works well.

There are different ways to write a test with RobotFramework. You can write your test in an html file using a table, in a standard text file or in a rst file. In this tutorial, we will use a standard text file.

So, now you can make a directory for your first test:

[mbu@mbu2 poc]$ mkdir tuto

And then you can create a txt file called myFirstTest.txt. It’s the main file that RobotFramework will execute for running the test. In this file, functions described by « Keywords » will be called by the robot.

The main test file: myFirstTest.txt

A test file contains different sections, like settings, variables, testcase or keywords. Section are defined with three wild cards.

First you have to fill the Settings section:

*** Settings ***
Documentation  This is your first test

In the Settings section, the word « Documentation » is a keyword. It means that this word is interpreted by RobotFramework and executed. The keyword « Documentation » will print in your terminal and in the log file the text : « this is your first test ».

Then, you have to set your resources files. Resource files are optional. You can write all your variables and your own keywords in the same file but it’s better for your test suite to put your variables and global informations in different files to reuse them in all your tests. To demonstrate their usage in this tutorial, you will use two resources files :

  • one for global variables and library: common.txt
  • another one for specific keywords: google-search.txt

Add the following lines to your file to declare these resources:

Resource  common.txt
Resource  google-search.txt

Then, you have to specify your tests cases. In this example, you’ll create TestsCases and call keywords which are contained in google-search.txt resource file (we’ll create those keywords in the second part of this tutorial).

*** Test Cases ***

Go To Google Page [Documentation] Go to google page and search something
    Open Browser to Google Page
    Input Search  selenium
    Submit Search

Open Selenium Page [Documentation] TestCase for open Selenium page
    Click Selenium Link
    Selenium Blog Should Open

    [Teardown]  Close Browser

Here, you have two TestsCases which contains your own keywords.

Resource File : google-search.txt

In this file, you’ll specify all keywords for running RobotFramework with the Selenium Library.

This file is also made of section like your first file:

*** Settings ***
Documentation  this ressource file contain specific keywords for running
...            Robotframework and Selenium Lib

*** Variables ***
${google_url}  http://www.google.fr/

You can create variables with this syntax ${myVar}. By default the type of variables is string. You can create other variables like list or dictionary. We’ll see this in another tutorial…

*** 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

So, in this second part, you have created all of your keywords needed for your test. Now you have to create the second resource file containing global and common variables.

Resource File : common.txt

Like in the other files you specify a Settings section:

*** Settings ***
Documentation  This resource file contains common and global variables
Library  SeleniumLibrary  5  localhost

*** Variables ***
# Choose Firefox Chrome Browser :
# You can change here your web browser.
${browser}  *chrome
${sel_speed}  1

Here, you import the Selenium library. You have to specify some parameters like the host, port and time out.

Then, you create two variables, one for the browser to use, another one for Selenium speed.

Important : In the RobotFramwork syntax, you can comment some words with the character # . You have to respect the indentation : keywords, data or variables must be separated by TWO spaces minimum.

Here, you could find all of Selenium Library keywords : http://robotframework-seleniumlibrary.googlecode.com/hg/doc/SeleniumLibrary.html?r=2.7

And here you can find all the standards libs included in RobotFramework and external libs: http://code.google.com/p/robotframework/wiki/TestLibraries

With these other libs you can automate ssh connection for example or telnet or use RobotFramework on a remote machine.

Launch your test

To launch your test you have to type in a terminal:

[mbu@mbu2 poc]$ pybot myFirstTest.txt
==============================================================================
myFirstTest :: This is your first test
==============================================================================
...Google Page [Documentation] Go to google page and search something | PASS |
------------------------------------------------------------------------------
Open Selenium Page [Documentation] TestCase for open Selenium page | PASS |
------------------------------------------------------------------------------
myFirstTest :: This is your first test | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output: /home/mbu/Projet/qa/poc/tuto/output.xml
Report: /home/mbu/Projet/qa/poc/tuto/report.html
Log: /home/mbu/Projet/qa/poc/tuto/log.html

Results and reports

After the execution, RobotFramework create three output files :

  • log.html : the result step by step of your test.
  • Report.html : A little report of your test suite
  • output.xml : another output in xml format.

Here are some screenshots of those output files:

Log file

Log file 2

Report file

Output file

Screenshots

RobotFramework takes screenshots during the execution and when TestsCases fail:

Screenshot

More

You can use RobotFramework IDE called RIDE to write your tests.

In a next tutorial, we’ll explain more about RobotFramework: how to manipulate strings, conditions (if else), loops, recursive functions, and more.

Stay tuned !

Article contributed by Mathieu Bultel, QA engineer at Wallix.

Incoming search terms:

  • robot framework tutorial
  • robot framework selenium
  • robot framework selenium tutorial
  • robot framework example
  • robot framework
  • robotframework
  • selenium robot framework tutorial
  • selenium robot framework
  • selenium robot
  • robot selenium
This entry was posted in test and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


+ six = 15

* Copy This Password *

* Type Or Paste Password Here *

50,616 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>