Thursday, May 5, 2016

Selenium Robot Framework Python Test Case with RIDE

In this example, you will learn how to use RIDE in Selenium Robot Framework Python test automation. 

Test Case: authorized users are able to login Mahara Demo site. Here are test steps.


 1. Navigate to demo.mahara.org and verify that web page title "Home-Mahara" displays.
 2. Enter "student2" in the Username filed.
 3. Enter "Testing1" in the Password field.
 4. Click on the Login button
 5. Assert that Dashboard Title is "Dashboard - Mahara".
 Note: if the above username and password do not work, try registering one and 
 use your   own user account for testing because the demo user password may have 
 been changed by others.

Selenium Python Robot Framework Implementation

Step 1: open command window as administrator and launch RIDE with the command "ride.py".  RIDE window will display. Please remember that do not close the command window, or else RIDE will be closed. 
Step 2: under File menu, click on "New Project", enter project Name and select a Parent Directory as shown below. Please note that you need to create "C:\MaharaDemoTest" folder in advance.




Step 3: in the navigation window, Right Click and choose "New Directory" and enter "LoginModuleTest". 



Step 4: right click on the "LoginModuleTest" and select "New Suite". Enter "Login" in the suite name field. 
Step 5: click on the "Login" test suite and enter the following keywords and test case in the "Text Edit" window. 


*** Settings ***
Library           Selenium2Library

*** Variables ***
${Username}       student2
${Password}       Testing1
${Browser}        Firefox
${SiteUrl}        http://demo.mahara.org
${DashboardTitle}    Dashboard - Mahara
${Delay}          5s

*** Test Cases ***
LoginTest
    Open Browser to the Login Page
    Enter User Name
    Enter Password
    Click Login
    sleep    ${Delay}
    Assert Dashboard Title
    [Teardown]    Close Browser

*** Keywords ***
Open Browser to the Login Page
    open browser    ${SiteUrl}    ${Browser}
    Maximize Browser Window

Enter User Name
    Input Text    login_login_username    ${Username}

Enter Password
    Input Text    login_login_password    ${Password}

Click Login
    click button    login_submit

Assert Dashboard Title
    Title Should be    ${DashboardTitle}
 
 
 

After you enter all the above information, the screen looks like this below.


Step 6: click on the "Run" tab and execute the test case "LoginTest". 




Step 7: check the log file "C:\MaharaDemoTest\LoginModuleTest\TestResult\log-20140601-232305.html" and result file "C:\MaharaDemoTest\LoginModuleTest\TestResult\report-20140601-232305.html". The result indicates that test passed. If test fails, the background will be Red. Click on the file links to see the result file in html format.

 

Wednesday, May 4, 2016

Selenium Robot Framework Installation with Python


To use Robot Framework with Python, you need to install Python, robot framework, Selenium2Library and other supporting libraries. 

Step 1: Install Python 2.7 version
        Before installing Robot Framework, you need to install Python 2.5 or later. Robot Framework has not been updated to work with Python 3 or above, so you should certainly install Python 2.7 version. Visit the link to download and install Python 2.7 version. After installing Python 2.7, add your python installation to your system environment path. 

on Windows, add "c:\Python27\;c:\Python27\Scripts\;" to your path. On Windows vista or Windows 7 environment, it is recommended to install Python to all users, and to run the installation as an administrator. After installing, you can confirm python installation with the command below. 

Step 2: Verify Python 2.7 version

 python --version

 command result will be

 Python 2.7.6

 Step 3: install robot framework with pip command. When you install Python, pip application will be installed automatically. If you are familiar with installing Python packages and have pip available, just run the following command.

 pip install robotframework

 Step 4: Verify robot framework installation
After a successful installation with Python, you should be able to  execute the following commands on the command prompt to verify the installation. 

pybot --version

 Command Result

 Robot Framework 2.8.4 (Python 2.7.6 on win32)

Step 5: Install Selenium2Library
You can install robotframework-selenium2library by downloading the packaged executable file or using the python pip application. Here is the link to download. Use the following command to install robotframework-selenium2library with pip. 

pip install robotframework-selenium2library

 Step 6: Install Selenium Library
