Software QA FYI - SQAFYI

TestNG Selenium Integration Example

By: Ram Mokkapaty

In this article, I am going to show you an example of TestNG and Selenium integration. Let me first brief you on TestNG and Selenium.

TestNG is an annotation based testing framework which allows you to create configurable test suites where each suite contains one or more tests. A test in turn is composed of one more test classes.

Selenium is used in automating web applications for testing purposes. Unlike HtmlUnit, it uses actual browser to execute its tests.

Let’s start with the setup:
Since the example is about TestNG and Selenium integration, we will simplify our setup by using Maven as our build tool.
TestNG Maven Project Example will guide you on how to setup a Maven based project and run the TestNG tests.
Our examples will be based on Chrome and Firefox, so make sure both are installed in your machine.
The browser drivers will be automatically downloaded as a result of the dependencies being added to the maven project file pom.xml.
In case of chrome, we also need to download chromedriver.exe. This acts as a bridge between chrome and the driver.
I have used Eclipse as the IDE, version Luna 4.4.1.

1. TestNG and Selenium Dependencies
Since our project is dependent on TestNG and selenium, we need to add their dependencies in Maven’s pom.xml file. For TestNG, we need to add org.testng package and for selenium, we need to add org.seleniumhq.selenium. Once saved, it will download Selenium 2.0 java client library and all its dependencies.

01 <:?xml version="1.0" encoding="UTF-8"?>
02 <:project xmlns=""
03 xsi:schemaLocation="">
04 <:modelVersion>4.0.0<:/modelVersion>
05 <:groupId>com.javacodegeeks.testng.spring
06 <:artifactId>testNGSpring<:/artifactId>
07 <:version>0.0.1-SNAPSHOT<:/version>
08 <:dependencies>
09 <:dependency>
10 <:groupId>org.seleniumhq.selenium<:/groupId>
11 <:artifactId>selenium-java<:/artifactId>
12 <:version>2.45.0<:/version>
13 <:/dependency>
14 <:dependency>
15 <:groupId>org.testng<:/groupId>
16 <:artifactId>testng<:/artifactId>
17 <:version>6.8.8<:/version>
18 <:scope>test<:/scope>
19 <:/dependency>
20 <:/dependencies>
21 <:properties>
22 <:spring.version>4.1.5.RELEASE<:/spring.version>
23 <:/properties>
24 <:/project>

2. TestNG Selenium Simple Search Example

Selenium provides WebDriver as its API for automating web application testing. WebDriver drives the browser directly using each browser’s built-in support for automation.

In this example, we will use the WebDriver to open google and search for TestNG.
WebDriver is an interface so we still need to an implementation to run the test on an actual browser. We will select the implementation based on the browser used. For example, if we are using firefox, we will use FirefoxDriver. If it is chrome, we will use ChromeDriver. Since ChromeDriver works with Chrome through the chromedriver.exe, we need to make sure that the binary be placed somewhere on your system’s path.
Points to be noted regarding the test class:
In @BeforeSuite method, initDriver(), we create the driver.
searchTestNGInGoogle() is our test method.
We call driver.navigate().to(url) to open the url.
Once the site is open, we need to get a handle on the search field so that we can type in the text to be searched.
When we call driver.findElement("q")), the WebDriver locates the search field using matching name attribute.
Next, we call element.sendKeys("TestNG") to type in the text “TestNG” in the search field.
The search is submitted on calling element.submit().
Finally, we wait for the results to be returned.
In @AfterSuite method, quitDriver(), we call driver.quit() to close the browser session.

