Executing Selenium Web Driver Scripts using ANT




Today let us see how we can integrate Selenium with Apache Ant i.e. use Apache Ant via Command Line for executing Selenium Web Driver Test Scripts. Basically Apache Ant is a popular build tool available for Java developers.

Pre-requisite: Please make sure to download and install Apache Ant from the below path.

https://ant.apache.org/bindownload.cgi

You would also need JUnit & other Selenium dependent Jars in order to successfully execute the Test Script. To demonstrate the steps I would be doing it from Eclipse IDE. Hence if you would like to get a hands-on on the steps mentioned below then please make sure to have Eclipse IDE installed and configured at your end as well.

For detailed steps on how to configure Selenium in Eclipse IDE please refer to my earlier article here.


Create a Java Project in Eclipse.

clip_image002

Create a Class and name it as “Google Search Test“

clip_image004

Copy & Paste the below code snippet.

package com.suntargali.webdriver.ant;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.*;
import static org.junit.Assert.*;
public class GoogleSearchTest {
private WebDriver driver;
@Before
public void setUp() {
// Launch a new Firefox instance
driver = new FirefoxDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Navigate to Google
driver.get("http://www.google.com");
}
@Test
public void testGoogleSearch() {
// Find the Search Box Text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Clear the existing text value
element.clear();
// Enter the Search Keyword
element.sendKeys("Software Testers Forum - Suntaragali");
// Submit the Search Query
element.submit();
// Wait for the page to get rendered, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle()
.startsWith("Software Testers Forum - Suntaragali");
}
});
assertEquals("Software Testers Forum - Suntaragali - Google Search",driver.getTitle());
}
@After
public void tearDown() throws Exception {
// Close the browser
driver.quit();
}
}


Create a Lib Folder and add the below jars.

clip_image006


Right Click on the Project à Build Path à Configure Build Path
Now Click on Add jars in the Java Build Path window and add all the jar files from Lib Folder as shown below. Click on Apply.

clip_image008


For more details on steps to configure Selenium Web Driver in Eclipse please refer to my earlier post here…
Try executing the Script from Eclipse once by selecting Run As à JUnit Test
clip_image010
You should be able to execute the script successfully.

clip_image012

Now let us go ahead and create an ANT Build file. Create the build.xml file in the project folder with the following XML
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="exec" basedir=".">
<property name="src" value="./src" />
<property name="lib" value="./lib" />
<property name="bin" value="./bin" />
<property name="report" value="./report" />
<path id="test.classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>
<target name="compile" depends="init">
<javac source="1.7" srcdir="${src}" fork="true" destdir="${bin}">
<classpath>
<pathelement path="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="exec" depends="compile">
<delete dir="${report}" />
<mkdir dir="${report}" />
<mkdir dir="${report}/xml" />
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
<test name="com.suntaragali.webdriver.ant.GoogleSearchTest"
haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
<formatter type="xml" />
</test>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}/xml">
<include name="TEST*.xml" />
</fileset>
<report format="frames" todir="${report}/html" />
</junitreport>
</target>
</project>

Now in order to execute the Test Script from ANT, either you can Navigate to the project folder from Command Line and type ‘ant’ or Right Click on the Build.xml file in Eclipse and click on Run As à Ant Build

clip_image014
The Test Script should be executed successfully and you should be able to see “Build Successful” message in the console as shown below.

clip_image016

Also upon refreshing your Project in the Package Explorer you should be able to see JUnit Reports being generated.

clip_image018

Hope you were able to follow the above steps and were able to execute your Selenium Test Scripts from ANT successfully. If you have any queries further please do feel free to reach out to me. Happy Testing..!!


Executing Selenium Web Driver Scripts using ANT Executing Selenium Web Driver Scripts using ANT Reviewed by Suntaragali The Smart Techie on July 26, 2016 Rating: 5

No comments: