Real Time Selenium Reports using Grafana & InfluxDB
By: Kishan Enjapuri
Tags: Selenium, Testing, Grafana, Automation Testing, Influxdb

Real Time Selenium Reports using Grafana & InfluxDB

There are many reporting tools and frameworks for Selenium but most of them are OFFLINE means test results are visible only when the test execution gets ended.

Let’s say we have around 500 Test cases in our regression suite in the build pipeline.What if there is a series of tests failing among the 500 tests because of some issue in environment configuration.We have to wait for the reports to find out what went wrong .Otherwise, you might need to look into jenkins console for the test execution results log to see the progress. It might be annoying in certain cases.

To solve this issue, we are going to learn about how to create real time reports using Influxdb and Grafana.

What is InfluxDB?

InfluxDB is an open-source time-series database developed by InfluxData.

InfluxDB Installation(Docker) & Configuration (Windows)

For installation of InfluxDB, Docker and Docker Compose should be installed on the host machine where it will run.Docker installation is covered at the Docker website.Docker Compose installation is also covered at the Docker website.

Run the below docker command to run an instance of InfluxDB .

docker run -p 8086:8086 -v ${PWD}:/var/lib/influxdb influxdb

We have successfully intialised influxdb

We need a database for logging the Test results . Open cmd and type the query to create a database.

curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE selenium"

In this query we have created db schema named “selenium”

Next we will set up Grafana.

What is Grafana?

Grafana is an open-source metric analytics & visualization suite.

Grafana Installation(Docker) & Configuration (Windows)

Run the below docker command to run an instance of Grafana.

docker run -d -p 3000:3000

--name=grafana \

-e "GF_INSTALL_PLUGINS=grafana-piechart-panel" \

-v $PWD:/var/lib/grafana \

grafana/grafana

We have successfully intialised Grafana

Now, let’s launch the Grafana Dashboard. Go to any browser and launch this URL: http://localhost:3000

Username: admin & Password: admin

Adding Data Source (Influxdb) in Grafana

Go to Settings -> Data sources -> Add new data source as given here. Click on ‘Save & Test’ button to ensure that Grafana is able to talk to InfluxDB.

DataSource is added to Grafana

Configure Charts in Grafana

Click on Create Dashboard -> Click on Add Query.

Create a Graph and configure the query as below:

For Test Case Execution Time Point Graph:

Test Results Count Pie-Chart :

And Save the Dashboard and select the Auto- refresh rate .

Dashboard will look like this

Now we will create a simple Maven project .

Create a simple Test class:

public class InfluxGrafanaTests { @Test(description = "Login")

public void launchApp() throws Exception {

Thread.sleep(10000);

Assert.fail(); } @Test(description = "Search")

public void sort() throws Exception {

Thread.sleep(10000);

Assert.fail(); } @Test(description = "Select")

public void selectAProduct() throws Exception {

Thread.sleep(10000);



} @Test(description = "Log Out")

public void goToAboutPage() throws Exception {

Thread.sleep(10000);



} }

Create a new class called InfluxDBListener which implements ITestListner of TestNG and copy & paste the below code there:

public class InfluxDBListener implements ITestListener {



public void onTestStart(ITestResult iTestResult) {



}



public void onTestSuccess(ITestResult iTestResult) {

this.postTestMethodStatus(iTestResult, "PASS");

}



public void onTestFailure(ITestResult iTestResult) {

this.postTestMethodStatus(iTestResult, "FAIL");

}



public void onTestSkipped(ITestResult iTestResult) {

this.postTestMethodStatus(iTestResult, "SKIPPED");

}



public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {



}



public void onStart(ITestContext iTestContext) {



}



public void onFinish(ITestContext iTestContext) {

this.postTestClassStatus(iTestContext);

}



private void postTestMethodStatus(ITestResult iTestResult, String status) {

Point point = Point.measurement("testmethod").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)

.tag("testclass", iTestResult.getTestClass().getName()).tag("name", iTestResult.getName())

.tag("description", iTestResult.getMethod().getDescription()).tag("result", status)

.addField("duration", (iTestResult.getEndMillis() - iTestResult.getStartMillis())).build();

UpdateResults.post(point);

}



private void postTestClassStatus(ITestContext iTestContext) {

Point point = Point.measurement("testclass").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)

.tag("name", iTestContext.getAllTestMethods()[0].getTestClass().getName())

.addField("duration", (iTestContext.getEndDate().getTime() - iTestContext.getStartDate().getTime()))

.build();

UpdateResults.post(point);

}



}

Above listener will monitor the test execution and as soon as a method/test class is executed, it sends the name, duration and few other details.

Add influxdb-java dependency in your pom.xml

<dependency>

<groupId>org.influxdb</groupId>

<artifactId>influxdb-java</artifactId>

<version>2.12</version>

</dependency>

Lets create a simple class which is responsible for sending the results to InfluxDB.

public class ResultSender {



private static final InfluxDB INFLXUDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root");

private static final String DATABASE = "selenium";



static{

INFLXUDB.setDatabase(DATABASE);

}



public static void send(final Point point){

INFLXUDB.write(point);

}



}

Here we are setting the Database as “selenium” (which we already added in influxdb)

Now include the listener in the suite xml or in the base TestNG class.



<suite name="My suite">

<listeners>

<listener class-name="com.utils.InfluxDBListener" />

</listeners>

<test name="Test1">

<classes>

<class name="com.tests.InfluxGrafanaTests"/>

</classes>

</test>

</suite>

Time for Action !!!

Let’s run the tests using testng file. Navigate to Grafana and see the test results updating dynamically.

To see the test results updating dynamically watch this below video

Hope this project was interesting in setting up the real time results using InfluxDB and Grafana . This article gives only a high level idea.You could further enhance this according to your needs.

Thanks.