? QA Design Gurus: How to write JavaScript in Ant tool

Apr 3, 2015

How to write JavaScript in Ant tool

Below are the some examples lets you understand to write Java Script in ANT Tool.

Ex: To Calculate how much time its taken to run the target .

Step1: Calculate Start Time before executing target

Get the System Date and convert into Simple Date Format

<script language="javascript">
<![CDATA[

           importClass(java.text.SimpleDateFormat);
         

           fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");

           self.getProject().setProperty('startdateformat', fmt.format(new Date().getTime()));

       ]]>

</script>
<echo> time is ${startdateformat}</echo>
<echo> time is ${startTime}</echo>


Step 2.  Invoke Target. Ex:
<parallel threadcount="3">
<echo message="UseCase1" />
<echo message="UseCase2" />
<echo message="UseCase3" />
</parallel>

Step 3. Now Calculate End Time and Elapsed Time.
<script language="javascript">

<![CDATA[

importClass(java.text.SimpleDateFormat);
   fmt = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss a");
self.getProject().setProperty('enddateformat', fmt.format(new Date().getTime()));
 project.setProperty('elapsedTime', new Date().getTime() - startTime)  ]]> </script>  
<echo>End Date format : ${enddateformat} ms</echo>
<echo>End time: ${endTime} ms</echo>


Here Elapsed Time Calcuated in Milli Seconds. Inorder to convert milliseconds to Seconds use below snippit in Ant.

<math result="result" operand1="${elapsedTime}" operation="/" operand2="1000" datatype="double"/>
<echo>result is ${result}</echo>  




No comments:

Post a Comment