Robot Framework – quit writing ugly robot code, just write proper python

Robotframework is not a programming language. As soon as the test logic is not trivial, writing robot code becomes a nightmare. Robot does not have the  proper structures of a very basic programming language. Just try to write an if-else statement or a while loop and you’ll see.
But Robotframework does something very good: it presents the test in a human friendly form, very usefull for non programmer.

A robot test could be presented either as an HTML table, or under the form of a txt file (.txt or .robot) with tests written in robot language. When the test is presented as an HTML table, each row is nothing but the call to a keyword, usually written in robot language, in some text file.

The approach I propose is the following: Always use the HTML format for the test presentation. Business people can understand the tables, test and documentation are described in a single document, blah blah blah.  Then, for each single keyword, you implement it directly in python. No need to implement the keywords in the textual robot language which can only make your life difficult. In the html file, import your python files as Libraries:

Library     my_keywords.py 

There are some things that will make your life easy when implementing keywords in python.

  • In the python implementation of a keyword, you can access all variables available in your robot test

from robot.libraries.BuiltIn import BuiltIn
def perform_login():
    user_name= BuiltIn().get_variable_value("${USERNAME}")
    pwd= BuiltIn().get_variable_value("${PASSWORD}")
    #....
  • You can call any of your java keywords (basically any java API available in the classpath)
from com.mycompany.myproject.testtools import LoginRobotKeywords

def perform_login(user, pwd):
    LoginRobotKeywords().login(user, pwd)
  • You can call any of your robot keywords
    You might already have low level keywords written in robot and don’t want to re-implement them in python. Don’t worry, you can just call them from your python code
</pre>
<pre>from com.mycompany.myproject.testtools import LoginRobotKeywords
from robot.libraries.BuiltIn import BuiltIn

def perform_login(user, pwd):
    LoginRobotKeywords().login(user, pwd)
    BuiltIn().run_keyword("check user connected", user)
    BuiltIn().run_keyword_and_expect_error("*", "check user can format primary disk ", user)

That’s it. I hope I will never again have to use robotframework language as if it was a programming language.

Advertisements
This entry was posted in robot framework, testing. Bookmark the permalink.

4 Responses to Robot Framework – quit writing ugly robot code, just write proper python

  1. Reef Turner says:

    Could you point me to any longer examples? I would like to see what the final result of this would be when applied to a real world codebase. I am investigating using Robot Framework, but no one in my team has the desire to use the Robot language.

    • esalagea says:

      Not sure I can help on this. Our integration test codebase became a mixture of robot, Java an Python. Not easy to bundle to deliver to clients for on site validation. The promise that non devs will write tests failed: only developers do. We got used to it. Robot is not too bad aferall

      • Reef Turner says:

        Thanks for the reply! We only expect developers to be writing tests, and they are already familiar with python. Space separated / tabular data in the tests is also not very natural for the many vision impaired developers we have working on our project. The libraries, test runner, and reporting provided with Robot does look good. Also, I haven’t found many ready to go alternatives capable of driving several applications at once to ensure that they interact as expected (our main system testing use case).

  2. esalagea says:

    As for the real world use case: we have around 30 devs working for the last five years on a project implementing about 20 services (summing up to about 350k lines of code) and we are now running more than 2000 robot tests on our Jenkins CI. The integration between Jenkins and Robot is good and the generated reports are nice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s