Selenium with Grafana and InfluxDB - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Monday 13 January 2020

Selenium with Grafana and InfluxDB

Download Grafana from this link

https://grafana.com/grafana/download?platform=windows

Dowload zip file (Standalone file)
Unzip it
Go to CMD


Go to bin folder

cd C:\Users\NEERAJ\Downloads\grafana-6.3.5\bin

type
grafana-server.exe

Grafana will start

Go to below URL
http://localhost:3000/login

By default username and password is "admin"



Download Influx DB

https://portal.influxdata.com/downloads/

https://dl.influxdata.com/influxdb/releases/influxdb-1.7.8_windows_amd64.zip


Extract zip folder

Click on influxd.exe
Also

Click on influx.exe



Now you should see below

Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8

We will  create Database now

> CREATE DATABASE selenium

> use selenium
Using database selenium


In project Pom.xml, add below Dependency

<dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
            <version>2.12</version>
 </dependency>

We will write ExecutionListener

package com.selenium.grid;

import java.util.concurrent.TimeUnit;

import org.influxdb.dto.Point;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ExecutionListener implements ITestListener {

public void onTestStart(ITestResult iTestResult) {

    }

    public void onTestSuccess(ITestResult iTestResult) {
        this.sendTestMethodStatus(iTestResult, "PASS");
    }

    public void onTestFailure(ITestResult iTestResult) {
        this.sendTestMethodStatus(iTestResult, "FAIL");
    }

    public void onTestSkipped(ITestResult iTestResult) {
        this.sendTestMethodStatus(iTestResult, "SKIPPED");
    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {

    }

    public void onStart(ITestContext iTestContext) {

    }

    public void onFinish(ITestContext iTestContext) {
        this.sendTestClassStatus(iTestContext);
    }

    private void sendTestMethodStatus(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()) // hence we will have to //write test cases with description
                .tag("result", status)
                .addField("duration", (iTestResult.getEndMillis() - iTestResult.getStartMillis()))
                .build();
        ResultSender.send(point);
    }

    private void sendTestClassStatus(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();
        ResultSender.send(point);
    }
}


Important
Make sure that you write test cases with description, as you can see in ExecutionListener we are setting the description, so we need description of test cases

Example

@Test(description = "login")


Write Result Sender Class

package com.selenium.grid;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;

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);
}

}


Next Step
Write test cases

with Listener


@Listeners(ExecutionListener.class)

public class GridExampleTest1 {

  @Test(description = "login")

Data Source for Dashboard


Execute Test case and you will see the Dashboard showing you execution graph


Explore is the area where you can run query, just in case if you want to see the data


8 comments: