Thursday, October 28, 2010

Executing Tests in multiple environments using Selenium Grid

                      Our Business Team came up with a requirement of having support for multiple Browsers like Google Chrome, Internet Explorer 6, Internet Explorer 7 and Internet Explorer 8. Till yesterday I was happily executing my Selenium tests in firefox and now I have to find a way in which I can run my test cases in different browsers. First thought I got is to install all the browsers on my laptopn and test. But unfortunately I realized that I cannot install all the Internet Explorer versions on single machine. So I have started exploring different options and came across Selenium Grid. It is an extension of Selenium Core. The idea is simple there will be a Master and many slaves. Master will give the commands and slaves will be executing these commands.

Process of setting up Selenium Grid.

These steps have to done both on Master and Slave machines. 
  1. Download and install the Java JRE. Verify the installation by issuing a command "java -version" on command prompt.
  2. Download and extract Apache Ant and put the path in PATH environment variable. Verify the installation by issuing a command "ant -version" on command prompt.
  3. Download and extract Selenium Grid.
You are done with the setup. :)

Making a machine master
  1. In Command prompt from the folder where you have extracted "Selenium Grid" run "ant launch-hub"
  2. To Verify just got to http://localhost:4444/console from your favourite browser
  3. There will be 3 tables on the Web Page. First shows Environments defined in file named "grid_configuration.yml" located at the root directory of the extracted location of selenium grid. the second column shows what remote controls are available and the third column shows what remote controls are currently running tests.
  4. For our purpose let us add some new environments for Internet Explorer 6, Internet Explorer 7, Internet Explorer 8. Copy paste the following lines at the end of "grid_configuration.yml"
       - name:    "*iexplore6"
         browser: "*iexplore"
       - name:    "*iexplore7"
         browser: "*iexplore"
       - name:    "*iexplore8"
         browser: "*iexplore"
You are done with Master Setup. :)

Making a machine which is having Internet Explorer 6 installed as slave

 In Command prompt from the folder where you have extracted "Selenium Grid" run
ant -Denvironment=*iexplore6 launch-remote-control -DhubURL=http://192.168.0.111:4444 -Dhost=192.168.0.112
where 192.168.0.111 is the ipaddress of Master and 192.168.0.112 is the ipaddress of slave. One can use hostname instead of IPAddresses if they are added in DNS.

Making a machine which is having Internet Explorer 7 installed as slave

 In Command prompt from the folder where you have extracted "Selenium Grid" run

ant -Denvironment=*iexplore7 launch-remote-control -DhubURL=http://192.168.0.111:4444 -Dhost=192.168.0.113
where 192.168.0.111 is the ipaddress of Master and 192.168.0.113 is the ipaddress of slave. One can use hostname instead of IPAddresses if they are added in DNS.

Making a machine which is having Internet Explorer 8 installed as slave
    
 In Command prompt from the folder where you have extracted "Selenium Grid" run
ant -Denvironment=*iexplore8 launch-remote-control -DhubURL=http://192.168.0.111:4444 -Dhost=192.168.0.114
where 192.168.0.111 is the ipaddress of Master and 192.168.0.114 is the ipaddress of slave. One can use hostname instead of IPAddresses if they are added in DNS.

Verify the setup at http://localhost:4444/console. You should see something like

You are done with Slaves Setup. :)

Important: You have to use *iexplore6 or *iexplore7 or *iexplore8 in your Test Cases instead of *iexplore.
I am using ROBOT Framework to run my selenium tests. So I can run tests using
pybot.bat -l Run1.html -o Run1out.xml -r Run1Report.html "C:\Users\yagnanarayana dande\Desktop\TestsIE6.html"
Remember Pybot cannot run in parallel. So start different pybots per environment like

pybot.bat -l Run1.html -o Run1out.xml -r Run1Report.html "C:\Users\yagnanarayana dande\Desktop\TestsIE6.html"
pybot.bat -l Run1.html -o Run1out.xml -r Run1Report.html "C:\Users\yagnanarayana dande\Desktop\TestsIE7.html"
pybot.bat -l Run1.html -o Run1out.xml -r Run1Report.html "C:\Users\yagnanarayana dande\Desktop\TestsIE8.html"

To remove any slave from your grid due to reasons like machine died during execution the use
 http://192.168.0.111:4444/registration-manager/unregister?host=192.168.0.113&port=5555&environment=*iexplore7

Wednesday, October 13, 2010

Unit Tests for JavaScript using JsUnit

Yesterday my boss asked me to test our Product UI for any script errors. As our application is full of JavaScripts I decided it would be wise to write some Unit Tests in JsUnit.

For those who are new to JavaScript let me give you some information. As per Wikipedia 
"JavaScript is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites.".
Regarding the popularity of Java Script trends.builtwith.com says
708,651 websites using this within the top million sites on the internet and an additional extended total of 4,797,491 websites that are using Javascript.
And why my decision is wise?
  1. Runs on “most” browser/platform combinations
  2. Preserves the standards of a typical XUnit framework
  3. Open source - Free!!!
  4. Can be hosted on a web server so that tests can be run across enterprise without different setups
Some facts about JsUnit
  1. Unit tests in JsUnit are called Test Functions
  2. Test Functions live in an HTML page called a Test Page
  3. A Test Page is any HTML page that has a JavaScript “include” of jsUnitCore.js
  4. jsUnitCore.js provides the assertion functions of JsUnit, e.g. assertEquals(comment, arg1, arg2)
  5. JsUnit supports setUp() and tearDown()
  6. A Test Suite Page declares a suite() function that returns a JsUnitTestSuite for grouping Test Pages
  7. The JsUnit testRunner.html page runs Test Pages
Installation:

      For most of the open source tools I worked with, installation is a night mare. But JsUnit is pretty straight forward and is just 2 steps
  1. Download installation zip from http://www.jsunit.net/
  2. Unzip file in some local directory or on a webserver to host JsUnit
and we are done with installation. Really simple right :)
To verify the installation
  1. go to http://xx.xx.xx.xx/jsunit/testRunner.html, where xx.xx.xx.xx is the IPAddress or Hostname of the server on which JsUnit is hosted
  2. Enter xx.xx.xx.xx/jsunit/tests/TestPageTest.html
  3. Hit run button
  4. Progress should change to green

My First Test Case

I would like to unit test below function

function multiply(arg1, arg2) {
    return arg1*arg2;
}

Write a html page as below and put it in tests folder under JsUnit base folder

<html>
<head>
<script language="JavaScript" src="../app/jsUnitCore.js"></script>
<script language="JavaScript" src="math.js"></script>
<script language="JavaScript">

function testWithValidArguments() {
assertEquals("2 times 3 is 6", 6, multiply(2, 3));
assertEquals("Should work with negative numbers", -20, multiply(-4, 5));
}

function testWithInvalidArguments() {
assertNull("null argument", multiply(2, null));
assertNull("string argument", multiply(2, "a string"));
assertNull("undefined argument", multiply(2, JSUNIT_UNDEFINED_VALUE));
}

function testStrictReturnType() {
assertNotEquals("Should return a number, not a string", "6", multiply(2, 3));
}

</script>
</head>
<body>
This is a Test Page for multiplyAndAddFive(arg1, arg2).
</body>
</html>

Thats all you have to do and now you can run your tests using http://xx.xx.xx.xx/jsunit/testRunner.html.

-------------------------------------------------------------------------------------------------------------------  
 Testing can be used to show the presence of bugs,but never to show their absence!
   Edsger Dijkstra