Thursday, December 30, 2010

Selenium - Handle Upload Dialog


Following is the Autoit Script to handle upload dialog box.

;~ File_Upload_FF.au3
;~ Purpose: TO handle the file Upoload dialog
;~ Usage: File_Upload_FF.exe "Dialog Title" "Path of the file"
;~ Create By: Gaurang Shah
;~ Email:

AutoItSetOption("WinTitleMatchMode","2") ; set the select mode to select using substring

if $CmdLine[0] < 2 then
; Arguments are not enough
msgbox(0,"Error","File_Upload_FF.exe 'Dialog Title' 'Path of the file'")

; wait Until dialog box appears
WinWait($CmdLine[1]) ; match the window with substring
$title = WinGetTitle($CmdLine[1]) ; retrives whole window title


You need to download Autoit in order to run this script else you can download the EXE uploaded here.

To know how to call this EXE from JAVA read the following blogpost.

Monday, October 4, 2010

Selenium-Select All Checkboxes

Few times back i just come across the situation where i require to select all the checkboxes on page. To do that one by one will make code to huge so i think about using loops for that. And here is the solution.

The following code will open the page that has 6 checkboxes and will check them all one by one.

import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class TestCheckbox {

private Selenium selenium;

public void setUp(){

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

public void selectAllCheckboxes() throws InterruptedException{"");

int totalCheckboxes = selenium.getXpathCount("//input[@type='checkbox']").intValue();
for (int i = 1; i < totalCheckboxes+1; i++) {





selenium checkbox example

First Checbox

Second Checkbox

Third Checkbox

Fourth Checkbox

Fifth Checkbox

Sixth Checkbox


Thursday, July 1, 2010

TestNG - Take Screenshot of Failed Test Cases.

In my project I required to take to take the screenshot of the failed test cases. I initially searched for ready made listeners that can do this. Bug when i failed to find any i decided to write down my own.

I also wanted the screenshots to be embedded in ReportNG report. But HTML tags in ReportNG report is by default disabled. You require to set its system property to enable to HTML Tags in report.

The following listener will take the screenshot of the failed testcases and will embed that in your ReportNG report but for that you require to change in your TestNG task in your build.xml.

Listener to Take the screenshot of failed testcases
package test.Lib;


import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

public class Screenshot extends TestListenerAdapter {

public void onTestFailure(ITestResult result) {
File file = new File("");


Reporter.log("screenshot saved at "+file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
Reporter.log("<a href='../"+result.getName()+".jpg' <img src='../"+result.getName()+".jpg' hight='100' width='100'/> </a>");

public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped

public void onTestSuccess(ITestResult result) {
// will be called after test will pass 


BaseClass: BaseClass is the class where my setup(@BeforeClass) and teardown(@AfterClass) method is declared. I have taken selenium object is static so i can access it directly by its classname.

To use this listener you need to mention that in your Testng.xml file as follows.

<suite name="TestNG Take Screenshot of failed Test cases">
<listener class-name="Screenshot" /> 
<test verbose="6" name="Summery and Submit Page Page">
<class name=test.Lib.testScreenshot" />

Changes Require in TestNG task to enable HTML tags in ReportNG report.
<project name="ScreenShotFailedTestcases" basedir=".">
<target name="TestNG" depends="compile">
<taskdef resource="testngtasks" classpathref="lib.classpath"/>
<testng outputDir="${report.dir}" 
<!-- <classfileset dir="${classes.dir}" includes="**/*.class" /> -->
<xmlfileset dir="${basedir}" includes="testng.xml"/>  
<sysproperty key="org.uncommons.reportng.escape-output" value="false"/>

Tuesday, June 1, 2010

ANT Exec task - Open New Command Prompt

How to Open New Command Prompt Using ANT task. ??

Currently we were working on ANT script to run the Selenium Test. And we think it would be nice if ANT script also starts the web server before executing test cases.

To start the web application we require to fire a command from command prompt. we used the Exec task task and it was working fine but problem was it was logging on the same command prompt from which we have run ANT task. We require to open the new command prompt for that.

We tried the cmd /k option but it was not working. Finally we got the solution and it is as below.

 Starting application 

In the script appDir is the Directory path where your server resides.
and grails run-app is the command we need to run the command prompt to launch the web application.