博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Selenium+Java] Apache ANT with Selenium: Complete Tutorial
阅读量:5098 次
发布时间:2019-06-13

本文共 9298 字,大约阅读时间需要 30 分钟。

Original URL: https://www.guru99.com/using-apache-ant-with-selenium.html

Apache ANT with Selenium: Complete Tutorial

What is Apache Ant?

While creating a complete software product, one needs to take care different third party API, their classpath, cleaning previous executable binary files, compiling our source code, execution of source code, creation of reports and deployment code base etc. If these tasks are done one by one manually, it will take an enormous time, and the process will be prone to errors.

Here comes the importance of a build tool like Ant. It stores, executes and automates all process in a sequential order mentioned in Ant's configuration file (usually build.xml).

Benefit of Ant build

  1. Ant creates the application life cycle i.e. clean, compile, set dependency, execute, report, etc.
  2. Third party API dependency can be set by Ant i.e. other Jar file's class path is set by Ant build file.
  3. A complete application is created for End to End delivery and deployment.
  4. It is a simple build tool where all configurations can be done using XML file and which can be executed from the command line.
  5. It makes your code clean as configuration is separate from actual application logic.

How to install Ant

Steps to install Ant in Windows is as follows

Step 1) Go to  Download .zip file from 

Step 2) Unzip the folder and go to and copy path to the root of unzipped folder

Step 3) Go to Start -> Computer -> right click here and select 'Properties' then click on Advanced System Settings

Step 4) A new window opens. Click on 'Environment Variable…' button.

Step 5) Click 'New…' button and set variable name as 'ANT_HOME' and variable value as the root path to unzipped folder and click OK.

Step 6) now select 'Path' variable from the list and click 'Edit' and append; %ANT_HOME%\bin.

Restart system one time and you are ready to use Ant build tool now.

Step 7) To check the version of your Ant using command line:

Ant –version

Understanding Build.xml

Build.xml is the most important component of Ant build tool. For aproject, all cleaning, setup, compilation and deployment related task are mentioned in this file in XML format. When we execute this XML file using command line or any IDE plugin, all instructions written into this file will get executed in sequential manner.

Let's understand the code within a sample build.XML

  • Project tag is used to mention a project name and basedir attribute. The basedir is the root directory of an application
  • Property tags are used as variables in build.XML file to be used in further steps
  • Target tags used as steps that will execute in sequential order. Name attribute is the name of the target. You can have multiple targets in a single build.xml
  • path tag is used to bundle all files logically which are in the common location
  • pathelement tag will set the path to the root of common location where all files are stored
  • pathconvert tag used to convert paths of all common file inside path tag to system's classpath format
  • fileset tag used to set classpath for different third party jar in our project
  • Echo tag is used to print text on the console
  • Delete tag will clean data from given folder
  • mkdir tag will create a new directory
  • javac tag used to compile java source code and move .class files to a new folder
  • jar tag will create jar file from .class files
  • manifest tag will set your main class for execution
  • 'depends' attribute used to make one target to depend on another target
  • java tag will execute main function from the jar created in compile target section

Run Ant using Eclipse plugin

To run Ant from eclipse go to build.xml file -> right click on file -> Run as… -> click Build file

Example:

We will take a small sample program that will explain Ant functionality very clearly. Our project structure will look like –

Here in this example we have 4 targets

  1. Set class path for external jars,
  2. Clean previously complied code
  3. Compile existing java code
  4. Run the code

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

How to Execute TestNG code using Ant

Here we will create a class withmethods and set class path forin build.xml.

Now to execute testng method we will create another testng.xml file and call this file from build.xml file.

Step 1) We create a "Guru99AntClass.class" in package testAnt

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();						}		}

Step 2) Create a target to load this class in Build.xml

Step 3) Create testng.xml

testng.xml

Step 4) Create Target in Build.xml to run this TestNG code

Step 5) The complete Build.xml

Step 6) Output

Ant with Selenium Webdriver:

So far, we have learned that using ANT we can put all third party jars in a particular location in the system and set their path for our project. Using this method we are setting all dependencies of our project in a single place and making it more reliable for compilation, execution, and deployment.

Similarly, for our testing projects using selenium, we can easily mention selenium dependency in build.xml and we don't need to add a class path of it manually in our application.

So now you can ignore below-mentioned traditional way to set classpaths for project.

Example:

We are going to modify the previous example

Step 1) Set the property selenium.jars to selenium related jar in the resource folder

Step 2) In the target setClassPath, add the selenium files

Step 3) Complete Build.xml:

Step 4) Now change previously created class Guru99AntClass.java with new code.

Here in this example our steps using Selenium are:

  1. Go to 
  2. Read all courses links one by one
  3. Print all courses hyperlink on console.

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://demo.guru99.com/test/guru99home/");		  List
listAllCourseLinks = driver.findElements(By.xpath("//div[@class='canvas-middle']//a")); for(WebElement webElement : listAllCourseLinks) { System.out.println(webElement.getAttribute("href")); } }}

Step 5) After successful execution output will look like:

Summary:

Ant is a build tool for Java.

Ant used for code compilation, deployment, execution process.

Ant can be downloaded fromwebsite.

Build.xml file used to configure execution targets using Ant.

Ant can be run from the command line or suitable IDE plugin like eclipse.

 

转载于:https://www.cnblogs.com/alicegu2009/p/9098811.html

你可能感兴趣的文章
WCF傻瓜模式写程序
查看>>
《绿色·精简·性感·迷你版》易语言,小到不可想象
查看>>
Java Web学习总结(13)Listener监听器
查看>>
开始Flask项目
查看>>
Ruby:多线程队列(Queue)下载博客文章到本地
查看>>
Android打包key密码丢失找回
查看>>
03 jQuery动画
查看>>
医药箱APP静态小项目
查看>>
安装使用eclipse
查看>>
VC6.0调试技巧(一)(转)
查看>>
用Chrome调试Android手机上的网页
查看>>
django 王中王8之踏青撒花
查看>>
学习网站收集
查看>>
linux命令
查看>>
类库与框架,强类型与弱类型的闲聊
查看>>
webView添加头视图
查看>>
字符环(openjudge 2755)
查看>>
php match_model的简单使用
查看>>
在NT中直接访问物理内存
查看>>
Intel HEX 文件格式
查看>>