带Selenium的Apache Ant:完整教程

什么是Apache Ant?

在创建一个完整的软件产品时,需要注意不同的第三方API,它们的类路径,清理以前的可执行二进制文件,编译我们的源代码,执行源代码,创建报表和部署代码库等。如果手动逐个完成这些任务,会花费大量的时间,而且过程中容易出错。

这就是像Ant这样的构建工具的重要性。它按照Ant的配置文件(通常是build.xml)中提到的顺序存储、执行和自动化所有流程。

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

Ant构建的好处

  1. Ant创建应用程序生命周期,即清理、编译、设置依赖项、执行、报告等。
  2. 第三方API依赖可以由Ant设置,即其他Jar文件的类路径由Ant构建文件设置。
  3. 为端到端交付和部署创建了完整的应用程序。
  4. 它是一个简单的构建工具,所有配置都可以使用XML文件完成,并且可以从命令行执行。
  5. 它使代码干净,因为配置与实际的应用程序逻辑是分开的。

如何安装Ant

在Windows中安装Ant的步骤如下

步骤1) 转到https://ant.apache.org/bindownload.cgi,从apache-ant-1.9.4-bin.zip下载.zip文件

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

步骤2)解压缩文件夹,然后转到解压缩文件夹的根目录并将路径复制到该根目录

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

步骤3)转到开始->计算机->右键单击此处并选择‘属性’,然后单击高级系统设置

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

步骤4)打开一个新窗口。点击‘Environment Variable…’(环境变量设置)按钮。

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

步骤5)点击‘新建…’按钮,并将变量名设置为‘ANT_HOME’,将变量值设置为解压缩文件夹的根路径,然后单击OK。

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

步骤6)现在从列表中选择‘PATH’变量并点击‘Edit’并追加;%ANT_HOME%。

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

重新启动系统一次,现在就可以使用Ant构建工具了。

步骤7)使用命令行检查Ant的版本:

Ant版

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

了解Build.xml

xml是Ant构建工具中最重要的组件。对于Java项目,所有与清理、设置、编译和部署相关的任务都以XML格式在此文件中提及。当我们使用命令行或任何IDE插件执行此XML文件时,写入此文件的所有指令都将按顺序执行。

让我们了解一下示例build.XML中的代码

  • 项目标签用于提及项目名称和basedir属性。basedir是应用程序的根目录

Java <project name="YTMonetize" basedir=".">

  • 属性标记用作build.XML文件中的变量,以便在后续步骤中使用
<property name="build.dir" value="${basedir}/build"/>
                                 <property name="external.jars" value=".\resources"/>
                                         <property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
                                                 <property name="src.dir"value="${basedir}/src"/>
  • 用作将按顺序执行的步骤的目标标记。可以在单个build.xml中包含多个目标

Java <target name="setClassPath">

  • PATH标签用于逻辑捆绑所有位于公共位置的文件

Java <path id="classpath_jars">

  • pathelement标签将设置存储所有文件的公共位置根目录的路径

Java <pathelement path="${basedir}/"/>

  • pathConvert标签,用于将path标签内所有公共文件的路径转换为系统的类路径格式

Java <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>

  • FileSet标签,用于为我们项目中的不同第三方JAR设置类路径

Java <fileset dir="${ytoperation.dir}" includes="*.jar"/>

  • ECHO标签用于在控制台上打印文本
<echo message="deleting existing build directory"/>
  • 删除标签将清除给定文件夹中的数据
<delete dir="${build.dir}"/>
  • mkdir标记将创建一个新目录
<mkdir dir="${build.dir}"/>
  • 用于编译Java源代码并将.class文件移动到新文件夹的javac标记
<javac destdir="${build.dir}" srcdir="${src.dir}">
                                     <classpath refid="classpath_jars"/>
                                             </javac>
  • JAR标记将从.class文件创建JAR文件
<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}">
  • MANIFEST标签将设置要执行的主类
<manifest>
<attribute name="Main-Class" value="test.Main"/>
                                   </manifest>
  • “Dependents”属性,用于使一个目标依赖于另一个目标
<target name="run" depends="compile">
  • Java标记将从COMPILE TARGET部分创建的JAR执行Main函数
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>

使用Eclipse插件运行Ant

要从Eclipse运行Ant,转到build.xml file->右键单击FILE->Run as…->单击构建文件

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

示例:

我们将采用一个小示例程序,它将非常清楚地解释Ant功能。我们的项目结构将看起来像-

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

在本例中,我们有4个目标

  1. 设置外部JAR的类路径,
  2. 清除以前编译的代码
  3. 编译现有的Java代码
  4. 运行代码

Guru99AntClass.class

