Wait for Element instead of waiting for page to load

Lot of time in selenium you are waiting for the page to load after an action on the page. There is no method in selenium to wait for an element for a specific time. The default timeout for most of the wait methods is 30 secs which is too long.

Here is a method you can use if you want to wait for an element for a specific time and then fail if the element is not present on the page. The following methods checks for the element identified by locatorString once every sec and returns true as soon as the element is found on the page. It fails if the element is not present after the desired time.

    public boolean waitForElement(Selenium selenium, String locatorString, int waitFor) throws Exception {
        if (0 >= waitFor) {
            //  Set default if no time specified.
            //  Setting it to 10 will set the total wait time  to 10 seconds 
            waitFor = 10;
        }

        Date startTime = new Date();
        Date currentTime;
        long elapsedTime = 0;
        while (false == selenium.isElementPresent(locatorString)) {
            currentTime = new Date();
            long sleepTime = 1000;

            elapsedTime = currentTime.getTime() - startTime.getTime();
            if (elapsedTime > waitFor * 1000) {
                throw new Exception("Element " + locatorString + " not found");
            }

            Thread.sleep(sleepTime);
        }
       System.out.println("Element " + locatorString + " found in " + (elapsedTime / 1000) + " seconds");
        return true;
    }

Categories: How-To, Selenium, Tutorial

Amazing story of pride, honor and sacrifice

February 28, 2010 1 comment

This is one of the most weird and inspiring stories I’ve ever heard. About 15 Km. west of Jaisalmer a city in western Rajasthan lies the ruins of a village which was called Kuldhara.

Kuldhara was once a very prosperous village of paliwal bhramins. Paliwal bhramins were a very prosperous clan and were known for their business acumen and agricultural knowledge. They had mastered the art of growing crops in the desert. But one night in 1825 all the people in Kuldhara and nearby 83 villages vanished in dark. Why did the villagers decide to leave their settlement after having lived there for more that 7 centuries.

The evil dewan or the minister in the ruling kingdom saw the young daughter of the village chief. He wanted to marry her and forced the village chief for marrying his daughter. He gave them a deadline for the marriage after which he would forcefully enter the village and take their daughter. All the chiefs of 84 villages met one night and for pride and honor decided to leave the villages in the dark of the night.

Nobody knows where they went but it is believed that they settled near Jodhpur another city in western Rajasthan. It is also said that they left a curse the villages before leaving that it will bring death to anybody who tries to live in those villages. Several people tried to live in the villages but no one succeeded.

Today the ruins of these villages can still be seen in western Rajasthan and are now tourist sites.

Categories: India, Travel Tags: , ,

How to use maven plugin to add user-extensions.js in selenium-server.jar

February 4, 2010 4 comments

If you are using maven for your selenium tests and you have extended Selenium using user-extensions.js, You can add following to your pom.xml. This plugin will run after your code has compiled. It will unzip selenium-server.jar and add you user-extensions.js to it core/scripts folder and then repackage it back.

pom.xml

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.1</version>
<executions>
 <execution>
    <phase>install</phase>
    <configuration>
    <tasks>
     <unzip src="${project.build.directory}/lib/selenium-server-1.0.1.jar"
         dest="${project.build.directory}/temp" overwrite="true"/>
     <unzip src="${project.build.directory}/lib/selenium-core-1.0.1.jar" 
         dest="${project.build.directory}/temp" overwrite="true"/>
     <unzip src="${project.build.directory}/lib/selenium-server-coreless-1.0.1.jar" 
         dest="${project.build.directory}/temp" overwrite="true"/>
     <copy file="${project.build.directory}/resources/user-extensions.js"
         tofile="${project.build.directory}/temp/core/scripts/user-extensions.js" 
         overwrite="true"/>
     
     <jar basedir="${project.build.directory}/temp" 
         manifest="${project.build.directory}/temp/META-INF/MANIFEST.MF"
         destfile="${project.build.directory}/temp/temp.jar"/>

     <copy file="${project.build.directory}/temp/temp.jar"
         tofile="${project.build.directory}/lib/selenium-server-1.0.1.jar" 
        overwrite="true"/>

     <delete file="${project.build.directory}/lib/selenium-server-coreless-1.0.1.jar"/>
     <delete file="${project.build.directory}/lib/selenium-core-1.0.1.jar"/>
     <delete dir="${project.build.directory}/temp"/> 
    </tasks>
    </configuration>
    <goals>
        <goal>run</goal>
    </goals>
 </execution>