Since Selenium2Library has some dependencies for selenium library, you need to install selenium library. The easy way of installing selenium library is to use pip. You can use the following command to install selenium.

 pip install -U selenium

 Step 7:  Install the Selenium robot framework IDE - RIDE
RIDE is a light-weight and intuitive editor for Robot Framework test case files. Since RIDE uses wxpython 2.8, you need to install the package as well. Visit the link for details. RIDE does not yet support Python 3. On OS X RIDE requires 32-bit Python version. 

To install wxpython 2.8 or later version, visit the wxpython.org download page and download the version depending on your OS. Please note that the package has 32 bit and 64 bit, so you need to install the corresponding package depending on your operating system version. 

To install RIDE, you can use pip with the following command. 

 pip install robotframework-ride

 Step 8: Verify RIDE Installation
After a successful installation, RIDE can be started from the command line by running

 ride.py 

 Alternatively you can specify a file or directory to open as an argument like ride.py path/to/tests. Starting from RIDE 0.54, you can also create a desktop shortcut during installation on Windows.

On Windows running ride.py requires having <PythonInstallationDir>\Scripts on PATH. If pybot command to run tests with Robot Framework works you should be fine. If it does not, see Robot Framework installation instructions for more information about setting PATH. When RIDE starts, you will see the UI as shown below. This screenshot has a MaharaDemoTest created by RIDE Program.



Selenium Robot Framework Introduction