package testAnt;
import java.util.Date;
public class Guru99AntClass {
    public static void main(String...s) {
        System.out.println("HELLO GURU99 ANT PROGRAM");
        System.out.println("TODAY's DATE IS->"+ currentDate() );
    }
    public static String currentDate() {
        return new Date().toString();
    }
}

Build.xml

<?xml version="1.0" encoding="UTF-8"    standalone="no"?>
                             <!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
                             <project name="YTMonetize" basedir=".">
                                     <!--Property tags will be used as variables in build.xml file to use in further steps-->
                                     <property name="build.dir" value="${basedir}/build"/>
                                             <property name="external.jars" value=".\resources"/>
                                                     <property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
                                                             <property name="src.dir"value="${basedir}/src"/>
                                                                     <!--Target tags used as steps that will execute in sequential order. name attribute will be the name  of the target and < a name=OLE_LINK1 >'depends' attribute used to make one target to depend on another target -->
                                                                             <target name="setClassPath">
                                                                                     <path id="classpath_jars">
                                                                                             <pathelement   path="${basedir}/"/>
                                                                                                     </path>
                                                                                                     <pathconvert   pathsep=";"property="test.classpath" refid="classpath_jars"/>
                                                                                                             </target>
                                                                                                             <target name="clean">
                                                                                                                     <!--echo tag will use to print text on console-->
                                                                                                                     <echo message="deleting existing build directory"/>
                                                                                                                             <!--delete tag will clean data from given folder-->
                                                                                                                             <delete dir="${build.dir}"/>
                                                                                                                                     </target>
                                                                                                                                     <target name="compile" depends="clean,setClassPath">
                                                                                                                                             <echo message="classpath:${test.classpath}"/>
                                                                                                                                                     <echo message="compiling.........."/>
                                                                                                                                                             <!--mkdir tag will create new director-->
                                                                                                                                                             <mkdir dir="${build.dir}"/>
                                                                                                                                                                     <echo message="classpath:${test.classpath}"/>
                                                                                                                                                                             <echo message="compiling.........."/>
                                                                                                                                                                                     <!--javac tag used to compile java source code and move .class files to a new folder-->
                                                                                                                                                                                     <javac destdir="${build.dir}" srcdir="${src.dir}">
                                                                                                                                                                                             <classpath refid="classpath_jars"/>
                                                                                                                                                                                                     </javac>
                                                                                                                                                                                                     <!--jar tag will create jar file from .class files-->
                                                                                                                                                                                                     <jar   destfile="${ytoperation.dir}/YTOperation.jar"basedir="${build.dir}">
                                                                                                                                                                                                             <!--manifest tag will set your main class for execution-->
                                                                                                                                                                                                                 <manifest>
                                                                                                                                                                                                                 <attribute name="Main-Class" value="testAnt.Guru99AntClass"/>
                                                                                                                                                                                                                         </manifest>
                                                                                                                                                                                                                         </jar>
                                                                                                                                                                                                                         </target>
                                                                                                                                                                                                                         <target name="run" depends="compile">
                                                                                                                                                                                                                                 <!--java tag will execute main function from the jar created in compile target section-->
                                                                                                                                                                                                                                 <java jar="${ytoperation.dir}/YTOperation.jar"fork="true"/>
                                                                                                                                                                                                                                         </target>
                                                                                                                                                                                                                                         </project>
Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

如何使用Ant执行测试代码

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

在这里,我们将使用TestNG方法创建一个类,并在build.xml中设置用于测试的类路径。

现在要执行testNG方法,我们将创建另一个testng.xml文件,并从build.xml文件调用该文件。

步骤1)在Package Testant中创建“Guru99AntClass.class

Guru99AntClass.class

package testAnt;
import java.util.Date;
import org.testng.annotations.Test;
public class Guru99AntClass {
    @Test
    public void Guru99AntTestNGMethod() {
        System.out.println("HELLO GURU99 ANT PROGRAM");
        System.out.println("TODAY's DATE IS->"+ currentDate() );
    }
    public static String currentDate() {
        return new Date().toString();
    }
}

步骤2)创建目标以在Build.xml中加载此类

<!-- Load testNG and add to the class path of application -->
    <target name="loadTestNG" depends="setClassPath">
                                      <!—using taskdef  tag we can add a task to run on the current project. In below line, we are adding testing task in this project. Using testing task here now we can run testing code using the ant script -->
                                      <taskdef resource="testngtasks" classpath="${test.classpath}"/>
                                              </target>

步骤3)创建testng.xml

testng.xml