</executions>
</plugin>

Once you clean and build your project you should have selenium-server.jar which has your updated user-extensions.js inside.

Categories: How-To Tags: , , ,

Using captureEntirePageScreenshot with Selenium

February 3, 2010 12 comments

Selenium provides a way to to capture the entire page displayed in the browser as a screenshot and save it as a png file on the disk. This functionality is only available in Selenium RC and is available for firefox (*chrome mode) and in IE (*iexploreproxy ) using Experimental utility Snapsie.

// Capture screen shot 
selenium.captureEntirePageScreenShot("screenshot.png");

Firefox is supported out of the box but it needs little setup for it to work in IE. To use the method in IE go through following steps.

1. Download and install Microsoft VC++ Redistributable Package

2. Download and unzip Snapsie from http://snapsie.sourceforge.net/
Copy Snapsie.dll to windows/system or windows/system32 folder Register Snapsie.dll using “regsvr32 Snapsie.dll

3. Snapsie download comes with some tests, open the test files and try to save the page in browser as a screenshot. You will have to allow the browser to install the active-x control since snapsie is an active-x control.

4. Add the site you are testing and want to capture to Trusted Sites in Internet Explorer otherwise the active-x control will not be loaded when you run your site through RC and try to capture the screenshot.

To programatically add your site to Trusted Sites for Internet Explorer add the following keys to windows registry. Save the contents in .reg file and double click to run or call from a windows batch script. The following example adds http://www.aol.com to the trusted sites.

ie-trusted-sites.reg

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains]
@=""

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\aol.com]

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\aol.com\www]
"http"=dword:00000002

If you run your tests on a fresh windows image every time, here is a sample .bat file you can use to install all the above things and start your selenium tests.

launch.bat

@echo off

REM Install in silent mode

C:\selenium-tests\vcredist_x86.exe /Q

C:\windows\regedit.exe  /s C:\selenium-tests\ie-trusted-sites.reg

copy C:\selenium-tests\snapsie-0.2\Snapsie.dll  C:\windows\system32\Snapsie.dll

C:\windows\system32\regsvr32.exe /s  C:\windows\system32\Snapsie.dll	

REM Start your tests here
Categories: How-To Tags: , ,

Extending selenium using javascript

January 25, 2010 Leave a comment

Functionality of selenium can be easily extended using JavaScript functions. JavaScript methods can be added in user-extensions.js to the Selenium prototype object and can be called from either IDE or selenium RC.

user-extensions.js

Selenium.prototype.method_name  = function( param1 ) {
   // do your specific stuff here
   alert(param1);
}

Now either start selenium server with the userExtensions option and pass it your user-extensions.js

java -jar selenium-server.jar -userExtensions user-extensions.js

Or you can use the following code in java to call this newly added method.

// Instantiate  HttpCommandProcessor and selenium object 
HttpCommandProcessor proc ;
proc = new HttpCommandProcessor("localhost", 4444, "*firefox", "http://yoursite.com/");
DefaultSelenium selenium = new DefaultSelenium(proc);

// Define input parameter for our method 
string[] inputParams = {"Hello World"};

// Call the method 
proc.DoCommand("method_name", inputParams);

Another way to call the above method could be by using getEval() method of selenium object

// call the method using the getEval method
selenium.getEval("this.medhod_name(\"Hello World\")");
Categories: How-To Tags:

Hello world!

August 14, 2006 2 comments

Hello fellow internet surfers,  I’m finally writing a blog after thinking about it for a long time. Let’s see how it goes 🙂

Categories: Uncategorized