Run Testng automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package com.adv.qa.selenium.helpers;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.testng.ISuite;
import org.testng.reporters.EmailableReporter;
import org.testng.xml.XmlSuite;
public class ReportListeners extends EmailableReporter{
String prefix = new SimpleDateFormat("yyyyMMddhhmm").format(new Date());
EmailableReporter email = new EmailableReporter();
@Override
public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1,String arg2)
{
super.generateReport(arg0, arg1, arg2);
}
@Override
protected PrintWriter createWriter(String outdir) throws IOException {
new File(outdir).mkdirs();
return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir+"\\HTMLReports","emailable-report"+prefix+".html"))));
}
}
package test.reports;
import org.testng.Assert;
import org.testng.IReporter;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.reporters.EmailableReporter;
import org.testng.reporters.EmailableReporter2;
import test.SimpleBaseTest;
import java.io.File;
import java.lang.reflect.Method;
import java.security.Permission;
public class EmailableReporterTest extends SimpleBaseTest {
private SecurityManager manager;
@BeforeClass(alwaysRun = true)
public void setup() {
manager = System.getSecurityManager();
System.setSecurityManager(new MySecurityManager(manager));
}
@AfterClass(alwaysRun = true)
public void cleanup() {
System.setSecurityManager(manager);
}
@Test(dataProvider = "getReporterInstances", priority = 1)
public void testReportsNameCustomizationViaRunMethodInvocationAndJVMArguments(IReporter reporter, String jvm) {
runTestViaRunMethod(reporter, jvm);
}
@Test(dataProvider = "getReporterInstances", priority = 2)
public void testReportsNameCustomizationViaRunMethodInvocation(IReporter reporter) {
runTestViaRunMethod(reporter, null /* no jvm arguments */);
}
@Test(dataProvider = "getReporterNames", priority = 3)
public void testReportsNameCustomizationViaMainMethodInvocation(String clazzName) {
runTestViaMainMethod(clazzName, null /* no jvm arguments */);
}
@Test(dataProvider = "getReporterNames", priority = 4)
public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments(String clazzName, String jvm) {
runTestViaMainMethod(clazzName, jvm);
}
@DataProvider(name = "getReporterInstances")
public Object[][] getReporterInstances(Method method) {
if (method.getName().toLowerCase().contains("jvmarguments")) {
return new Object[][] {
{new EmailableReporter(), "emailable.report.name"},
{new EmailableReporter2(), "emailable.report2.name"}
};
}
return new Object[][] {
{new EmailableReporter()},
{new EmailableReporter2()}
};
}
@DataProvider(name = "getReporterNames")
public Object[][] getReporterNames(Method method) {
if (method.getName().toLowerCase().contains("jvmarguments")) {
return new Object[][] {
{EmailableReporter.class.getName(), "emailable.report.name"},
{EmailableReporter2.class.getName(), "emailable.report2.name"}
};
}
return new Object[][] {
{EmailableReporter.class.getName()},
{EmailableReporter2.class.getName()}
};
}
private void runTestViaMainMethod(String clazzName, String jvm) {
String name = Long.toString(System.currentTimeMillis());
File output = createDirInTempDir(name);
String filename = "report" + name + ".html";
String[] args = {"-d", output.getAbsolutePath(), "-reporter", clazzName +
":fileName=" + filename, "src/test/resources/1332.xml"};
try {
if (jvm != null) {
System.setProperty(jvm, filename);
}
TestNG.main(args);
if (jvm != null) {
//reset the jvm arguments
System.setProperty(jvm, "");
}
} catch (SecurityException t) {
//Gobble Security exception
}
File actual = new File(output.getAbsolutePath(), filename);
Assert.assertEquals(actual.exists(), true);
}
private void runTestViaRunMethod(IReporter reporter, String jvm) {
String name = Long.toString(System.currentTimeMillis());
File output = createDirInTempDir(name);
String filename = "report" + name + ".html";
if (jvm != null) {
System.setProperty(jvm, filename);
}
TestNG testNG = create();
testNG.setOutputDirectory(output.getAbsolutePath());
if (reporter instanceof EmailableReporter2) {
((EmailableReporter2) reporter).setFileName(filename);
}
if (reporter instanceof EmailableReporter) {
((EmailableReporter) reporter).setFileName(filename);
}
testNG.addListener((ITestNGListener) reporter);
testNG.setTestClasses(new Class[] {ReporterSample.class});
testNG.run();
if (jvm != null) {
//reset the jvm argument if it was set
System.setProperty(jvm, "");
}
File actual = new File(output.getAbsolutePath(), filename);
Assert.assertEquals(actual.exists(), true);
}
public static class MySecurityManager extends SecurityManager {
private SecurityManager baseSecurityManager;
MySecurityManager(SecurityManager baseSecurityManager) {
this.baseSecurityManager = baseSecurityManager;
}
@Override
public void checkPermission(Permission permission) {
if (permission.getName().startsWith("exitVM")) {
throw new SecurityException("System exit not allowed");
}
if (baseSecurityManager != null) {
baseSecurityManager.checkPermission(permission);
}
}
}
}
package Utilities;
import java.util.List;
import org.testng.ISuite;
import org.testng.reporters.EmailableReporter;
import org.testng.xml.XmlSuite;
public class AfterSuite extends EmailableReporter
{
@Override
public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {
super.generateReport(xml, suites, outdir);
Email e= new Email();
try {
e.Emailsend();
}catch (Exception e1) {
e1.printStackTrace();
}
}
}
attributes.setProperty(XMLReporterConfig.ATTR_NAME, testResult.getMethod().getMethodName());
@AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result, ITestContext context) {
try {
BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod().clone();
String featureName = StringUtils.substringBetween(result.getParameters()[1].toString(), "[", "]");
String scenarioName = result.getParameters()[0].toString().replace("\"", "");
Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
f.setAccessible(true);
f.set(baseTestMethod, featureName + " - " + scenarioName);
f = result.getClass().getDeclaredField("m_method");
f.setAccessible(true);
f.set(result, baseTestMethod);
} catch (Exception e) {
e.printStackTrace();
}
}
import org.testng.Assert;
import org.testng.ITest;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class NewDataProviderTest implements ITest {
//The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread
private ThreadLocal<String> testName = new ThreadLocal<>();
/*
* TestNG, for some reason, when building different reports, calls getName() on the test while building the report.
* This is fine if you are not using a data provider to generate different runs and set a unique name for each run by using the ITest strategy.
* If you are using a data provider to generate multiple runs of the same test and want each run to have a unique name then there is a problem.
* As the ITest strategy returns the name for the test as the name set by the last run.
* */
private int emailNameIndex = 0;
private int htmlNameIndex = 0;
private int xmlNameIndex = 0;
private ArrayList<String> allTests = new ArrayList<String>();
/*
* TestHTMLReporter gets the name by first getting all the names for failed tests and then the names for passing tests
* Hence keeping them in 2 separate lists
* */
private ArrayList<String> passedTests = new ArrayList<String>();
private ArrayList<String> failedTests = new ArrayList<String>();
@BeforeClass(alwaysRun = true)
public void initialize() {
this.testName.set("");
}
@BeforeMethod(alwaysRun = true)
public void setCustomTestcaseName(Method method, Object[] testData) {
//Set the default name
this.testName.set(method.getName());
//Change the test name only if Dataprovider is used
//Check if data provider is used in the test
if (testData != null && testData.length > 0) {
System.out.println("\n\nParameters "+testData[0]+" are passed to the test - "+method.getName());
//Taking 1st field in the Dataprovider for updating testname - can be changed as desired maybe using a variable
//I'm changing the name only if the Dataprovider field is String
if (testData[0] instanceof String) {
//Taking 1st field in the Dataprovider for updating testname - can be changed as desired
System.out.println("I'm setting custom name to the test as "+method.getName() + "_" + testData[0]);
this.testName.set(method.getName() + "_" + testData[0]);
}
}
//Add the name to the collection that stores all list names
allTests.add(testName.get());
}
@AfterMethod (alwaysRun = true)
public void setTheTestcaseNameInResult(ITestResult result, Method method) {
//Fill the Passed and Failed tests collections
try {
if(result.getStatus() == ITestResult.SUCCESS) {
System.out.println("Adding "+ result.getTestName() + " to passed tests collection");
passedTests.add(result.getTestName());
}
if(result.getStatus() == ITestResult.FAILURE) {
System.out.println("Adding " + result.getTestName() + " to FAILURE tests collection");
failedTests.add(result.getTestName());
}
} catch (Exception e) {
e.printStackTrace();
}
// To change display name in HTML report
//Only changing the name if the parameter is instance of String
if(iTestResult.getParameters().length > 0) {
if (iTestResult.getParameters()[0] instanceof String) {
System.out.println("Updating the name as Parameters are passed to the test-"+method.getName());
try {
/* This helps in setting unique name to method for each test instance for a data provider*/
Field resultMethod = TestResult.class.getDeclaredField("m_method");
resultMethod.setAccessible(true);
resultMethod.set(iTestResult, iTestResult.getMethod().clone());
Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
methodName.setAccessible(true);
methodName.set(iTestResult.getMethod(), this.getTestName());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("New Name is - " + iTestResult.getMethod().getMethodName());
}
}
}
@Override
public String getTestName() {
String name = testName.get();
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();// .toString();
//This is called
if (isCalledFromMethod(stackTrace, "XMLSuiteResultWriter")) {
//System.out.println("Got called from XMLSuiteResultWriter");
if (allTestNames.size() > 0) {
if (xmlNameIndex < allTestNames.size()) {
name = allTestNames.get(xmlNameIndex);
} else {
name = allTestNames.get(0);
}
} else {
name = "undefined";
}
xmlNameIndex++;
if (xmlNameIndex >= allTestNames.size()) {
xmlNameIndex = 0;
}
// System.out.println("Got called from XMLSuiteResultWriter returning name - "+name);
} else if (isCalledFromMethod(stackTrace, "EmailableReporter")) {
if (allTestNames.size() > 0) {
if (emailNameIndex < allTestNames.size()) {
name = allTestNames.get(emailNameIndex);
} else {
name = allTestNames.get(0);
}
} else {
name = "undefined";
}
emailNameIndex++;
if (emailNameIndex >= allTestNames.size()) {
emailNameIndex = 0;
}
System.out.println("Got called from EmailableReporter returning name -"+name);
}
if (isCalledFromMethod(stackTrace, "TestHTMLReporter")) {
if (allTestNames.size() <= 0) {
name = "undefined";
} else {
if (htmlNameIndex < failedTestNames.size()) {
name = failedTestNames.get(htmlNameIndex);
} else {
int htmlPassedIndex = htmlNameIndex - failedTestNames.size();
if (htmlPassedIndex < passedTestNames.size()) {
name = passedTestNames.get(htmlPassedIndex);
} else {
name = "undefined";
}
}
}
htmlNameIndex++;
if (htmlNameIndex >= allTestNames.size()) {
htmlNameIndex = 0;
}
System.out.println("Got called from TestHTMLReporter returning name - "+name);
}
System.out.println("Returning testname as-"+name);
return name;
}
private boolean isCalledFromMethod(StackTraceElement[] stackTrace, String checkForMethod) {
boolean calledFrom = false;
for (StackTraceElement element : stackTrace) {
String stack = element.toString();
// System.out.println("Rohit the called from value is:"+stack);
if (stack.contains(checkForMethod))
calledFrom = true;
}
return calledFrom;
}
@Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
public void dataProviderTest(String username) {
System.out.println("\n\nI'm in dataProviderTest with data-"+username);
/* Fail the test if value is L2 - deliberately so that we have failed test in report */
if(username.contains("L2")) {
Assert.fail();
}
}
@Test(dependsOnMethods = "dataProviderTest", groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
public void dataProviderDependentTest(String username) {
System.out.println("\n\nI'm in dataProvider DEPENDENT Test with data-"+username);
}
//This test consumes data of type list so the name will not be updated in report
@Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp2")
public void dataListProviderTest(List<String[]> list) {
Object[] arr = list.get(0);
List<Object> arrList = Arrays.asList(arr);
Iterator<Object> iterator = arrList.iterator();
while (iterator.hasNext()) {
String[] data = (String[]) iterator.next();
System.out.println("In list test - "+data[0]);
}
}
@DataProvider(name="dp1")
public Object[][] getDataForTest(ITestContext iTestContext){
Object[][] L1 = new Object[][] {
{"L1"}, {"L2"}, {"L3"}
};
return L1;
}
@DataProvider(name="dp2")
public Object[][] getDataListForTest(ITestContext iTestContext){
List<Object[][]> list = new ArrayList<Object[][]>();
Object[][] L1 = new Object[][] {
new String [] {"L1", "l1"},
new String [] {"L1", "l1"}
};
list.add(L1);
return new Object[][] { { list } };
}
}
public class NinjaTest {
...
...
@AfterMethod (alwaysRun = true)
public void afterMethod(ITestResult result, Method method) {
try {
//I have XML test suites organized in directories.
String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
//Total hack to change display name in HTML report (^o^)ïŒ
Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
methodName.setAccessible(true);
methodName.set(result.getMethod(), testMethodName);
} catch (Exception e) {
// Eh.... ÂŻ\_(ă)_/ÂŻ
e.printStackTrace();
}
}
...
...
public String getTestName()
{
String name = testName;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();//.toString();
if(calledFrom(stackTrace, "XMLSuiteResultWriter"))
{
name = testNames.size()>0?testNames.get(xmlNameIndex<testNames.size()?xmlNameIndex:0):"undefined";
xmlNameIndex++;
if(xmlNameIndex>=testNames.size())
xmlNameIndex = 0;
}
else if(calledFrom(stackTrace, "EmailableReporter"))
{
name = testNames.size()>0?testNames.get(emailNameIndex<testNames.size()?emailNameIndex:0):"undefined";
emailNameIndex++;
if(emailNameIndex>=testNames.size())
emailNameIndex = 0;
}
if(calledFrom(stackTrace, "TestHTMLReporter"))
{
if(testNames.size()<0)
{
name = "undefined";
}
else
{
if(htmlNameIndex < testNamesFailed.size())
{
name = testNamesFailed.get(htmlNameIndex);
}
else
{
int htmlPassedIndex = htmlNameIndex - testNamesFailed.size();
if(htmlPassedIndex < testNamesPassed.size())
{
name = testNamesPassed.get(htmlPassedIndex);
}
else
{
name = "undefined";
}
}
}
htmlNameIndex++;
if(htmlNameIndex>=testNames.size())
htmlNameIndex = 0;
}
return name;
}
private boolean calledFrom(StackTraceElement[] stackTrace, String checkForMethod)
{
boolean calledFrom = false;
for(StackTraceElement element : stackTrace)
{
String stack = element.toString();
if(stack.contains(checkForMethod))
calledFrom = true;
}
return calledFrom;
}
//oversimplified for demontration purposes
public class TestParameters {
private String testName = null;
private String testDescription = null;
public TestParameters(String name,
String description) {
this.testName = name;
this.testDescription = description;
}
public String getTestName() {
return testName;
}
public String getTestDescription() {
return testDescription;
}
}
public class SampleTest implements ITest {
// Has to be set to prevent NullPointerException from reporters
protected String mTestCaseName = "";
@DataProvider(name="BasicDataProvider")
public Object[][] getTestData() {
Object[][] data = new Object[][] {
{ new TestParameters("TestCase1", "Sample test 1")},
{ new TestParameters("TestCase2", "Sample test 2")},
{ new TestParameters("TestCase3", "Sample test 3")},
{ new TestParameters("TestCase4", "Sample test 4")},
{ new TestParameters("TestCase5", "Sample test 5") }
};
return data;
}
@BeforeMethod(alwaysRun = true)
public void testData(Method method, Object[] testData) {
String testCase = "";
if (testData != null && testData.length > 0) {
TestParameters testParams = null;
//Check if test method has actually received required parameters
for (Object testParameter : testData) {
if (testParameter instanceof TestParameters) {
testParams = (TestParameters)testParameter;
break;
}
}
if (testParams != null) {
testCase = testParams.getTestName();
}
}
this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
}
@Override
public String getTestName() {
return this.mTestCaseName;
}
@Test(dataProvider="BasicDataProvider")
public void testSample1(TestParameters testParams){
//test code here
}
@Test(dataProvider="BasicDataProvider")
public void testSample2(TestParameters testParams){
//test code here
}
@Test
public void testSample3(){
//test code here
}
}
<testng-results skipped="0" failed="0" total="5" passed="5">
<suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test-method
status="PASS"
signature="testSample1(org.example.test.TestParameters)[pri:0, instance:[email protected]]"
test-instance-name="testSample1(TestCase5)"
name="testSample1"
duration-ms="1014"
started-at="<some-time-before>"
data-provider="BasicDataProvider"
finished-at="<some-time-later>" >
<!-- excluded for demonstration purposes -->
</test-method>
<!-- the rest of test results excluded for brevity -->
</test>
</suite>
</testng-result>
attributes.setProperty(XMLReporterConfig.ATTR_NAME, testResult.getMethod().getMethodName());
@AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result, ITestContext context) {
try {
BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod().clone();
String featureName = StringUtils.substringBetween(result.getParameters()[1].toString(), "[", "]");
String scenarioName = result.getParameters()[0].toString().replace("\"", "");
Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
f.setAccessible(true);
f.set(baseTestMethod, featureName + " - " + scenarioName);
f = result.getClass().getDeclaredField("m_method");
f.setAccessible(true);
f.set(result, baseTestMethod);
} catch (Exception e) {
e.printStackTrace();
}
}
import org.testng.Assert;
import org.testng.ITest;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class NewDataProviderTest implements ITest {
//The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread
private ThreadLocal<String> testName = new ThreadLocal<>();
/*
* TestNG, for some reason, when building different reports, calls getName() on the test while building the report.
* This is fine if you are not using a data provider to generate different runs and set a unique name for each run by using the ITest strategy.
* If you are using a data provider to generate multiple runs of the same test and want each run to have a unique name then there is a problem.
* As the ITest strategy returns the name for the test as the name set by the last run.
* */
private int emailNameIndex = 0;
private int htmlNameIndex = 0;
private int xmlNameIndex = 0;
private ArrayList<String> allTests = new ArrayList<String>();
/*
* TestHTMLReporter gets the name by first getting all the names for failed tests and then the names for passing tests
* Hence keeping them in 2 separate lists
* */
private ArrayList<String> passedTests = new ArrayList<String>();
private ArrayList<String> failedTests = new ArrayList<String>();
@BeforeClass(alwaysRun = true)
public void initialize() {
this.testName.set("");
}
@BeforeMethod(alwaysRun = true)
public void setCustomTestcaseName(Method method, Object[] testData) {
//Set the default name
this.testName.set(method.getName());
//Change the test name only if Dataprovider is used
//Check if data provider is used in the test
if (testData != null && testData.length > 0) {
System.out.println("\n\nParameters "+testData[0]+" are passed to the test - "+method.getName());
//Taking 1st field in the Dataprovider for updating testname - can be changed as desired maybe using a variable
//I'm changing the name only if the Dataprovider field is String
if (testData[0] instanceof String) {
//Taking 1st field in the Dataprovider for updating testname - can be changed as desired
System.out.println("I'm setting custom name to the test as "+method.getName() + "_" + testData[0]);
this.testName.set(method.getName() + "_" + testData[0]);
}
}
//Add the name to the collection that stores all list names
allTests.add(testName.get());
}
@AfterMethod (alwaysRun = true)
public void setTheTestcaseNameInResult(ITestResult result, Method method) {
//Fill the Passed and Failed tests collections
try {
if(result.getStatus() == ITestResult.SUCCESS) {
System.out.println("Adding "+ result.getTestName() + " to passed tests collection");
passedTests.add(result.getTestName());
}
if(result.getStatus() == ITestResult.FAILURE) {
System.out.println("Adding " + result.getTestName() + " to FAILURE tests collection");
failedTests.add(result.getTestName());
}
} catch (Exception e) {
e.printStackTrace();
}
// To change display name in HTML report
//Only changing the name if the parameter is instance of String
if(iTestResult.getParameters().length > 0) {
if (iTestResult.getParameters()[0] instanceof String) {
System.out.println("Updating the name as Parameters are passed to the test-"+method.getName());
try {
/* This helps in setting unique name to method for each test instance for a data provider*/
Field resultMethod = TestResult.class.getDeclaredField("m_method");
resultMethod.setAccessible(true);
resultMethod.set(iTestResult, iTestResult.getMethod().clone());
Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
methodName.setAccessible(true);
methodName.set(iTestResult.getMethod(), this.getTestName());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("New Name is - " + iTestResult.getMethod().getMethodName());
}
}
}
@Override
public String getTestName() {
String name = testName.get();
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();// .toString();
//This is called
if (isCalledFromMethod(stackTrace, "XMLSuiteResultWriter")) {
//System.out.println("Got called from XMLSuiteResultWriter");
if (allTestNames.size() > 0) {
if (xmlNameIndex < allTestNames.size()) {
name = allTestNames.get(xmlNameIndex);
} else {
name = allTestNames.get(0);
}
} else {
name = "undefined";
}
xmlNameIndex++;
if (xmlNameIndex >= allTestNames.size()) {
xmlNameIndex = 0;
}
// System.out.println("Got called from XMLSuiteResultWriter returning name - "+name);
} else if (isCalledFromMethod(stackTrace, "EmailableReporter")) {
if (allTestNames.size() > 0) {
if (emailNameIndex < allTestNames.size()) {
name = allTestNames.get(emailNameIndex);
} else {
name = allTestNames.get(0);
}
} else {
name = "undefined";
}
emailNameIndex++;
if (emailNameIndex >= allTestNames.size()) {
emailNameIndex = 0;
}
System.out.println("Got called from EmailableReporter returning name -"+name);
}
if (isCalledFromMethod(stackTrace, "TestHTMLReporter")) {
if (allTestNames.size() <= 0) {
name = "undefined";
} else {
if (htmlNameIndex < failedTestNames.size()) {
name = failedTestNames.get(htmlNameIndex);
} else {
int htmlPassedIndex = htmlNameIndex - failedTestNames.size();
if (htmlPassedIndex < passedTestNames.size()) {
name = passedTestNames.get(htmlPassedIndex);
} else {
name = "undefined";
}
}
}
htmlNameIndex++;
if (htmlNameIndex >= allTestNames.size()) {
htmlNameIndex = 0;
}
System.out.println("Got called from TestHTMLReporter returning name - "+name);
}
System.out.println("Returning testname as-"+name);
return name;
}
private boolean isCalledFromMethod(StackTraceElement[] stackTrace, String checkForMethod) {
boolean calledFrom = false;
for (StackTraceElement element : stackTrace) {
String stack = element.toString();
// System.out.println("Rohit the called from value is:"+stack);
if (stack.contains(checkForMethod))
calledFrom = true;
}
return calledFrom;
}
@Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
public void dataProviderTest(String username) {
System.out.println("\n\nI'm in dataProviderTest with data-"+username);
/* Fail the test if value is L2 - deliberately so that we have failed test in report */
if(username.contains("L2")) {
Assert.fail();
}
}
@Test(dependsOnMethods = "dataProviderTest", groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
public void dataProviderDependentTest(String username) {
System.out.println("\n\nI'm in dataProvider DEPENDENT Test with data-"+username);
}
//This test consumes data of type list so the name will not be updated in report
@Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp2")
public void dataListProviderTest(List<String[]> list) {
Object[] arr = list.get(0);
List<Object> arrList = Arrays.asList(arr);
Iterator<Object> iterator = arrList.iterator();
while (iterator.hasNext()) {
String[] data = (String[]) iterator.next();
System.out.println("In list test - "+data[0]);
}
}
@DataProvider(name="dp1")
public Object[][] getDataForTest(ITestContext iTestContext){
Object[][] L1 = new Object[][] {
{"L1"}, {"L2"}, {"L3"}
};
return L1;
}
@DataProvider(name="dp2")
public Object[][] getDataListForTest(ITestContext iTestContext){
List<Object[][]> list = new ArrayList<Object[][]>();
Object[][] L1 = new Object[][] {
new String [] {"L1", "l1"},
new String [] {"L1", "l1"}
};
list.add(L1);
return new Object[][] { { list } };
}
}
public class NinjaTest {
...
...
@AfterMethod (alwaysRun = true)
public void afterMethod(ITestResult result, Method method) {
try {
//I have XML test suites organized in directories.
String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
//Total hack to change display name in HTML report (^o^)ïŒ
Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
methodName.setAccessible(true);
methodName.set(result.getMethod(), testMethodName);
} catch (Exception e) {
// Eh.... ÂŻ\_(ă)_/ÂŻ
e.printStackTrace();
}
}
...
...
public String getTestName()
{
String name = testName;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();//.toString();
if(calledFrom(stackTrace, "XMLSuiteResultWriter"))
{
name = testNames.size()>0?testNames.get(xmlNameIndex<testNames.size()?xmlNameIndex:0):"undefined";
xmlNameIndex++;
if(xmlNameIndex>=testNames.size())
xmlNameIndex = 0;
}
else if(calledFrom(stackTrace, "EmailableReporter"))
{
name = testNames.size()>0?testNames.get(emailNameIndex<testNames.size()?emailNameIndex:0):"undefined";
emailNameIndex++;
if(emailNameIndex>=testNames.size())
emailNameIndex = 0;
}
if(calledFrom(stackTrace, "TestHTMLReporter"))
{
if(testNames.size()<0)
{
name = "undefined";
}
else
{
if(htmlNameIndex < testNamesFailed.size())
{
name = testNamesFailed.get(htmlNameIndex);
}
else
{
int htmlPassedIndex = htmlNameIndex - testNamesFailed.size();
if(htmlPassedIndex < testNamesPassed.size())
{
name = testNamesPassed.get(htmlPassedIndex);
}
else
{
name = "undefined";
}
}
}
htmlNameIndex++;
if(htmlNameIndex>=testNames.size())
htmlNameIndex = 0;
}
return name;
}
private boolean calledFrom(StackTraceElement[] stackTrace, String checkForMethod)
{
boolean calledFrom = false;
for(StackTraceElement element : stackTrace)
{
String stack = element.toString();
if(stack.contains(checkForMethod))
calledFrom = true;
}
return calledFrom;
}
//oversimplified for demontration purposes
public class TestParameters {
private String testName = null;
private String testDescription = null;
public TestParameters(String name,
String description) {
this.testName = name;
this.testDescription = description;
}
public String getTestName() {
return testName;
}
public String getTestDescription() {
return testDescription;
}
}
public class SampleTest implements ITest {
// Has to be set to prevent NullPointerException from reporters
protected String mTestCaseName = "";
@DataProvider(name="BasicDataProvider")
public Object[][] getTestData() {
Object[][] data = new Object[][] {
{ new TestParameters("TestCase1", "Sample test 1")},
{ new TestParameters("TestCase2", "Sample test 2")},
{ new TestParameters("TestCase3", "Sample test 3")},
{ new TestParameters("TestCase4", "Sample test 4")},
{ new TestParameters("TestCase5", "Sample test 5") }
};
return data;
}
@BeforeMethod(alwaysRun = true)
public void testData(Method method, Object[] testData) {
String testCase = "";
if (testData != null && testData.length > 0) {
TestParameters testParams = null;
//Check if test method has actually received required parameters
for (Object testParameter : testData) {
if (testParameter instanceof TestParameters) {
testParams = (TestParameters)testParameter;
break;
}
}
if (testParams != null) {
testCase = testParams.getTestName();
}
}
this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
}
@Override
public String getTestName() {
return this.mTestCaseName;
}
@Test(dataProvider="BasicDataProvider")
public void testSample1(TestParameters testParams){
//test code here
}
@Test(dataProvider="BasicDataProvider")
public void testSample2(TestParameters testParams){
//test code here
}
@Test
public void testSample3(){
//test code here
}
}
<testng-results skipped="0" failed="0" total="5" passed="5">
<suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test-method
status="PASS"
signature="testSample1(org.example.test.TestParameters)[pri:0, instance:[email protected]]"
test-instance-name="testSample1(TestCase5)"
name="testSample1"
duration-ms="1014"
started-at="<some-time-before>"
data-provider="BasicDataProvider"
finished-at="<some-time-later>" >
<!-- excluded for demonstration purposes -->
</test-method>
<!-- the rest of test results excluded for brevity -->
</test>
</suite>
</testng-result>
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTestâs cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.