Robot Framework is a generic test automation framework for acceptance testing and acceptance test-driven development (ATDD). It has easy-to-use tabular test data syntax and it utilizes the keyword-driven testing approach. Its testing capabilities can be extended by test libraries implemented either with Python or Java, and users can create new higher-level keywords from existing ones using the same syntax that is used for creating test cases. (Source: http://robotframework.org/).
Robot framework project is hosted on Google code, where users can find technical documentation and source code. Robot framework Python language bindings are hosted at Pypi
Robot framework has following features:
  • Framework architecture is clear. Robot Framework has a modular architecture that can be extended with bundled and self-made test libraries.
  • Framework is easy. When test execution is started, the framework first parses the test data, and then it utilizes keywords provided by the test libraries to interact with the system under test. 
  • Framework test result is user friendly. Test execution is started from the command line. As a result, users get report and log in HTML format as well as XML output. 
  • Framework is modular as listed below: 
  1. Test Date (test data is defined in files using robot framework syntax)
  2. Robot Framework ( framework application)
  3. Test Libraries (robot framework has common libraries , see the link
  4. System Under Test (this is the test application) 
Selenium Robot Framework is using Selenium2Library for Selenium 2 Web Driver or SeleniumLibrary for Selenium Remote Controller API with robot framework. For details, visit the link
Robot Framework and Selenium libraries are open source, so users can use it without any additional cost. 
Selenium Master will guide you how to use Selenium2Library with robot framework to complete test automation of your application under test.

Thursday, January 7, 2016

Getting Started with FitNesse

Now the world is moving to Agile. Early and continuous feedback is imperial for any scrum team. Because the world is changing, the mindset of testers also needs to be changed.
Instead of “finding bugs, breaking software, measuring requirement”, testers are now thinking of “delivering the quality, right at first time, test without the UI or test even before the UI is available”.

Testers are also now required to respond to change, and hence it is important to come out of black box testing technique and not waiting until the UI is developed; instead, begin testing the intermediately deliverables as well.

 

But WHY?


“NOW THIS IS VERY AGILE PERSPECTIVE”.
Whenever we build software, the lowest layers of tests are kept at the unit/component level. Unit tests are carried out by the development team. These unit tests are very much technology oriented and mostly written in the same language in with the system under test is written.
These unit tests are written with “X unit” test tool. We say in the testing world that if our unit test is rock solid, defects are identified much earlier and the testing above the unit testing layer becomes easy in a stable environment. And when we talk in Agile, we say that if a team is mastered the art of TDD (Test Driven Development), the Unit level tests provide the quickest feedback.
The layer above the unit/component layer is the Acceptance tests layer which is carried out the business. These are functional tests have more coverage than the unit tests and are most often executed by the non-developers. These tests test the layer behind the presentation layer or the APIs. These APIs or methods when are tested, gives a quick feedback and by the time GUI is developed, most of the functionalities are tested.

FitNesse is an example of this Automated Acceptance Tests Layer.

 

What is FitNesse?

FitNesse is “A fully integrated standalone wiki, and acceptance testing framework”. It is Open source, wiki web server. Wiki- because it allows creating your own web pages on which test tables are created. These test tables are nothing but the test data.
Its intention is to support agile style of black box acceptance and regression testing. It’s also a collaboration tool because testers collaborate with developers to prepare the test suite.

 

Why should I use FitNesse?

Agile test team can use FitNesse to prepare test suits that will test the methods in the code. FitNesse is analogous to Junit in a way that it also tests the methods, but it’s different than Junit because the tests are in the form of simple tables which can be used by both developers and non-developers.

Benefits:
  • Early feedback, by executing the automated acceptance tests as often as required.
  • Tests results are deterministic because they are highlighted in either Red or Green.
  • Test data can be designed to suite the quality needs.
  • The tests are written in simple language and easy to understand as they are written in tabular form.
  • These tables are defined in terms of input and expected outputs.
  • See all FitNesse features here.

 

So what all can I create?


In FitNesse you can create Tests and Suite. The terms are very much the same as used in the testing world. Tests are single script and suit is collection / group of tests. When you create a suit and execute it, the advantage is that all the tests in that suit are executed. So you need a proper planning to arrange your tests in suit.

 

Downloading and Configuring FitNesse:

=> To download FitNesse, Click Here


Download the latest version of fitnesse-standalone.jar and save it to your local drive.





Open command prompt and execute the jar file. For ease, I have created a batch file:



Once the jar file is executed, FitNesse is started as shown below: (click on image for enlarged view)
To open FitNesse, open your browser and type: http://localhost:<portnumber>. In this case the port number is 2222.

The page received is shown below: (click on image for enlarged view)





So here, if you can see the Tests dropdown, we can create a “Suite page” as well as a “Test Page”. When you create a suite, all the test scripts within that suite will be executed.
For explanation purpose, I am taking an example of Test Page.

FitNesse Example – The stuff to Test:

As of now, we are testing a simple calculator program shown below.
Here is the code in java, which has 4 methods:
  • addition ()
  • minus ()
  • multiply ()
  • divide ()
(Please see that FitNesse works with any language of your choice. For explanation, I have used java)
This code in FitNesse world is called “Fixture”.
Fixtures are nothing but the sample code – or a link between FitNesse and the application under test. So whenever we want to test a method, we have to write a fixture and this fixture will invoke, and test the method.

So the “Fixture” code for our example is as follows:



package calc;

public class Calculator {

   

    private int first,second;

     public void setFirst(int first)

    {

    this.first=first;

    }

   

    public void setSecond(int second)

    {

    this.second=second;

    }

   

    public int addition()

    {

    return (first+second);

    }

   

    public int minus()

    {

    return (first-second);

    }

   

    public int multiply()

    {

    return (first*second);

    }

     public float divide()

    {

    return (first/second);

}

    }








We would need the class file, so make sure that you compile it.

Writing your test in FitNesse:

Step #1) Let’s go back to the browser where we have the FitNesse front page.
In the front page, click on “Test Page”, enter the name of the test and click on “Save” button. In our case, it’s “Calculator”







Step #2) In your URL, append the name of your test with a dot “.” Operator.
Like: http://localhost:2222/FrontPage.Calculator



 Step #3) Click on the Edit button and enter the lines shown below
 




Here are the Lines entered:

!define TEST_SYSTEM {slim}
!path D:\Lifefray\FitnesseTests\bin
!| import |
| calc |
| calc.Calculator |
| first | second | addition? | minus? | multiply? | divide? |
| 4 | 2 | 6 | 2 | 8 | 2.0 |
| 10 | 5 | 15 | 5 | 50 | 2.0 |
| 10 | 10 | 20 | 0 | 100 | 1.0 |



Let’s understand the lines one by one.

a) The first line says FitNesse to use SLIM test system.
(SLIM – Stands for Simple List Invocation Method. By saying SLIM test system, all the table processing is done by FitNesse. SLIM has SLIM Runner and SLIM Executer. SLIM Runner breaks the test pages into simple instructions and these instructions are passed to SLIM Executer which directs the fixture code to call the system under tests)

b) The second line defines the location of the class file. In this case, the java code is compiled and the class file is kept at location “path D:\Lifefray\FitnesseTests\bin