01 package com.javacodegeeks.testng.selenium;
03 import org.openqa.selenium.By;
04 import org.openqa.selenium.WebDriver;
05 import org.openqa.selenium.WebElement;
06 import org.openqa.selenium.firefox.FirefoxDriver;
07 import;
08 import;
09 import org.testng.annotations.AfterSuite;
10 import org.testng.annotations.BeforeSuite;
11 import org.testng.annotations.Test;
13 public class TestNGSeleniumSimpleSearchExample {
14 private WebDriver driver;
16 @BeforeSuite
17 public void initDriver() throws Exception {
18 System.out.println("You are testing in firefox");
19 driver = new FirefoxDriver();
20 }
22 @Test
23 public void searchTestNGInGoogle() {
24 final String searchKey = "TestNG";
25 System.out.println("Search " + searchKey + " in google");
26 driver.navigate().to("");
27 WebElement element = driver.findElement("q"));
28 System.out.println("Enter " + searchKey);
29 element.sendKeys(searchKey);
30 System.out.println("submit");
31 element.submit();
32 (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
33 public Boolean apply(WebDriver d) {
34 return d.getTitle().toLowerCase()
35 .startsWith(searchKey.toLowerCase());
36 }
37 });
38 System.out.println("Got " + searchKey + " results");
39 }
41 @AfterSuite
42 public void quitDriver() throws Exception {
43 driver.quit();
44 }
45 }

1 <:?xml version="1.0" encoding="UTF-8"?>
2 <:suite name="TestNgSeleniumSuite" parallel="false">
3 <:test name="TestNgSeleniumTest">
4 <:classes>
5 <:class
6 name="com.javacodegeeks.testng.selenium.TestNGSeleniumSimpleSearchExample" /> 7 <:/classes>
8 <:/test>
9 <:/suite>

01 [TestNG] Running:
04 You are testing in firefox
05 Search TestNG in google
06 Enter TestNG
07 submit
08 Got TestNG results
10 ===============================================
11 TestNgSeleniumSuite
12 Total tests run: 1, Failures: 0, Skips: 0
13 =============================================== 3. WebDriver Injection using spring

Instead of creating the WebDriver ourselves, we can inject the implementation using the spring’s dependency injection. Here we will modify our previous example by using spring to inject the driver.

Since we are going to rely on spring, we need to add spring-context and spring-test to our dependencies in pom.xml.

01 <:?xml version="1.0" encoding="UTF-8"?>
02 <:project xmlns="" xmlns:xsi="" 03 xsi:schemaLocation="">
04 <:modelVersion>4.0.0
05 <:groupId>com.javacodegeeks.testng.spring<:/groupId>
06 <:artifactId>testNGSpring<:/artifactId>
07 <:version>0.0.1-SNAPSHOT<:/version>
08 <:dependencies>
09 <:dependency>
10 <:groupId>org.seleniumhq.selenium<:/groupId>
11 <:artifactId>selenium-java<:/artifactId>
12 <:version>2.45.0<:/version>
13 <:/dependency>
14 <:dependency>
15 <:groupId>org.testng<:/groupId>
16 <:artifactId>testng<:/artifactId>
17 <:version>6.8.8<:/version>
18 <:scope>test<:/scope>
19 <:/dependency>
20 <:dependency>
21 <:groupId>org.springframework<:/groupId>
22 <:artifactId>spring-context<:/artifactId>
23 <:version>${spring.version}<:/version>
24 <:/dependency>
25 <:dependency>
26 <:groupId>org.springframework<:/groupId>
27 <:artifactId>spring-test<:/artifactId>
28 <:version>${spring.version}<:/version>
29 <:scope>test<:/scope>
30 <:/dependency>
31 <:/dependencies>
32 <:properties>
33 <:spring.version>4.1.5.RELEASE<:/spring.version>
34 <:/properties>
35 <:/project>

The below context file contains the driver bean definition. The class attribute points to org.openqa.selenium.firefox.FirefoxDriver.

1 <:?xml version="1.0" encoding="UTF-8"?>
2 <:beans xmlns=""
3 xmlns:xsi="" xmlns:context="" 4 xsi:schemaLocation=" 5">
6 <:bean id="driver" class="org.openqa.selenium.firefox.FirefoxDriver" />
7 <:/beans>

The below test class, does exactly what the previous one did, the only change is that the driver is injected through spring’s annotation @ContextConfiguration. Its value contains the location path to the context file driver_context.xml. The driver member is anootated with @Autowired so that the spring can inject the implementation.

Since we are going to rely on TestNG-spring integration framework, test class TestNGSeleniumDriverInjectionExample extends AbstractTestNGSpringContextTests.

01 package com.javacodegeeks.testng.selenium;
03 import org.openqa.selenium.By;
04 import org.openqa.selenium.WebDriver;

Full article...

Other Resource

... to read more articles, visit

TestNG Selenium Integration Example