Thursday, April 28, 2011

Using Selenium for System and Regression Testing

Dears,

For past few days I was exploring the tool selenium it is very popular in the agile world for testing web applications. To describe simply:

  • Selenium can record our web activities [Currently supported only as firefox addon].
  • Recorded testcases can be saved in different languages [in our case Java as JUnit testcase].
  • Testsuites of these testcases can be run on any browsers automatically.[In our case Internet Explorer].

Recording:

Selenium IDE installation and use:

  • Install the Firefox addon, [selenium-ide-1.0.4.xpi]
  • Open URL www.google.com in firefox.
  • Click on tools-> Selenium IDE

Change Base URL textbox to reflect http://www.google.com/ if its not the case already.

  • Move your mouse the RED round icon at the top-right corner. [Click on it if it is not already recording].
  • Switch to google search window. Search for text Infosys
  • Right click any link in that results page and select verifyText present context menu item

  • Switch back to the selenium IDE window. You will notice the steps are recorded by selenium.

  • Click on the red button to stop recording. If you want you can just run the test case by pressing the green Arrow buttons in the toolbar of the window.
  • Now click File->Export testcase As in Selenium IDE and select Java[JUnit] – Selenium RC


  • Save this with .java extension. You must has a file like

package com.example.tests;

import com.thoughtworks.selenium.*;

import java.util.regex.Pattern;

public class Untitled extends SeleneseTestCase {

public void setUp() throws Exception {

setUp("http://www.google.co.in/", "*chrome");

}

public void testUntitled() throws Exception {

selenium.open("/");

selenium.type("q", "infosys");

selenium.click("btnG");

verifyTrue(selenium.isTextPresent("Infosys - Wikipedia, the free encyclopedia"));

}

}

This is the testcase which can be run automatically, we can customize the parameters and flow. Below I will explain to automatically run this recorded testcase in Internet explorer.

Running Remote Control:

  • For this we need to have selenium RC [remote control].
  • Get the same from selenium-remote-control-1.0.1-dist.zip
  • Unzip this in any location in your PC.
  • Open command prompt and change directory to location <Unzipped Location>\selenium-remote-control-1.0.1-dist\selenium-remote-control-1.0.1\selenium-server-1.0.1
  • Now run java -jar selenium-server.jar [note you are in the location where the .jar file is present]
  • You will see something like

C:\Narain\0_Applications\selenium-remote-control-1.0.1-dist\selenium-remote-control-1.0.1\selenium-server-1.0.1>java -jar selenium-server.jar

14:32:02.218 INFO - Java: Sun Microsystems Inc. 1.5.0_15-b04

14:32:02.218 INFO - OS: Windows XP 5.1 x86

14:32:02.264 INFO - v1.0.1 [2696], with Core v@VERSION@ [@REVISION@]

14:32:02.374 INFO - Version Jetty/5.1.x

14:32:02.374 INFO - Started HttpContext[/,/]

14:32:02.374 INFO - Started HttpContext[/selenium-server,/selenium-server]

14:32:02.374 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]

14:32:02.421 INFO - Started SocketListener on 0.0.0.0:4444

14:32:02.421 INFO - Started org.mortbay.jetty.Server@6b7920

  • This means selenium RC is running.

Running Test Cases:

  • Create new java project in eclipse.
  • Add the junit and Selenium lib files junit-4.8.1.jar and selenium-java-client-driver.jar to the buildPath
  • Just copy the saved java file into this project.
  • Right click and run as Junit test case.


I assume above gave you a introduction to the tool. You can google for more information. I have used this to run a complexusecase for all the customer in database in less than a minute and also I have customized to take screenshots for each page load eg.,

selenium.select("dbo_searchBy", "label=" + SearchBy);

selenium.type("searchByValue", searchByValue);

selenium.captureScreenshot("C:/Narain/1_Projects/1_GLP/5_Development/Sprint2/FunctionalTesting/Captures2/" + searchByValue +"SearchScreen.png");

selenium.click("checkEliBtn");

selenium.waitForPageToLoad("30000");

selenium.click("checkEliBtn");

selenium.waitForPopUp("CustomerComposition", "30000");

selenium.captureScreenshot("C:/Narain/1_Projects/1_GLP/5_Development/Sprint2/FunctionalTesting/Captures2/" + searchByValue +"CustomerComposition.png");

This way the testcases once recorded and customized can be run for several customers and can be run as regression when new functionalities are developed. I am working on automating all this using ANT and HUDSON to automatically build, deploy and test.

Thanks for your patience for reading this long Blog!

Tuesday, March 15, 2011

Java Unit testing

 

Java Unit Testing:

 

1.    Tool: JUnit.

2.    Process:

a.    Sources has the new folder structure added for this NGRP->UnitTesting.

b.    Use Eclipse to generate the unit test skeleton for your target class [All the screenshots are self explanative] ->>>> Look at the folder structure in clearcase

Naming Convention: <TargetClass>Test

Choose target method to test choose checkboxes as below

Note: Never add the cases to clearcase before testing locally. If eclipse plugin claims just cancel the dialog.

 

c.     Most of the cases it is not as straight forward to write unit test , sometimes methods calls other methods in different layer.

To unit test such methods we should start doing Refactoring.

Target Class:

class TestActionClass {

...

  public subscriptionTO callRules(subscriptionTO) {

    if (level=”G”){

subscriptionTO.level=”G”;

}

elseif(level=”A”){

subscriptionTO.level=”A”;

}

    RemoteService r = new RemoteService();

    r.callRules(subscriptionTO);

return subscriptionTO;

}

..

}

With the above example you cannot unit test as the function call will try to call RemoteService which needs server to be running and ofcourse application to be deployed.

 

Action-> Refactor the call to Remote method to make this method[callRemote] unit testable:

 

Refactored Class:

class TestActionClass {

...

  public subscriptionTO callRules(subscriptionTO) {

    if (level=”G”){

subscriptionTO.level=”G”;

}

elseif(level=”A”){

subscriptionTO.level=”A”;

}

    RemoteService r = createRemoteService();

    r.callRules(subscriptionTO);

return subscriptionTO;

}

..

protected RemoteService createRemoteService(){

return new RemoteService();

}

 

}

 

Now you can write a unit Testcase as follows:

Unit Test Class:

class TestActionClassTest {

MYObject sampleObject = new MYObject();

...

     @Before

public void setUp() throws Exception {

      sampleObject.level=”G”;       

}

 

@Test

public void testCallRules() {

        TestActionClass t = TestActionClass(){

        protected RemoteService createRemoteService(){

            return sampleObject;

             }

        };

}

..

}

 

As you can see above now I have overridden the createRemoteService method in my unit testcase, thus passing my mock object to test callRemote and avoid calling other layers.

 

Reference: http://www.ibm.com/developerworks/library/j-mocktest.html

 

Technical Debt