c)The third & fourth lines states importing of package name "calc" . Fifth line is the name of the Class with Package name. In our case class name is “Calculator

d) Now comes the sixth line:
The first two columns |first|second| are the parameters or the inputs to the java method.
The next 4 columns which are followed by “?” addition?|minus?|multiply?|divide?| are the methods in the java class. These methods will return a value which would be compared against the expected values.

e) The lines:
|4   |2     |6       |2     |8       |2.0   |
|10  |5     |15       |5     |50     |2.0   |
|10   |10   |20       |0     |100     |1.0   |
Are the test cases or I should say Test data for our method.
The first line:
|first|second|addition?|minus?|multiply?|divide?|
|4   |2            |6            |2            |8                |2.0   |
Will take 4 as first parameter and 2 as second parameter and will pass these two values in the addition method of the java class. The method will execute and will return a value. This returned value will be compared against the expected value written under the “addition?” which is |6             |
In the similar fashion, FitNesse will pass the first 2 parameters in the minus? Method of the java class and returns a value. This value will be compared against the expected value against |2         |
In the same way multiply? and divide? will work by taking the values of the first and second parameter and returns value which are compared against |8           |2.0   | respectively
In the similar fashion, the below 2 rows (or I should say, the test cases) are executed.
|10   |5     |15       |5     |50       |2.0   |
|10   |10   |20       |0     |100     |1.0   |


Step #4) Once you have edited your tests, click on the save button and your page will look like:



Step #5) To run the tests, Click on the Test button, and we get the result as follows: (click on image for enlarged view)





For the first row (which is our first test case), the green color highlights that the values, returned from the method addition (), minus (), multiply () and divide () are matching with what is expected i.e. 6, 2, 8 and 2.0 respectively. Similarly for the second row (which is second test case) all the values returned from the methods are matching.

Step #6) Now to demonstrate, let me change few of the expected values to some other values (the values are incorrect, but I have done it purposefully for explanation)


As of now I have:
  • Changed the expected values for minus() for the second test case
Step #7) Run the test by clicking the “Test” button. The above tests should fail. (click on image for enlarged view)

The red color highlights that these tests failed.

Some insights on Fixture/Table styles:

We have seen that In FitNesse the tests are executed by executing rows in a table. Hence to execute different kinds of tests (or I should say to test different kinds of methods), we would need to use different kinds of tables. We use the below Fixture/table styles most often:
  • Column Fixture – is most widely used (and is used in the above example). Here the rows of data represent different sets of input and its expected output.
  • Row Fixtures – It is used for testing queries which returns some set of values.
  • Action Fixtures – It is used to run tests for sequence of events. These events can be like clicking on a button, checking values

 

Recommendation:

I have tried to demonstrate the concepts, so that we can start exploring more on FitNesse. The tester’s mindset also needs to be changed and have to be broadened. We have to stop restricting ourselves to look inside the code. I feel; ultimately we are testing the code, so why don’t we try to see the code and test then and there?
Start sharpening your programming skills and emphasis more on building the logic and rather learning syntax. Once you are well versed with programming concepts and have practice on implementing it, exploring FitNesse will become easier.

 

Conclusion:

Testing in agile comes in 4 flavors:
  1. Automated Unit testing – By using Junit
  2. Automated acceptance verification test – By using FitNesse
  3. Automated UI / regression tests – by using Selenium or QTP
  4. Manual testing

We should try to push maximum of our testing in the unit and the acceptance layer. Till now we have been trying to keep most of our testing for the UI layer using tools like QTP and Selenium, but the disadvantage here is that the these functionalities could not be tested unless the UI is developed. By the time you find a defect, the developers have moved into some other feature development.
On the other hand, if we can test the APIs soon after it has been written, developers can fix it instantly. This would also result in less effort when we test the GUI. Because all the functionalities are tested, testing the GUI becomes easy.


With Agile, the mindset of testers also needs a change and they have to come out of their routine set of testing and now you should look inside code and try to identify defects even the UI is not available.





Note - Above article published first appeard in http://www.softwaretestinghelp.com. Some updates done from my side i.e Configurations changes, code changes with new screenshots which will help for new users without any errors or minimum errors.