In this post, I will explain how to execute webdriver java test script using ant build tool.for this first we need to setup following in our machine.
- Java should be installed on machine.
- Selenium2 jar file.
- TestNG jar file.
- Ant should be installing and setup path in system environment variable.
Webdriver Test scripts.
I have created two webdriver test scripts in java on Wikipedia web application as mentioned below.
NavigateURL.java
package com.webdriver.test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.AfterSuite;
import
org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class NavigateURL {
private
WebDriver driver;
private
String baseUrl;
@BeforeSuite
public
void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl =
"http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.xpath("//a[contains(@title, \"English —
Wikipedia\")]")).click();
driver.findElement(By.linkText("Contents")).click();
driver.findElement(By.cssSelector("a[title=\"Featured content
– the best of Wikipedia\"]")).click();
driver.findElement(By.cssSelector("a[title=\"Find background
information on current events\"]")).click();
driver.findElement(By.linkText("Random
article")).click();
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}
SearchCountry.java
package com.webdriver.test;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.AfterSuite;
import
org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class SearchCountry {
private
WebDriver driver;
private String baseUrl;
@BeforeSuite
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.xpath("//a[contains(@title, \"English —
Wikipedia\")]")).click();
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("India");
driver.findElement(By.id("searchButton")).click();
String searchedResult =
driver.findElement(By.xpath("//h1[@id='firstHeading']/span"))
.getText();
Assert.assertTrue(searchedResult.equals("India"));
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}