Report Generate Module 20 | Selenium Forum
M
Posted on 28/01/2016
HI Team,

When I run test case through command prompt, as mvn test
code running successfully but report is not generated. It shows null under Target-->Report folder

Below is my customlistner class file
--------------
package com.testing.framework.DDF;
import com.testing.framework.DDF.util.ErrorUtil;
import java.io.File;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.internal.Utils;

import com.testing.framework.DDF.util.Constants;
import com.testing.framework.DDF.util.Xls_Reader;



public class CustomListener extends TestListenerAdapter implements IInvokedMethodListener,ISuiteListener{

public static Hashtable<String, String> resultTable;
public static ArrayList<String> keys;
public static String resultFolderName;
public static String resultFilePath;
public static Xls_Reader xls;


public void onTestFailure(ITestResult tr){
// report(tr.getName(), tr.getThrowable().getMessage());

List<Throwable> verificationFailures = ErrorUtil.getVerificationFailures();
String errMsg="";
for(int i=0;i<verificationFailures.size();i++){
errMsg=errMsg+"["+verificationFailures.get(i).getMessage()+"]-";
}
report(tr.getName(), errMsg);

}

public void onTestSkipped(ITestResult tr) {
report(tr.getName(), tr.getThrowable().getMessage());


}

public void onTestSuccess(ITestResult tr){
report(tr.getName(), "PASS");

}

public void afterInvocation(IInvokedMethod method, ITestResult result) {

Reporter.setCurrentTestResult(result);

if (method.isTestMethod()) {
List<Throwable> verificationFailures = ErrorUtil.getVerificationFailures();
//if there are verification failures...
if (verificationFailures.size() != 0) {
//set the test to failed
result.setStatus(ITestResult.FAILURE);

//if there is an assertion failure add it to verificationFailures
if (result.getThrowable() != null) {
verificationFailures.add(result.getThrowable());
}

int size = verificationFailures.size();
//if there's only one failure just set that
if (size == 1) {
result.setThrowable(verificationFailures.get(0));
} else {
//create a failure message with all failures and stack traces (except last failure)
StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):nn");
for (int i = 0; i < size-1; i++) {
failureMessage.append("Failure ").append(i+1).append(" of ").append(size).append(":n");
Throwable t = verificationFailures.get(i);
String fullStackTrace = Utils.stackTrace(t, false)[1];
failureMessage.append(fullStackTrace).append("nn");
}

//final failure
Throwable last = verificationFailures.get(size-1);
failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":n");
failureMessage.append(last.toString());

//set merged throwable
Throwable merged = new Throwable(failureMessage.toString());
merged.setStackTrace(last.getStackTrace());

result.setThrowable(merged);

}
}

}


}

public void beforeInvocation(IInvokedMethod arg0, ITestResult test)
{
}


@Override
public void onStart(ISuite suite) {
System.out.println("Starting suite "+ suite.getName());
if(resultTable==null){
keys = new ArrayList<String>();
resultTable = new Hashtable<String,String>();
}

if(resultFolderName==null){// first Suite
Date d = new Date();
resultFolderName=d.toString().replace(":", "_");
File f = new File(System.getProperty("user.dir")+"//target//reports//"+resultFolderName);
f.mkdir();
// change
resultFilePath=System.getProperty("user.dir")+"//target//reports//"+resultFolderName+"//Report.xlsx";
File src = new File(System.getProperty("user.dir")+"//target//reports//ReportTemplate.xlsx");
File dest = new File(resultFilePath);
try {
FileUtils.copyFile(src, dest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


@Override
public void onFinish(ISuite suite) {
if(resultTable !=null){
System.out.println("Finishing suite "+ suite.getName());
System.out.println(resultTable);
System.out.println(keys);

// write results in xlsx
if(!suite.getName().equals(Constants.ROOT_SUITE)){
Xls_Reader xls = new Xls_Reader(resultFilePath);
xls.addSheet(suite.getName());
// add the results in the sheet

// create col Names
xls.setCellData(suite.getName(), 0, 1, "Test Case");
xls.setCellData(suite.getName(), 1, 1, "Result");

for(int i=0;i<keys.size();i++){
String key = keys.get(i);
String result = resultTable.get(key);
xls.setCellData(suite.getName(), 0, i+2, key);
xls.setCellData(suite.getName(), 1, i+2, result);

}

}

resultTable=null;
keys=null;
}

}

public void report(String name,String result){
// test Iteration 1
// test Iteration 2
int iteration_number=1;
while(resultTable.containsKey(name+" Iteration "+iteration_number)){
iteration_number++;
}
keys.add(name+" Iteration "+iteration_number);
resultTable.put(name+" Iteration "+iteration_number, result);

}

}

M
Replied on 28/01/2016

are you using extent reports?


M
Replied on 30/01/2016

Yes. I am


M
Replied on 31/01/2016

Hi team,

When i run test case individually from eclipse. It is working fine but if i go for grid parallel execution. Say , a test case executing in two chrome browser. Both the chrome browser is launching but the issue is only one browser proceeding with executing other browser remain idle.

Also the one which is continuing with the execution is holding somewhere in the flow and unable to complete execution..

What will be the possible reason for this

Thanks


M
Replied on 31/01/2016

how many instances Firefox are on node?


M
Replied on 01/02/2016

I tried with
IE -1
Chrome -1
Mozilla -1

also
IE-1
CHROME 2
MOZILLA -2

In both the cases it is unable to run test case in more than one browser at a time.


M
Replied on 02/02/2016

have you tried running you project with out GRID?


M
Replied on 10/02/2016

Yes, It is running successfully.

If I use grid with single browser also working fine

Issue only when i try to run in more that one browser


M
Replied on 11/02/2016

check your settings with these blog.

http://blog.wedoqa.com/2013/07/how-to-run-parallel-tests-with-selenium-webdriver-and-testng-2/