http://www.softwaretestinghelp.com/apache-ant-selenium-tutorial-23/
In the last tutorial, we tried to make you acquainted with the concept of generics and common methods. We also discussed the benefits we get out of generics like reusability. We also shared the practical approaches towards creation of generics and their accessibility.
In the current tutorial in this Selenium automation series, we would shed light on a build tool named as “Apache Ant”. We would broadly discuss its applicability and importance besides the practical approach.
Take a note that the tutorial is limited to testing aspects of using Apache Ant.
Apache Ant is a very popular and conventional build tool of our times. Ant is an open source java based build tool provided by Apache Software Foundation freely distributed under GNU license. Apache Ant plays a significant role in developer’s as well as Tester’s day to day work schedule. The tool has immense power to build the development code into deployment utilities.
Ant is a tool that automates the software building process. Ant is not just limited to compilation of code, rather packaging, testing and a lot more can be achieved in some simple steps.
The tool works on the principle of targets and dependencies defined in the XML files. Ant libraries are used to build the applications. The libraries have a set of defined tasks to archive, compile, execute, document, deploy, and test and many more targets. Moreover, Ant allows the user to create his/her own tasks by implementing their own libraries.
Ant is primarily used with Java Applications but it can still be used for applications built on other languages depending on the extended support.
The most important aspect of using Ant is that it doesn’t demands another set of code to be written in order to build the application, rather the entire process is defined by targets which are none other than XML elements.
Apache Ant Benefits
-
Ease of Use – The tool provides a wide range of tasks that almost fulfills all the build requirements of the user.
-
Platform Independent – Ant is written in Java thus is a platform independent build tool. The only requirement for the tool is JDK.
-
Extensibility – As the tool is written in Java and the source code is freely available, user is leveraged with the benefit to extend the tool’s capabilities by writing java code for adding task in Ant Libs.
Apache Ant Features
- Can compile java based applications
- Can create Java Doc
- Can create war, jar, zip, tar files
- Can copy files to at different locations
- Can delete or move files
- Can send Emails to the stakeholders
- Supports Junit 3, Junit 4, TestNG etc.
- Can convert XML based test reports to HTML reports
- Can make directories
- Can check out the code from version control system (SVN, GIT, CVS etc).
- Can execute test scripts and test suites
Environment Setup
Let us demonstrate the entire setup process step by step.
Step 1: Apache Ant Download
The first and the foremost step is to download the zipped folder of Apache Ant latest version from the repository. The distribution is available at “http://ant.apache.org/bindownload.cgi”.
Step 2: Extract folder and Set Environment Variables
Extract the zipped folder at any desired location onto local file system.
Prior to setting up environment for Ant, it is required to install and set JDK on to your system. I am assuming that the JDK is already set and installed, Thus moving forward with the Ant Setup.
Create an environment variable for “ANT_HOME” and set the variable’s value to the location of Ant folder. Refer the following screenshot for the same.
(Click to enlarge image)
Edit the Path variable to append the location of the bin folder i.e. compiler location.
User can also verify for the successful Ant installation by typing in the “ant -version” command in the command prompt. The user would be able to see the following screen for the successful installation.
Step 3: Download and Extract Junit Jar
Download the latest version of Junit jar from “https://github.com/junit-team/junit/wiki/Download-and-Install” and configure the project’s build path in eclipse and add the jar as external library. Refer the following illustration.
Thus, no other installation is required to use Apache Ant in collaboration with Junit and Selenium WebDriver to build, execute and report the test scripts.
Note: Take a note to necessarily add “ant-junit4.jar” jar file that can be found within the library folder of the Ant’s software distribution.
Sample Build.xml
The next step is to create the project’s build file. Build file is nothing but a collection of xml elements. Worth mentioning that one build file can relate to one and only one project i.e. one build file per project or vice versa. Build file is customarily located at the project’s root/base folder but the user is leveraged to select the build’s location driven by his/her wish. Moreover the user is free to rename the build file if he/she desires.
Each of the build file must have one project and at least one target element. Refer the sample build.xml
1 |
<? xml version = "1.0" encoding = "UTF-8" ?>
|
2 |
< project name = "Learning_Selenium" default = "junitReport" basedir = "." >
|
3 |
< property name = "src" value = "./src" />
|
4 |
< property name = "lib" value = "./lib" />
|
5 |
< property name = "bin" value = "./bin" />
|
6 |
< property name = "report" value = "./report" />
|
7 |
< property name = "test.dir" value = "./src/com/tests" />
|
8 |
< path id = "Learning_Selenium.classpath" >
|
9 |
< pathelement location = "${bin}" />
|
10 |
< fileset dir = "${lib}" >
|
11 |
< include name = "**/*.jar" />
|
14 |
< echo message = "-----------------------------------------------------------------" />
|
15 |
< echo message = "--------------------Selenium Learning Tests----------------------" />
|
16 |
< echo message = "-----------------------------------------------------------------" />
|
17 |
< target name = "init" description = "Delete the binary folder and create it again" >
|
18 |
< echo message = "----------Delete the binary folder and create it again----------" />
|
19 |
< delete dir = "${bin}" />
|
22 |
< format property = "lastUpdated" pattern = "dd-MM-yyyy HH:mm:ss" />
|
25 |
< mkdir dir = "${bin}" />
|
27 |
< target name = "compile" depends = "init" description = "Compile the source files" >
|
28 |
< echo message = "----------Compile the source files----------" />
|
29 |
< javac source = "1.7" srcdir = "${src}" fork = "true" destdir = "${bin}" includeantruntime = "false" debug = "true" debuglevel = "lines,vars,source" >
|
30 |
< classpath refid = "Learning_Selenium.classpath" />
|
33 |
< target name = "exec" depends = "compile" description = "Launch the test suite" >
|
34 |
< echo message = "----------Launch the test suite----------" />
|
35 |
< delete dir = "${report}" />
|
36 |
< mkdir dir = "${report}" />
|
37 |
< mkdir dir = "${report}/xml" />
|
38 |
< junit fork = "yes" printsummary = "withOutAndErr" haltonfailure = "no" >
|
39 |
< classpath refid = "Learning_Selenium.classpath" />
|
40 |
< formatter type = "xml" />
|
41 |
< batchtest fork = "yes" todir = "${report}/xml" >
|
42 |
< fileset dir = "${src}" includes = "**/com/TestSuite.java" />
|
46 |
< target name = "junitReport" depends = "exec" description = "Generate the test report" >
|
47 |
< echo message = "----------Generate the test report----------" />
|
48 |
< junitreport todir = "${report}" >
|
49 |
< fileset dir = "${report}/xml" >
|
50 |
< include name = "TEST-*.xml" />
|
52 |
< report format = "frames" todir = "${report}/html" >
|
53 |
< param name = "TITLE" expression = "Selenium_Learning_Report" />
|
Explanation of Build.xml
The project element is fundamentally consists of 3 attributes:
<project name=“Learning_Selenium” default=“junitReport”basedir=“.”>
Each of the attribute has a “Key-Value pair” structure.
-
Name – The value of the name attribute represents the name of the project. Thus in our case, the project’s name is “Learning_Selenium”.
-
Default – The value of the default attribute represents the compulsory target for the build.xml. A build.xml file can have any number of targets. Thus this field represents the mandatory target amongst all.
-
Basedir – Represents the root folder or base directory of the project. Under this directory, there may be several other folders like src, lib, bin etc.
<target name=“init” description=“Delete the binary folder and create it again”>
All the tasks in the Ant build file are defined under Target elements. Each Target element corresponds to a particular task or goal. A single target can consists of multiple tasks if needed. Like I mentioned earlier, the user is credited to create more than one target within a particular build file.
In the above xml code, we have created targets for the following goals:
- Deleting and creating directories
- Compiling the code
- Executing the test classes
- Generating the test reports
<target name=“exec” depends=“compile” description=“Launch the test suite”>
Sometimes it is required to execute a particular target only when some other target is executed successfully. Take a note that the target are executed sequentially i.e. in order of sequence they are mentioned in the build file. Also I would like to mention that a particular target is executed once and only once for the current build execution. Thus, when the user is required to generate dependency between the target, he/she has to use depends attribute. The value of the “depends” attribute shall be the name of the target on which it depends. A target can depend on more than one target as well.
Built-in Tasks
Ant build file provides varieties of tasks. Few of them are discussed below:
File Tasks – File task are self explanatory.
- <copy>
- <concat>
- <delete>
- <get>
- <mkdir>
- <move>
- <replace>
Compile Tasks
- <javac> – Compiles source files within the JVM
- <jspc> – Runs jsp compiler
- <rmic> – Runs rmic compiler
Archive Tasks
- <zip>, <unzip> – Creates a zipped folder
- <jar>, <unjar> – Creates a jar file
- <war>, <unwar> – Creates a war file for deployment
Testing Tasks
- <junit> – Runs JUnit testing framework
- <junitreport> – Generates the test report by converting JUnit generated XML test reports.
Property Tasks
- <dirname> – Sets the property
- <loadfile> – Loads a file into property
- <propertyfile> – Creates a new property file
Misc. Tasks
- <echo> – Echoes the text message to be printed either on the console or written within an external file.
- <javadoc> – Generates the java based documentation using javadoc tool.
- <sql> – Establishes a JDBC connection and hits dash of SQL commands.
Execution
The easiest section is to execute the test suite with Ant. To execute the test suite with Ant, Right click on “build.xml” and select “Run As -> Ant Build” option. Thus, the option hits the execution. Refer the following figure for the same.
After the entire execution is completed, Ant generates a test execution report for review inside the “Report” folder.
The execution can also be initiated outside the eclipse by hitting the command on the command prompt. User is expected to navigate to the directory where build.xml is kept and type “ant”.
Conclusion
In this tutorial, we laid emphasis on useful information related to Ant, its installation and various Ant tasks. Our motive was to at least introduce you with the basic conceptual picture and its importance as a tool all together with respect to testing. Hence, we discussed build.xml in detail describing the various components.
Briefing in the end, Ant is a tool that automates the software building process. Ant is not just limited to compilation of code, rather packaging, testing and a lot more can be achieved in some simple steps.
Next Tutorial #24: We will learn about Maven – a build automation tool. Maven simplifies the code handling and process of building the project. Most of the projects follow maven structure. We will learn how to use Maven and Maven project setup for Selenium
相关推荐
【Python的selenium操作:判断元素是否存在】 在Python的自动化测试中,Selenium是一个非常强大的工具,用于模拟用户与网页的交互。Selenium库提供了一系列API,使得我们可以控制浏览器进行各种操作,例如点击按钮...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
"selenium ant 最新版本"指的是将Selenium集成到Apache Ant构建工具中的最新版本,以便在自动化构建流程中执行测试。 Apache Ant是一个Java库和命令行工具,其任务是驱动构建过程。它基于XML来定义项目规则和依赖...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
在进行自动化测试时,Selenium 是一款非常强大的工具,它允许开发者编写脚本来模拟用户对网页的操作。在本文中,我们将深入探讨Selenium与谷歌浏览器(Chrome)以及对应的Chrome驱动(ChromeDriver)的配合使用,...
Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....
Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver About This Book Learn to leverage the power of Selenium WebDriver with simple examples that...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
用于firefox录制的插件,是php语言的文件导出,使用firefox打开的话下载完成后直接安装。
Selenium.WebDriver.4.0.0-alpha02.nupkg,Selenium.WebDriver 4.0,Selenium is a set of different software tools each with a different approach to supporting browser automation. These tools are highly ...
执行项目需要提前安装好python环境(anaconda、pycharm)以及selenium依赖包(pip install selenium),同时还需下载selenium对应的浏览器驱动程序(driver.exe),放在本机环境变量路径中。具体可见博客专题中的...
Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage...
1. **构建脚本**:一个XML配置文件(通常是build.xml),定义了Ant任务,包括编译Java源代码、生成Selenium和TestNG的测试类、运行测试并生成报告等。 2. **测试类**:使用Java编写的Selenium WebDriver和TestNG...
SelAnt是针对自动化Web应用程序测试的一个开源库,它巧妙地结合了Apache Ant构建工具和Selenium测试框架。Ant是一款强大的Java项目构建系统,而Selenium则是一个广泛用于Web应用程序功能测试的工具。通过SelAnt,...