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!

Technical Debt