<?xml version="1.0"encoding="UTF-8"?>
                            <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
                            <suite name="YT"thread-count="1">
                                        <test name="GURU99TestNGAnt">
                                                <classes>
                                                <class name="testAnt.Guru99AntClass">
                                                            </class>
                                                                </classes>
                                                                </test>
                                                                </suite>

步骤4)在Build.xml中创建目标以运行此测试代码

<target name="runGuru99TestNGAnt" depends="compile">
             <!-- testng tag will be used to execute testng code using corresponding testng.xml file. Here classpath attribute is setting classpath for testng's jar to the project-->
             <testng classpath="${test.classpath};${build.dir}">
                               <!—xmlfileset tag is used here to run testng's code using testing.xml file. Using includes tag we are mentioning path to testing.xml file-->
                               <xmlfileset dir="${basedir}" includes="testng.xml"/>
                                       </testng>

步骤5)完整的Build.xml

<?xml version="1.0"encoding="UTF-8"standalone="no"?>
                            <!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
                            <project name="YTMonetize" basedir=".">
                                    <!--Property tags will be used as variables in build.xml file to use in further steps-->
                                    <property name="build.dir"value="${basedir}/build"/>
                                            <!-- put  testng related jar in the resource  folder -->
                                            <property name="external.jars" value=".\resource"/>
                                                    <property name="src.dir" value="${basedir}/src"/>
                                                            <!--Target tags used as steps that will execute in  sequential order. name attribute will be the name
                                                            of the target and 'depends' attribute used to make one target to depend on another target-->
                                                            <!-- Load testNG and add to the class path of application -->
                                                                <target name="loadTestNG"depends="setClassPath">
                                                                        <taskdef resource="testngtasks"classpath="${test.classpath}"/>
                                                                                </target>
                                                                                <target name="setClassPath">
                                                                                        <path id="classpath_jars">
                                                                                                <pathelement path="${basedir}/"/>
                                                                                                        <fileset dir="${external.jars}" includes="*.jar"/>
                                                                                                                </path>
                                                                                                                <pathconvert pathsep=";"property="test.classpath"refid="classpath_jars"/>
                                                                                                                        </target>
                                                                                                                        <target name="clean">
                                                                                                                                <!--echo tag will use to print text on console-->
                                                                                                                                <echo message="deleting existing build directory"/>
                                                                                                                                        <!--delete tag will clean data from given folder-->
                                                                                                                                        <delete             dir="${build.dir}"/>
                                                                                                                                                </target>
                                                                                                                                                <target name="compile"depends="clean,setClassPath,loadTestNG">
                                                                                                                                                        <echo message="classpath:${test.classpath}"/>
                                                                                                                                                                <echo   message="compiling.........."/>
                                                                                                                                                                        <!--mkdir tag will create new director-->
                                                                                                                                                                        <mkdir dir="${build.dir}"/>
                                                                                                                                                                                <echo message="classpath:${test.classpath}"/>
                                                                                                                                                                                        <echo message="compiling.........."/>
                                                                                                                                                                                                <!--javac tag used to compile java source code and move .class files to a new folder-->
                                                                                                                                                                                                <javac destdir="${build.dir}"srcdir="${src.dir}">
                                                                                                                                                                                                        <classpath refid="classpath_jars"/>
                                                                                                                                                                                                                </javac>
                                                                                                                                                                                                                </target>
                                                                                                                                                                                                                <target name="runGuru99TestNGAnt"depends="compile">
                                                                                                                                                                                                                        <!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
                                                                                                                                                                                                                        <testng classpath="${test.classpath};${build.dir}">
                                                                                                                                                                                                                                <xmlfileset dir="${basedir}"includes="testng.xml"/>
                                                                                                                                                                                                                                        </testng>
                                                                                                                                                                                                                                        </target>
                                                                                                                                                                                                                                        </project>

步骤6)输出

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

下载上述文件

带有Selenium WebDriver的Ant:

到目前为止,我们已经了解到,使用ANT,我们可以将所有第三方JAR放在系统中的特定位置,并为我们的项目设置它们的路径。使用此方法,我们将项目的所有依赖项设置在一个位置,使其更可靠地进行编译、执行和部署。

类似地,对于使用Selenium的测试项目,我们可以很容易地在build.xml中提到Selenium依赖,而不需要在我们的应用程序中手动添加它的类路径。

所以现在可以忽略下面提到的为项目设置类路径的传统方法。

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

示例:

我们将修改前面的示例

步骤1)将资源文件夹中的属性selenium.jars设置为Selenium Related JAR

<property name="selenium.jars" value=".\selenium"/>

步骤2)在目标setClassPath中,添加Selenium文件

