Search This Blog


Monday, November 15, 2010

Extend Selenium Server with the help of Default Selenium

Default Selenium
DefaultSelenium is the base class that you instantiate early on in your test class to get the selenium object which you later use in almost every line of you test script.

Selenium selenium=new DefaultSelenium("localhost",4444,"*chrome","");

/*Creates a new DefaultSelenium object and starts it using the specified baseUrl and browser string*/
setUp("", "*firefox");"/");"nb15go_image");
does that ring a bell?

What do you mean by extending the default selenium?
Extending the default selenium means enhancing or customizing or modifying the existing features of selenium class. We are reusing the existing class after adding some enhancements so as to customize the existing features to our taste and requirement. Programming languages such as JAVA provide the facility of extending/modifying the behavior of a class without having to re-write the entire class. We can create a new class that inherits the attributes and methods of another. We don't need a copy of the original source code (as is the case with many other languages) to extend the usefulness of a class/library.

Why do I need to extend selenium?
If you want certain operations to be done every time before a step is executed then you would have to extend the default selenium. For example let’s say that you wish to wait 5 seconds after every step or you want to highlight the element before clicking on it or you want to record every step in log file or you want to poll for a element’s existence for a certain amount of time before performing an operation (click, type) on it. For all these to happen you would have to extend the default selenium class that you use in your RC test script.

Below is a simple example that demonstrates this concept. The class
MySelenium extends the class DefaultSelenium. The methods click and type of the base class have been over-ridden in such a way that the target element (which is to be clicked on or typed into) is highlighted thrice before the click or type operation. The class UsingMySelenium by the means of a simple example demonstrates the usage of our extended class.

Class MySelenium
package seleniumExtention;

import com.thoughtworks.selenium.DefaultSelenium;

public class MySelenium extends DefaultSelenium{

public MySelenium(String seleniumSeverHost, int port,String browserString,String autURL){
super(seleniumSeverHost, port,browserString, autURL);

public void click(String target) {
try {
for (int i=0;i<3;i++){ i="0;i<3;i++){">

Class UsingMySelenium
package seleniumExtention;

import org.junit.Test;
import org.junit.BeforeClass;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.SeleneseTestCase;

public class UsingMySelenium extends SeleneseTestCase{
public MySelenium selenium;

public void setUp() throws Exception {

SeleniumServer seleniumserver=new SeleniumServer();

/*using the customized selenium class that we created earlier instead of the default selenium*/
selenium=new MySelenium ("localhost",4444,"*iexplore","");


public void testSimpleGoogleSearch() throws Exception {"/");
selenium.type("q", "Aston Martin volante");"//input[@value='Google Search']");
selenium.waitForPageToLoad("5000");"xpath=/descendant::a[text()=' - The Cars']/em[text()='Aston Martin']/parent::a");

To make this work you need to do the following
1) Create a package seleniumExtention inside the java project.
2) Create the classes MySelenium & UsingMySelenium and copy paste the content as given above.
3) Run the testSimpleGoogleSearch() given in the class UsingMySelenium (right click Run As-->JUnit Test).

*Following jars need to be referenced

good luck with this and keep the questions coming!


Post a Comment