Road to execute java webdriver testNg suite using ant build.

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.
  1. Java should be installed on machine.
  2. Selenium2 jar file.
  3. TestNG jar file.
  4. 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();              
                      }
}


Now we create testNg suite file as in my case, below is testng suite file created for the above test scripts


1 comment:

  1. I am not getting TestNG suite and ant build.xml file can you tell me how I get it.

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution