如何在ant build.xml的帮助下运行我的自动化脚本?

时间:2013-04-03 13:53:33

标签: java ant webdriver testng

我已经安装了ant,我已经在系统变量中设置了ANT_HOME。在系统环境变量中设置JAVA_HOME。将tools.jar文件复制到“JRE / lib”文件夹中。现在我应该在哪个文件夹中创建build.xml。我有基本的build.xml。在这个build.xml中,我在哪里提到要运行的脚本。例如,我的测试脚本在谷歌上搜索一些内容然后退出。 我正在使用java,testng在windows7上编写我的脚本

<?xml version="1.0"?>
<project name="sampleProject" basedir="." default="jar">
    <property name="src" value="ant-source"/>
    <property name="output" value="bin"/>

    <target name="compile" depends="create">
        <javac destdir="bin">
            <src path="${src}"/>
            <classpath refid="java"/>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <jar destfile="test.jar">
            <fileset dir="bin"/>
        </jar>
    </target>


    <target name="clean">
        <delete dir="${output}"/>
    </target>

    <target name="create" depends="clean">
        <mkdir dir="${output}"/>
    </target>

    <path id="java">
        <fileset dir="D:JarsHibernate">
            <include name="*.jar"/>
        </fileset>
    </path>
</project>

2 个答案:

答案 0 :(得分:0)

你应该考虑来自蚂蚁的任何“Execution tasks”任务。

答案 1 :(得分:0)

这是我为从命令行执行自动化测试而创建的build.xml文件。希望它可以帮到你。

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="WebTestProject" default="start" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>
    <property file="./environmentConfig.properties"/>

    <property name="ws.home" value="${basedir}"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
    <property name="browser" value="/usr/bin/google-chrome"/>
    <property name="mail_body_file" value="${basedir}/email_body.txt"/>
    <property name="buildID" value="build123"/>
    <property name="sendmailscript_path" value="${basedir}/sendmail.sh"/>
    <property name="mail_subject" value="Automated_test_execution_of_${buildID}"/>
    <property name="mail_recipient" value="username@domain.com"/>


    <!-- ====== For setting the classpath ====  -->
    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.home}/lib" includes="*.jar"/>
        </path>
         <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
    </target>




    <!-- ============ Initializing other stuff ===========  -->
    <target name="init" depends="setClassPath">

    <taskdef name="testng" classpath="${test.classpath}" classname="org.testng.TestNGAntTask" />

    </target>



    <!-- cleaning the destination folders -->
    <target name="clean">
        <delete dir="${test.dest}"/>
    </target>


    <!-- target for compiling the java files -->
    <target name="compile" depends="init, clean" > 
        <delete includeemptydirs="true" quiet="true">
            <fileset dir="${test.dest}" includes="**/*"/>
        </delete>
        <echo message="making directory..."/>
        <mkdir dir="${test.dest}"/>

        <echo message="compiling..."/>
        <javac 
            debug="true" 
            destdir="${test.dest}" 
            srcdir="${test.src}" 
            target="1.6" 
            classpath="${test.classpath}"
            includeantruntime="true"
        >
        </javac>
      </target>


    <!-- run -->
    <target name="run" depends="compile">
        <testng classpath="${test.classpath}:${test.dest}" suitename="InitialTestSuite">
           <xmlfileset dir="${ws.home}" includes="testng.xml"/> 
        </testng>

    </target>



    <!--  ========== Generating reports using XSLT utility ==============    -->
    <target name="testng-xslt-report">
                <delete dir="${basedir}/testng-xslt">
                </delete>
                <mkdir dir="${basedir}/testng-xslt">
                </mkdir>
                <echo message="Generating XSLT report..."/>
                <xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">
                    <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
                    <param expression="true" name="testNgXslt.sortTestCaseLinks" />
                    <param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
                    <param expression="true" name="testNgXslt.showRuntimeTotals" />
                    <classpath refid="classpath_jars">
                    </classpath>
                </xslt>
                <echo message="XSLT report generated"/>
    </target>


    <!-- ====== Fetch the generated report's index.html, open a chrome browser and show the report.========= -->
     <target name="runAndViewReport" depends="run, testng-xslt-report"> 
            <exec executable="${browser}" spawn="yes"> 
           <arg line="'${basedir}/testng-xslt/index.html'" /> 
          </exec>
        <echo message="Opening up the report in a browser..."/>
     </target>


    <!-- Starting point of the execution, should be dependent on target "run".

    Target sequence will be:
    start (not_execute) ==> run (not_execute) ==> compile (not_execute) ==> init (execute) ==> clean (execute)
    start (execute) <== testng-xslt-report (execute) <== run (execute) <== compile (execute) <==

    Suitable for ANT 1.7. Currently using this ====================== -->


    <target name="start" depends="run, testng-xslt-report">
        <tstamp prefix="getTime">
                <format property="TODAY" pattern="MM-dd-yyyyhhmmaa"/>
        </tstamp>
        <echo message="sending report as mail...."/>
        <property name="execution_time" value="${buildID}_${getTime.TODAY}"/>
        <property name="dest_file" value="/home/Reports/${execution_time}.zip"/>
        <zip destfile="/home/${execution_time}.zip" 
            basedir="${basedir}/testng-xslt"/>
        <property name="report_attachment_file" value="${dest_file}"/>
        <exec executable="${sendmailscript_path}" newenvironment="false"> 
                    <arg value="${mail_subject}"/>
                    <arg value="${mail_recipient}"/>
                    <arg value="${report_attachment_file}"/>
                    <arg value="${mail_body_file}"/> 
        </exec>

    </target>


</project>