keskiviikko 25. maaliskuuta 2015

Robot Framework : Installation on local Linux and VPS. XML + Selenium2Library. Jenkins script.

http://robotframework.org/
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: http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Click%20Button

To install Robot Framework on Xubuntu:

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

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 ***
${PAGE} http://www.amazon.com
*** Test Cases ***
User must be able to open amazon.com
[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:
 https://github.com/shnigi/robotframework

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: http://robotframework.org/robotframework/latest/libraries/XML.html

XML .robot file looks like this:

*** Settings ***
Library           XML

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

*** Test Cases ***
Parse-Xml-Test
    ${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:

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

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


Jenkins


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.

#!/bin/sh
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 "run_qa_tests.sh" which has code:

#!/bin/bash
source executerobot.sh
runQAAcceptanceTests

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

#!/bin/bash
# executerobot.sh: a  shell script to run robot tests

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

Trigger the first script with command: ./run_qa_tests.sh 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/testdata.py 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: https://atom.io/packages/language-robot-framework

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

Read more about creating test case: http://en.wikipedia.org/wiki/Robot_Framework

Sources:
http://datakurre.pandala.org/2014/03/cross-browser-selenium-testing-with.html
http://testingknols.blogspot.fi/2014/05/robot-framework-installation-on-ubuntu.html
http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-framework-python-menu/221-read-xml-file-in-robot-framework-python


Ei kommentteja:

Lähetä kommentti