Search This Blog

Sunday, November 4, 2012

Robot Framework

Robot Framework is generic test automation framework to write acceptance tests in ATDD(Acceptance Test Driven Development). ATDD is also known as Specification By Example wherein the requirements and business-oriented functional tests are defined using real time examples than abstract statements. These requirements are called Executable Requirements.


Robot Framework also can be used to automate functional test cases. It's using Keyword Driven approach for writing test scripts. The keywords are provided by standard or external TEST LIBRARIES(written in python or java). Also users can create their own keywords from existing keywords. The syntax to create User Keywords is similar to the syntax to create Test Cases.
Robot Framework itself is written in Python.
Test cases can be written in the following formats in Robot Framework:
  • Workflow tests(Keyword driven approach where the testcases are constructed from the keyword and their arguments)
  • High Level tests(Testcases are created by only high-level keywords that take no positional arguments)
  • Data driven tests (where test data varies without duplicating workflow(sequence of test steps))
Robot framework scripts are saved as .html, .tsv, .txt or .robot formats.
Sample .html file format:
SettingValueValueValue
LibraryOperatingSystem
VariableValueValueValue
${MESSAGE}Hello, world!
Test CaseActionArgumentArgument
Test 1[Documentation]Example test
Log${MESSAGE}
My Keyword/tmp
Test 2Should Be Equal${MESSAGE}Hello, world!
KeywordActionArgumentArgument
My Keyword[Arguments]${path}
Directory Should Exist${path}
The popular format is Plain Text Format, since it's easy to edit in any text editor.

Sample.robot
---------------------------------------------------------------------
***Settings***
Library  Process
***Variables***
***Test Cases***
My First Testcase
   
    ${handle} =  Start Process  notepad.exe
    ${result} =  Is Process Running
    Log To Console  ${result} it's running
    Terminate Process  ${handle}
--------------------------------------------------------------------
The above sample script uses 'Start Process', 'Is Process Running', 'Log To Console' and 'Terminate Process' keywords from Process Library.


Installation of Robot Framework:
I assume that you have already installed the Python Interpreter and configured it in PATH variable.
>pip install robotframework-python3
>pip list

Run the .robot file:
Open Cmd
>pybot c:\rfscripts\sample.robot

Upon running the script, it creates output.xml, log.html and report.html.


In ATDD, the requirements/tests follow Given-When-Then style of BDD.
Given- The initial state
When - The actions
Then - The expectations
This is implemented in Robot Framework by creating user keywords for each statement(keyword) in Given-When-Then style.


BDD Example:


*** Test Cases ***
Add two numbers
    Given I have Calculator open
    When I add 2 and 40
    Then result should be 42

Add negative numbers
    Given I have Calculator open
    When I add 1 and -2
    Then result should be -1

*** Keywords ***
I have ${program} open
    Start Program    ${program}

I add ${number 1} and ${number 2}
    Input Number    ${number 1}
    Push Button     +
    Input Number    ${number 2}
    Push Button     =

Result should be ${expected}
    ${result} =    Get Result
    Should Be Equal    ${result}    ${expected}






Data Driven Test Example:
Test Templates are used to convert normal keyword-driven test cases to data-driven test cases. Test cases with templates define only arguments for Template keyword. Also all the rounds are executed even if there are failures in any data set.




Test Case
Action
Argument
Argument
Test case 1
Data driven
Notepad.exe
Test Case 2
Data driven
Wordpad.exe
Templated test case
[Template]
Data driven
Notepad.exe
Wordpad.exe


Data-driven.robot:
---------------------------------------------------------------------------------


***Settings***
Library  Process
***Variables***
***Test Cases***
Templated test case
    [Template]  Data driven
    notepad.exe  ${true}
    calc.exe  ${true}
***Keywords***
Data driven
    [Arguments]  ${appname}  ${result}
    Start Process  ${appname}
    ${output} =  Is Process Running
    Should Be Equal  ${output}  ${result}

-----------------------------------------------------------------   
Variables in Robot Framework:
There are 4 types of variables:
  • Scalar ($)
  • List (@)
  • Dictionary(&)
  • Environment variable (%)
Robot Framework also provides Builtin variables.