Thursday, December 18, 2014

Java + Selenium + Maven + TestNG + ReportNG integration

Java and Selenium work great together. They are a popular combo when it comes to Automation Testing. In this article I'll show you how to integrate Java, Selenium and also Maven, TestNG and ReportNG. This set of tools provide enough flexibility to create a tailored automation framework for almost any automation project. It is not so difficult, but it can take some time. This is what I'll use in this example:
  • Ubuntu 14.04 (get it here)
  • Eclipse Luna (get it here)
  • Maven installed
  • Eclipse integrated with Maven and TestNG (I recommend to read this article before if you need further details)
In this case, I'll work with the default driver provided by Selenium. That means that the tests will only run with Firefox.


Create a basic Maven project

1. Open Eclipse
2. Click File>New>Project...
New Project menu
3. Select Maven>Maven Project, then click Next
Maven project
4. Click Next twice
5. Enter the Group ID and the Artifact ID (if you don't know what are they about, please check this). You can use the following configuration. Then click Finish.
Maven Project configuration

Add required dependencies

The heart of a Maven project is the pom.xml file. Here you can specify important aspects of the project, and also you can add dependencies. This is like adding .jar libraries, but Maven handles everything and in most cases, it is possible to find the dependency in the Maven online repository.

1. In the Package explorer, double click the file pom.xml
POM location
2. In the main panel, click "pom.xml":
POM edition
3. Now, add the following code to the pom.xml file:


The file should look like this:
TestNG dependency
4. Add the following code to the pom.xml file:


The file should look like this:
ReportNG dependency
5. Finally, add the following code to the pom.xml file:


6. Now you need to add the Surefire plugin to use ReportNG to get better reports and also to specify an .xml file where the test cases will be listed. So, add the following lines to the pom.xml file:

                <suiteXmlFiles>                    <suiteXmlFile>src/test/java/net/testools/resources/testSuite.xml</suiteXmlFile>

Place that just outside <project></project>.

Run a basic test case

1. Create a simple test case. I created one that just opens the browser and closes it. This is the code:

package net.testools.test_project;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestoolsTest {
public TestoolsTest() {
WebDriver driver = new FirefoxDriver();

public void setUp() {

public void simpleTest() {

public void tearDown() {

And this is the folder structure I'm using:
Folder structure
2. Create the .xml file to run the test cases. If you see closely at the picture above, there's a file named testSuite.xml. Create it at the same folder level with the following code:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestSuite">
    <listener class-name="org.uncommons.reportng.HTMLReporter" />
    <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
  <test name="Test1">
      <class name="net.testools.test_project.TestoolsTest" />

And, finally, that's all. Now, to try your configuration, open a terminal and go to the root of your project (where the pom.xml file is). Now, run this command: mvn clean install. If everything went well, you should see something like this in the terminal after the test ran:
Tests run
And here's a screenshot of the report enhanced with ReportNG. This is generated by default at: /test-project/target/surefire-reports:
ReportNG report

I know that there's plenty of space for mistakes with this, so you probably won't get it working the first time, but it's actually easy once you understand how Maven and the tools work. Good luck!

1 comment: