Thursday, December 30, 2010

Selenium - Handle Upload Dialog

Download
Handle_File_Upload.exe



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: shahgomji@gmail.com
;--------------------------------------------------------------------

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'")
Exit
EndIf

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

WinWaitActive($title)
ControlSetText($title,"","Edit1",$CmdLine[2])
ControlClick($title,"","&Open")

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.
http://qtp-help.blogspot.com/2009/07/selenium-handle-dialogs.html

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;

@BeforeClass
public void setUp(){

selenium = new DefaultSelenium("localhost", 2323, "*chrome", "http://qtp-help.blogspot.com");
selenium.start();
selenium.open("/");
}

@Test
public void selectAllCheckboxes() throws InterruptedException{
selenium.open("http://qtp-help.blogspot.com/2010/10/selenium-checkbox-example.html");

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

if(!selenium.isChecked("//input[@id='checkbox"+i+"']")){
selenium.click("//input[@id='checkbox"+i+"']");

}
}

}

}

selenium checkbox example

First Checbox

Second Checkbox


Third Checkbox


Fourth Checkbox


Fifth Checkbox


Sixth Checkbox




gaurang00hdhd

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 java.io.File;

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

public class Screenshot extends TestListenerAdapter {

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

Reporter.setCurrentTestResult(result);
System.out.println(file.getAbsolutePath());
Reporter.log(file.getAbsolutePath());

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>");
BaseClass.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
Reporter.setCurrentTestResult(null);
}


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

@Override
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">
<listeners>
<listener class-name="Screenshot" /> 
</listeners>
<test verbose="6" name="Summery and Submit Page Page">
<classes>
<class name=test.Lib.testScreenshot" />
</classes>
</test>
</suite>


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}" 
haltonfailure="false"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter"
classpathref="lib.classpath">
<!-- <classfileset dir="${classes.dir}" includes="**/*.class" /> -->
<xmlfileset dir="${basedir}" includes="testng.xml"/>  
<sysproperty key="org.uncommons.reportng.escape-output" value="false"/>
</testng> 
</target>
</project>

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.