<target name="setClassPath">
             <path id="classpath_jars">
                      <pathelement path="${basedir}/"/>
                                        <fileset dir="${external.jars}" includes="*.jar"/>
                                                <!-- selenium jar added here -->
                                                <fileset dir="${selenium.jars}" includes="*.jar"/>
                                                        </path>

步骤3)完成Build.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
                             <!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
                             <project name="YTMonetize" basedir=".">
                                     <!--Property tags will be used as variables in build.xml file to use in further steps-->
                                     <property name="build.dir" value="${basedir}/build"/>
                                             <!-- put  testng related jar in the resource  folder -->
                                             <property name="external.jars" value=".\resource"/>
                                                     <!-- put  selenium related jar in resource  folder -->
                                                     <property name="selenium.jars" value=".\selenium"/>
                                                             <property name="src.dir" value="${basedir}/src"/>
                                                                     <!--Target tags used as steps that will execute in  sequential order. name attribute will be the name
                                                                     of the target and 'depends' attribute used to make one target to depend on another target-->
                                                                     <!-- Load testNG and add to the class path of application -->
                                                                         <target name="loadTestNG" depends="setClassPath">
                                                                                 <taskdef resource="testngtasks" classpath="${test.classpath}"/>
                                                                                         </target>
                                                                                         <target name="setClassPath">
                                                                                                 <path id="classpath_jars">
                                                                                                         <pathelement path="${basedir}/"/>
                                                                                                                 <fileset dir="${external.jars}" includes="*.jar"/>
                                                                                                                         <!-- selenium jar added here -->
                                                                                                                         <fileset dir="${selenium.jars}"includes="*.jar"/>
                                                                                                                                 </path>
                                                                                                                                 <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
                                                                                                                                         </target>
                                                                                                                                         <target name="clean">
                                                                                                                                                 <!--echo tag will use to print text on console-->
                                                                                                                                                 <echo message="deleting existing build directory"/>
                                                                                                                                                         <!--delete tag will clean data from given folder-->
                                                                                                                                                         <delete dir="${build.dir}"/>
                                                                                                                                                                 </target>
                                                                                                                                                                 <target name="compile" depends="clean,setClassPath,loadTestNG">
                                                                                                                                                                         <echo message="classpath:${test.classpath}"/>
                                                                                                                                                                                 <echo message="compiling.........."/>
                                                                                                                                                                                         <!--mkdir tag will create new director-->
                                                                                                                                                                                         <mkdir dir="${build.dir}"/>
                                                                                                                                                                                                 <echo message="classpath:${test.classpath}"/>
                                                                                                                                                                                                         <echo message="compiling.........."/>
                                                                                                                                                                                                                 <!--javac tag used to compile java source code and move .class files to new folder-->
                                                                                                                                                                                                                 <javac destdir="${build.dir}"srcdir="${src.dir}">
                                                                                                                                                                                                                         <classpath refid="classpath_jars"/>
                                                                                                                                                                                                                                 </javac>
                                                                                                                                                                                                                                 </target>
                                                                                                                                                                                                                                 <target name="runGuru99TestNGAnt" depends="compile">
                                                                                                                                                                                                                                         <!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
                                                                                                                                                                                                                                         <testng classpath="${test.classpath};${build.dir}">
                                                                                                                                                                                                                                                 <xmlfileset dir="${basedir}" includes="testng.xml"/>
                                                                                                                                                                                                                                                         </testng>
                                                                                                                                                                                                                                                         </target>
                                                                                                                                                                                                                                                         </project>

步骤4)现在使用新代码更改以前创建的类Guru99AntClass.java。

在本例中,我们使用Selenium的步骤如下:

  1. 访问http://www.itxiaonv.com/test/guru99home/
  2. 逐一阅读所有课程链接
  3. 在控制台上打印所有课程超链接。

Guru99AntClass.java:

package testAnt;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Guru99AntClass {
    @Test
    public void Guru99AntTestNGMethod() {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.itxiaonv.com/test/guru99home/");
        List<WebElement> listAllCourseLinks = driver.findElements(By.xpath("//div[@class='canvas-middle']//a"));
        for(WebElement webElement : listAllCourseLinks) {
            System.out.println(webElement.getAttribute("href"));
        }
    }
}

步骤5)成功执行后,输出将如下所示:

Apache ANT with Selenium: Complete Tutorial
Apache ANT with Selenium: Complete Tutorial

下载上述示例文件

总结:

Ant是Java的构建工具。

Ant用于代码编译、部署、执行过程。

Ant可以从Apache网站下载。

用于使用Ant配置执行目标的Build.xml文件。

Ant可以从命令行或Eclipse等合适的IDE插件运行。

IT赶路人

专注IT知识分享