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.

Friday, December 11, 2009

ANT with ReportNG, TestNG and Eclipse

After exploring the TestNG, I came to know about two more things that can help in Selenium Automation. One is ANT and another is TestNG. Let’s have a look what both of these are.
ReportNG
ANT
Download Demo Project
Run Demo Project

ReportNG
ReportNG is a plug-in for TestNG which generates nicer reports than TestNG. TestNG has some interfaces that allow you to modify the behavior of TestNG, they call it listeners. One of the listeners is for reporting and ReportNG has used these listeners to make the reports better.
You can check out the sample reports here.
Sample Report

ANT
Ant is an Apache java based build tool, just like make but better than that. It is used to automate your JAVA building process. You can use ANT to do so many tasks but these are few which would help in running TestNG test cases.
  • Start the selenium server
  • Compile the java files and Run it using TestNG
  • Overwrites the TestNG report with ReportNG plug-in
  • Open the Report in browser after completation of test.
Following is the ANT file, which will perform above all task.

Just change the paths of jar file to location on your machine.
<project name="ANT_with_ReportNG" default="run" basedir=".">

 <property name="classes.dir" value="bin" />
 <property name="src.dir" value="src" />
 <property name="report.dir" value="reports" />
 <property name="logs.dir" value="logs" />
 <property name="browser" value="C:/Program Files/Mozilla Firefox/firefox.exe"/>

 <path id="libs">
  <fileset dir="lib">
   <include name="*.jar"/>
  </fileset>
  <pathelement path="${basedir}\${classes.dir}"/>
 </path>

 <target name="run">
  <antcall target="startSeleniumServer"/>
  <antcall target="init"/>
  <antcall target="compile"/>
  <antcall target="runTests"/>
  <antcall target="stopSeleniumServer"/>
  <antcall target="openReport"/>
  </target>
 
 <!-- Start the server -->
 <target name="startSeleniumServer">
  <echo>Starting Selenium Server...</echo>
  <java jar="lib\selenium-server-1.0.2.jar" fork="true" spawn="true">
   <arg line="-singlewindow -log ${logs.dir}\selenium_server_log.txt"/>
  </java>
  
 </target>
 
 <target name="stopSeleniumServer">
  <echo> Trying to stop selenium server ... </echo>
  <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
   dest="${logs.dir}\selenium_server_shutdown_result.txt" ignoreerrors="true" />
  <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
 </target>
 
 <!-- Delete old data and create new directories -->
 <target name="init" >
  <echo>Initlizing...</echo>
  <delete dir="${classes.dir}" />
  <mkdir dir="${classes.dir}"/>
  <delete dir="${report.dir}" />
  <mkdir dir="${report.dir}"/>
  <mkdir dir="${logs.dir}"/>
 </target>

 <!-- Complies the java files -->
 <target name="compile">
  <echo>Compiling...</echo>
  <javac includeantruntime="false" debug="true" srcdir="${src.dir}" destdir="${classes.dir}"   classpathref="libs" />
 </target>

 <!-- Runs the file and generates Reportng report -->
 <target name="runTests" description="Running tests" >
  <echo>Running Tests...</echo>
  <taskdef resource="testngtasks" classpathref="libs"/>
  <testng outputDir="${report.dir}"
    haltonfailure="true"
    useDefaultListeners="false"
    listeners="org.uncommons.reportng.HTMLReporter"
    classpathref="libs">
   <classfileset dir="${classes.dir}" includes="**/*.class" />
  </testng>
 </target>

 <!-- Open the report in browser.-->
 <target name="openReport"> 
  <exec executable="${browser}" spawn="yes"> 
   <arg line="'${report.dir}\html\index.html'" /> 
  </exec>
 </target>
</project>


Download Demo project
you can download the demo project from the following location
Download

Run Demo Project.
You can run project in two waya. From command prompt and from IDE. 

To Run the project from the command prompt do following steps
But in order to run ant file from command prompt you need to have ANT on your machine and it's path in ur PATH variable. 
  • Open the command prompt and navigate to extracted folders. 
  • Now type the ant command

follow the given steps to run the project from eclipse IDE
  • Extract the ZIP file
  • Open the Eclipse and navigate to file->New ->Java Project...
  • Choose "Create project from existing source" and browse to the extracted folder.
  • Now to run project open the Ant build.xml file and right click on it and choose Run as->Ant Build