How to use EmailableReporter class of org.testng.reporters package

Best Testng code snippet using org.testng.reporters.EmailableReporter

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.adv.qa.selenium.helpers;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.io.PrintWriter;
8import java.text.SimpleDateFormat;
9import java.util.Date;
10import java.util.List;
11
12import org.testng.ISuite;
13import org.testng.reporters.EmailableReporter;
14import org.testng.xml.XmlSuite;
15
16public class ReportListeners extends EmailableReporter{
17	String prefix = new SimpleDateFormat("yyyyMMddhhmm").format(new Date());
18	EmailableReporter email = new EmailableReporter();
19	@Override
20	public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1,String arg2)
21	{
22			super.generateReport(arg0, arg1, arg2);
23	}
24
25	@Override
26	protected PrintWriter createWriter(String outdir) throws IOException {
27		   new File(outdir).mkdirs();
28		    return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir+"\\HTMLReports","emailable-report"+prefix+".html"))));
29	}
30}
31
Full Screen
copy
1package test.reports;
2
3import org.testng.Assert;
4import org.testng.IReporter;
5import org.testng.ITestNGListener;
6import org.testng.TestNG;
7import org.testng.annotations.AfterClass;
8import org.testng.annotations.BeforeClass;
9import org.testng.annotations.DataProvider;
10import org.testng.annotations.Test;
11import org.testng.reporters.EmailableReporter;
12import org.testng.reporters.EmailableReporter2;
13import test.SimpleBaseTest;
14
15import java.io.File;
16import java.lang.reflect.Method;
17import java.security.Permission;
18
19public class EmailableReporterTest extends SimpleBaseTest {
20    private SecurityManager manager;
21
22    @BeforeClass(alwaysRun = true)
23    public void setup() {
24        manager = System.getSecurityManager();
25        System.setSecurityManager(new MySecurityManager(manager));
26    }
27
28    @AfterClass(alwaysRun = true)
29    public void cleanup() {
30        System.setSecurityManager(manager);
31    }
32
33    @Test(dataProvider = "getReporterInstances", priority = 1)
34    public void testReportsNameCustomizationViaRunMethodInvocationAndJVMArguments(IReporter reporter, String jvm) {
35        runTestViaRunMethod(reporter, jvm);
36    }
37
38    @Test(dataProvider = "getReporterInstances", priority = 2)
39    public void testReportsNameCustomizationViaRunMethodInvocation(IReporter reporter) {
40        runTestViaRunMethod(reporter, null /* no jvm arguments */);
41    }
42
43    @Test(dataProvider = "getReporterNames", priority = 3)
44    public void testReportsNameCustomizationViaMainMethodInvocation(String clazzName) {
45        runTestViaMainMethod(clazzName, null /* no jvm arguments */);
46    }
47
48    @Test(dataProvider = "getReporterNames", priority = 4)
49    public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments(String clazzName, String jvm) {
50        runTestViaMainMethod(clazzName, jvm);
51    }
52
53
54    @DataProvider(name = "getReporterInstances")
55    public Object[][] getReporterInstances(Method method) {
56        if (method.getName().toLowerCase().contains("jvmarguments")) {
57            return new Object[][] {
58                {new EmailableReporter(), "emailable.report.name"},
59                {new EmailableReporter2(), "emailable.report2.name"}
60            };
61        }
62        return new Object[][] {
63            {new EmailableReporter()},
64            {new EmailableReporter2()}
65        };
66    }
67
68    @DataProvider(name = "getReporterNames")
69    public Object[][] getReporterNames(Method method) {
70        if (method.getName().toLowerCase().contains("jvmarguments")) {
71            return new Object[][] {
72                {EmailableReporter.class.getName(), "emailable.report.name"},
73                {EmailableReporter2.class.getName(), "emailable.report2.name"}
74            };
75        }
76        return new Object[][] {
77            {EmailableReporter.class.getName()},
78            {EmailableReporter2.class.getName()}
79        };
80    }
81
82    private void runTestViaMainMethod(String clazzName, String jvm) {
83        String name = Long.toString(System.currentTimeMillis());
84        File output = createDirInTempDir(name);
85        String filename = "report" + name + ".html";
86        String[] args = {"-d", output.getAbsolutePath(), "-reporter", clazzName +
87            ":fileName=" + filename, "src/test/resources/1332.xml"};
88        try {
89            if (jvm != null) {
90                System.setProperty(jvm, filename);
91            }
92            TestNG.main(args);
93            if (jvm != null) {
94                //reset the jvm arguments
95                System.setProperty(jvm, "");
96            }
97        } catch (SecurityException t) {
98            //Gobble Security exception
99        }
100        File actual = new File(output.getAbsolutePath(), filename);
101        Assert.assertEquals(actual.exists(), true);
102    }
103
104    private void runTestViaRunMethod(IReporter reporter, String jvm) {
105        String name = Long.toString(System.currentTimeMillis());
106        File output = createDirInTempDir(name);
107        String filename = "report" + name + ".html";
108        if (jvm != null) {
109            System.setProperty(jvm, filename);
110        }
111        TestNG testNG = create();
112        testNG.setOutputDirectory(output.getAbsolutePath());
113        if (reporter instanceof EmailableReporter2) {
114            ((EmailableReporter2) reporter).setFileName(filename);
115        }
116        if (reporter instanceof EmailableReporter) {
117            ((EmailableReporter) reporter).setFileName(filename);
118        }
119        testNG.addListener((ITestNGListener) reporter);
120        testNG.setTestClasses(new Class[] {ReporterSample.class});
121        testNG.run();
122        if (jvm != null) {
123            //reset the jvm argument if it was set
124            System.setProperty(jvm, "");
125        }
126
127        File actual = new File(output.getAbsolutePath(), filename);
128        Assert.assertEquals(actual.exists(), true);
129    }
130
131    public static class MySecurityManager extends SecurityManager {
132
133        private SecurityManager baseSecurityManager;
134
135        MySecurityManager(SecurityManager baseSecurityManager) {
136            this.baseSecurityManager = baseSecurityManager;
137        }
138
139        @Override
140        public void checkPermission(Permission permission) {
141            if (permission.getName().startsWith("exitVM")) {
142                throw new SecurityException("System exit not allowed");
143            }
144            if (baseSecurityManager != null) {
145                baseSecurityManager.checkPermission(permission);
146            }
147        }
148    }
149}
150
Full Screen
copy
1package Utilities;
2
3import java.util.List;
4
5import org.testng.ISuite;
6import org.testng.reporters.EmailableReporter;
7import org.testng.xml.XmlSuite;
8
9public class AfterSuite extends EmailableReporter
10{
11	@Override
12	public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {
13		
14		super.generateReport(xml, suites, outdir);
15		
16		Email e= new Email();
17
18		try {
19
20			e.Emailsend();
21
22		}catch (Exception e1) {
23
24			e1.printStackTrace();
25		}
26
27	}
28}
29
Full Screen
copy
1    attributes.setProperty(XMLReporterConfig.ATTR_NAME, testResult.getMethod().getMethodName());
2
Full Screen
copy
1  @AfterMethod(alwaysRun = true)
2public void setResultTestName(ITestResult result, ITestContext context) {
3    try {
4        BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod().clone();
5
6        String featureName = StringUtils.substringBetween(result.getParameters()[1].toString(), "[", "]");
7        String scenarioName = result.getParameters()[0].toString().replace("\"", "");
8        Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
9        f.setAccessible(true);
10        f.set(baseTestMethod, featureName + " - " + scenarioName);
11
12        f = result.getClass().getDeclaredField("m_method");
13        f.setAccessible(true);
14        f.set(result, baseTestMethod);
15
16    } catch (Exception e) {
17        e.printStackTrace();
18    }
19}
20
Full Screen
copy
1import org.testng.Assert;
2import org.testng.ITest;
3import org.testng.ITestContext;
4import org.testng.ITestResult;
5import org.testng.annotations.AfterMethod;
6import org.testng.annotations.BeforeClass;
7import org.testng.annotations.BeforeMethod;
8import org.testng.annotations.DataProvider;
9import org.testng.annotations.Test;
10
11import java.lang.reflect.Method;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Iterator;
15import java.util.List;
16
17
18public class NewDataProviderTest implements ITest {
19    //The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread
20    private ThreadLocal<String> testName = new ThreadLocal<>();
21
22    /*
23    * TestNG, for some reason, when building different reports, calls getName() on the test while building the report.
24    * 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.
25    * 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.
26    * As the ITest strategy returns the name for the test as the name set by the last run.
27    * */
28    private int emailNameIndex = 0;
29    private int htmlNameIndex = 0;
30    private int xmlNameIndex = 0;
31    private ArrayList<String> allTests = new ArrayList<String>();
32    /*
33    * TestHTMLReporter gets the name by first getting all the names for failed tests and then the names for passing tests
34    * Hence keeping them in 2 separate lists
35    * */
36    private ArrayList<String> passedTests = new ArrayList<String>();
37    private ArrayList<String> failedTests = new ArrayList<String>();
38
39    @BeforeClass(alwaysRun = true)
40    public void initialize() {
41        this.testName.set("");
42    }
43
44    @BeforeMethod(alwaysRun = true)
45    public void setCustomTestcaseName(Method method, Object[] testData) {
46        //Set the default name
47        this.testName.set(method.getName());
48        //Change the test name only if Dataprovider is used
49        //Check if data provider is used in the test
50        if (testData != null && testData.length > 0) {
51            System.out.println("\n\nParameters "+testData[0]+" are passed to the test - "+method.getName());
52            //Taking 1st field in the Dataprovider for updating testname - can be changed as desired maybe using a variable
53            //I'm changing the name only if the Dataprovider field is String
54            if (testData[0] instanceof String) {
55                //Taking 1st field in the Dataprovider for updating testname - can be changed as desired
56                System.out.println("I'm setting custom name to the test as "+method.getName() + "_" + testData[0]);
57                this.testName.set(method.getName() + "_" + testData[0]);
58            }
59
60        }
61        //Add the name to the collection that stores all list names
62        allTests.add(testName.get());
63
64    }
65
66     @AfterMethod (alwaysRun = true)
67     public void setTheTestcaseNameInResult(ITestResult result, Method method) {
68        //Fill the Passed and Failed tests collections
69         try {
70             if(result.getStatus() == ITestResult.SUCCESS) {
71                 System.out.println("Adding "+ result.getTestName() + " to passed tests collection");
72                 passedTests.add(result.getTestName());
73             }
74             if(result.getStatus() == ITestResult.FAILURE) {
75                 System.out.println("Adding " + result.getTestName() + " to FAILURE tests collection");
76                 failedTests.add(result.getTestName());
77             }
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81         // To change display name in HTML report
82         //Only changing the name if the parameter is instance of String
83         if(iTestResult.getParameters().length > 0) {
84             if (iTestResult.getParameters()[0] instanceof String) {
85                 System.out.println("Updating the name as Parameters are passed to the test-"+method.getName());
86                 try {
87                     /* This helps in setting unique name to method for each test instance for a data provider*/
88                     Field resultMethod = TestResult.class.getDeclaredField("m_method");
89                     resultMethod.setAccessible(true);
90                     resultMethod.set(iTestResult, iTestResult.getMethod().clone());
91
92                     Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
93                     methodName.setAccessible(true);
94                     methodName.set(iTestResult.getMethod(), this.getTestName());
95                 } catch (Exception e) {
96                     e.printStackTrace();
97                 }
98                 System.out.println("New Name is - " + iTestResult.getMethod().getMethodName());
99
100             }
101         }
102     }
103
104    @Override
105    public String getTestName() {
106        String name = testName.get();
107        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();// .toString();
108        //This is called
109        if (isCalledFromMethod(stackTrace, "XMLSuiteResultWriter")) {
110            //System.out.println("Got called from XMLSuiteResultWriter");
111            if (allTestNames.size() > 0) {
112                if (xmlNameIndex < allTestNames.size()) {
113                    name = allTestNames.get(xmlNameIndex);
114                } else {
115                    name = allTestNames.get(0);
116                }
117            } else {
118                name = "undefined";
119            }
120            xmlNameIndex++;
121            if (xmlNameIndex >= allTestNames.size()) {
122                xmlNameIndex = 0;
123            }
124            // System.out.println("Got called from XMLSuiteResultWriter returning name - "+name);
125        } else if (isCalledFromMethod(stackTrace, "EmailableReporter")) {
126            if (allTestNames.size() > 0) {
127                if (emailNameIndex < allTestNames.size()) {
128                    name = allTestNames.get(emailNameIndex);
129                } else {
130                    name = allTestNames.get(0);
131                }
132            } else {
133                name = "undefined";
134            }
135            emailNameIndex++;
136            if (emailNameIndex >= allTestNames.size()) {
137                emailNameIndex = 0;
138            }
139            System.out.println("Got called from EmailableReporter returning name -"+name);
140        }
141        if (isCalledFromMethod(stackTrace, "TestHTMLReporter")) {
142            if (allTestNames.size() <= 0) {
143                name = "undefined";
144            } else {
145                if (htmlNameIndex < failedTestNames.size()) {
146                    name = failedTestNames.get(htmlNameIndex);
147                } else {
148                    int htmlPassedIndex = htmlNameIndex - failedTestNames.size();
149                    if (htmlPassedIndex < passedTestNames.size()) {
150                        name = passedTestNames.get(htmlPassedIndex);
151                    } else {
152                        name = "undefined";
153                    }
154                }
155            }
156            htmlNameIndex++;
157            if (htmlNameIndex >= allTestNames.size()) {
158                htmlNameIndex = 0;
159            }
160            System.out.println("Got called from TestHTMLReporter returning name - "+name);
161        }
162        System.out.println("Returning testname as-"+name);
163        return name;
164    }
165
166    private boolean isCalledFromMethod(StackTraceElement[] stackTrace, String checkForMethod) {
167        boolean calledFrom = false;
168        for (StackTraceElement element : stackTrace) {
169            String stack = element.toString();
170            // System.out.println("Rohit the called from value is:"+stack);
171            if (stack.contains(checkForMethod))
172                calledFrom = true;
173        }
174        return calledFrom;
175    }
176
177
178  @Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
179  public void dataProviderTest(String username) {
180      System.out.println("\n\nI'm in dataProviderTest with data-"+username);
181      /* Fail the test if value is L2 - deliberately so that we have failed test in report */ 
182      if(username.contains("L2")) {
183          Assert.fail();
184      }
185
186  }
187
188  @Test(dependsOnMethods = "dataProviderTest", groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
189  public void dataProviderDependentTest(String username) {
190      System.out.println("\n\nI'm in dataProvider DEPENDENT Test with data-"+username);
191
192  }
193
194  //This test consumes data of type list so the name will not be updated in report
195  @Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp2")
196  public void dataListProviderTest(List<String[]> list) {
197      Object[] arr = list.get(0);
198        List<Object> arrList = Arrays.asList(arr);
199        Iterator<Object> iterator = arrList.iterator();
200        while (iterator.hasNext()) {
201
202        String[] data = (String[]) iterator.next();
203        System.out.println("In list test - "+data[0]);
204        }    
205
206  }
207
208  @DataProvider(name="dp1")
209  public Object[][] getDataForTest(ITestContext iTestContext){
210      Object[][] L1 = new Object[][] {
211          {"L1"}, {"L2"}, {"L3"}
212      };
213
214
215      return L1;
216  }
217
218
219  @DataProvider(name="dp2")
220  public Object[][] getDataListForTest(ITestContext iTestContext){
221      List<Object[][]> list = new ArrayList<Object[][]>();
222      Object[][] L1 = new Object[][] {
223          new String [] {"L1", "l1"}, 
224          new String [] {"L1", "l1"}
225      };
226
227     list.add(L1);
228      return new Object[][] { { list } };
229  }
230
231}
232
Full Screen
copy
1public class NinjaTest {
2...
3...
4@AfterMethod (alwaysRun = true)
5public void afterMethod(ITestResult result, Method method) {
6    try {
7        //I have XML test suites organized in directories. 
8        String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
9        String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
10        String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
11        String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
12        String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
13
14        //Total hack to change display name in HTML report  \(^o^)/ 
15        Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
16        methodName.setAccessible(true);
17        methodName.set(result.getMethod(), testMethodName);
18    } catch (Exception e) {
19        // Eh....  ¯\_(ツ)_/¯
20        e.printStackTrace();
21    }
22}
23...
24...
25
Full Screen
copy
1public String getTestName()
2{
3    String name = testName;
4    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();//.toString();
5    if(calledFrom(stackTrace, "XMLSuiteResultWriter"))
6    {
7        name = testNames.size()>0?testNames.get(xmlNameIndex<testNames.size()?xmlNameIndex:0):"undefined";
8        xmlNameIndex++;
9        if(xmlNameIndex>=testNames.size())
10            xmlNameIndex = 0;
11    }
12    else if(calledFrom(stackTrace, "EmailableReporter"))
13    {
14        name = testNames.size()>0?testNames.get(emailNameIndex<testNames.size()?emailNameIndex:0):"undefined";
15        emailNameIndex++;
16        if(emailNameIndex>=testNames.size())
17            emailNameIndex = 0;
18    }
19    if(calledFrom(stackTrace, "TestHTMLReporter"))
20    {
21        if(testNames.size()<0)
22        {
23            name = "undefined";
24        }
25        else
26        {
27            if(htmlNameIndex < testNamesFailed.size())
28            {
29                name = testNamesFailed.get(htmlNameIndex);
30            }
31            else
32            {
33                int htmlPassedIndex = htmlNameIndex - testNamesFailed.size();
34                if(htmlPassedIndex < testNamesPassed.size())
35                {
36                    name = testNamesPassed.get(htmlPassedIndex);
37                }
38                else
39                {
40                    name = "undefined";
41                }
42            }
43        }
44        htmlNameIndex++;
45        if(htmlNameIndex>=testNames.size())
46            htmlNameIndex = 0;
47    }
48    return name;
49}
50
51private boolean calledFrom(StackTraceElement[] stackTrace, String checkForMethod)
52{
53    boolean calledFrom = false;
54    for(StackTraceElement element : stackTrace)
55    {
56        String stack = element.toString();
57        if(stack.contains(checkForMethod))
58            calledFrom = true;
59    }
60    return calledFrom;
61}
62
Full Screen
copy
1//oversimplified for demontration purposes
2public class TestParameters {
3    private String testName = null;
4    private String testDescription = null;
5
6    public TestParameters(String name,
7                          String description) {
8        this.testName = name;
9        this.testDescription = description;
10    }
11
12    public String getTestName() {
13        return testName;
14    }
15    public String getTestDescription() {
16        return testDescription;
17    }
18}
19
20public class SampleTest implements ITest {
21    // Has to be set to prevent NullPointerException from reporters
22    protected String mTestCaseName = "";
23
24    @DataProvider(name="BasicDataProvider")
25    public Object[][] getTestData() {
26        Object[][] data = new Object[][] {
27                { new TestParameters("TestCase1", "Sample test 1")},
28                { new TestParameters("TestCase2", "Sample test 2")},
29                { new TestParameters("TestCase3", "Sample test 3")},
30                { new TestParameters("TestCase4", "Sample test 4")},
31                { new TestParameters("TestCase5", "Sample test 5") }
32        };
33        return data;
34    }
35
36    @BeforeMethod(alwaysRun = true)
37    public void testData(Method method, Object[] testData) {
38        String testCase = "";
39        if (testData != null && testData.length > 0) {
40            TestParameters testParams = null;
41            //Check if test method has actually received required parameters
42            for (Object testParameter : testData) {
43                if (testParameter instanceof TestParameters) {
44                    testParams = (TestParameters)testParameter;
45                    break;
46                }
47            }
48            if (testParams != null) {
49                testCase = testParams.getTestName();
50            }
51        }
52        this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
53    }
54
55    @Override
56    public String getTestName() {
57        return this.mTestCaseName;
58    }
59
60    @Test(dataProvider="BasicDataProvider")
61    public void testSample1(TestParameters testParams){
62        //test code here
63    }
64
65    @Test(dataProvider="BasicDataProvider")
66    public void testSample2(TestParameters testParams){
67        //test code here
68    }
69
70    @Test
71    public void testSample3(){
72        //test code here
73    }
74}
75
Full Screen
copy
1<testng-results skipped="0" failed="0" total="5" passed="5">
2  <suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
3    <test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
4        <test-method 
5            status="PASS" 
6            signature="testSample1(org.example.test.TestParameters)[pri:0, instance:[email protected]]"
7            test-instance-name="testSample1(TestCase5)"
8            name="testSample1" 
9            duration-ms="1014"
10            started-at="<some-time-before>" 
11            data-provider="BasicDataProvider" 
12            finished-at="<some-time-later>" >
13            <!-- excluded for demonstration purposes -->
14        </test-method>
15        <!-- the rest of test results excluded for brevity -->
16    </test>
17  </suite>
18</testng-result>
19
Full Screen
copy
1    attributes.setProperty(XMLReporterConfig.ATTR_NAME, testResult.getMethod().getMethodName());
2
Full Screen
copy
1  @AfterMethod(alwaysRun = true)
2public void setResultTestName(ITestResult result, ITestContext context) {
3    try {
4        BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod().clone();
5
6        String featureName = StringUtils.substringBetween(result.getParameters()[1].toString(), "[", "]");
7        String scenarioName = result.getParameters()[0].toString().replace("\"", "");
8        Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
9        f.setAccessible(true);
10        f.set(baseTestMethod, featureName + " - " + scenarioName);
11
12        f = result.getClass().getDeclaredField("m_method");
13        f.setAccessible(true);
14        f.set(result, baseTestMethod);
15
16    } catch (Exception e) {
17        e.printStackTrace();
18    }
19}
20
Full Screen
copy
1import org.testng.Assert;
2import org.testng.ITest;
3import org.testng.ITestContext;
4import org.testng.ITestResult;
5import org.testng.annotations.AfterMethod;
6import org.testng.annotations.BeforeClass;
7import org.testng.annotations.BeforeMethod;
8import org.testng.annotations.DataProvider;
9import org.testng.annotations.Test;
10
11import java.lang.reflect.Method;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Iterator;
15import java.util.List;
16
17
18public class NewDataProviderTest implements ITest {
19    //The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread
20    private ThreadLocal<String> testName = new ThreadLocal<>();
21
22    /*
23    * TestNG, for some reason, when building different reports, calls getName() on the test while building the report.
24    * 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.
25    * 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.
26    * As the ITest strategy returns the name for the test as the name set by the last run.
27    * */
28    private int emailNameIndex = 0;
29    private int htmlNameIndex = 0;
30    private int xmlNameIndex = 0;
31    private ArrayList<String> allTests = new ArrayList<String>();
32    /*
33    * TestHTMLReporter gets the name by first getting all the names for failed tests and then the names for passing tests
34    * Hence keeping them in 2 separate lists
35    * */
36    private ArrayList<String> passedTests = new ArrayList<String>();
37    private ArrayList<String> failedTests = new ArrayList<String>();
38
39    @BeforeClass(alwaysRun = true)
40    public void initialize() {
41        this.testName.set("");
42    }
43
44    @BeforeMethod(alwaysRun = true)
45    public void setCustomTestcaseName(Method method, Object[] testData) {
46        //Set the default name
47        this.testName.set(method.getName());
48        //Change the test name only if Dataprovider is used
49        //Check if data provider is used in the test
50        if (testData != null && testData.length > 0) {
51            System.out.println("\n\nParameters "+testData[0]+" are passed to the test - "+method.getName());
52            //Taking 1st field in the Dataprovider for updating testname - can be changed as desired maybe using a variable
53            //I'm changing the name only if the Dataprovider field is String
54            if (testData[0] instanceof String) {
55                //Taking 1st field in the Dataprovider for updating testname - can be changed as desired
56                System.out.println("I'm setting custom name to the test as "+method.getName() + "_" + testData[0]);
57                this.testName.set(method.getName() + "_" + testData[0]);
58            }
59
60        }
61        //Add the name to the collection that stores all list names
62        allTests.add(testName.get());
63
64    }
65
66     @AfterMethod (alwaysRun = true)
67     public void setTheTestcaseNameInResult(ITestResult result, Method method) {
68        //Fill the Passed and Failed tests collections
69         try {
70             if(result.getStatus() == ITestResult.SUCCESS) {
71                 System.out.println("Adding "+ result.getTestName() + " to passed tests collection");
72                 passedTests.add(result.getTestName());
73             }
74             if(result.getStatus() == ITestResult.FAILURE) {
75                 System.out.println("Adding " + result.getTestName() + " to FAILURE tests collection");
76                 failedTests.add(result.getTestName());
77             }
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81         // To change display name in HTML report
82         //Only changing the name if the parameter is instance of String
83         if(iTestResult.getParameters().length > 0) {
84             if (iTestResult.getParameters()[0] instanceof String) {
85                 System.out.println("Updating the name as Parameters are passed to the test-"+method.getName());
86                 try {
87                     /* This helps in setting unique name to method for each test instance for a data provider*/
88                     Field resultMethod = TestResult.class.getDeclaredField("m_method");
89                     resultMethod.setAccessible(true);
90                     resultMethod.set(iTestResult, iTestResult.getMethod().clone());
91
92                     Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
93                     methodName.setAccessible(true);
94                     methodName.set(iTestResult.getMethod(), this.getTestName());
95                 } catch (Exception e) {
96                     e.printStackTrace();
97                 }
98                 System.out.println("New Name is - " + iTestResult.getMethod().getMethodName());
99
100             }
101         }
102     }
103
104    @Override
105    public String getTestName() {
106        String name = testName.get();
107        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();// .toString();
108        //This is called
109        if (isCalledFromMethod(stackTrace, "XMLSuiteResultWriter")) {
110            //System.out.println("Got called from XMLSuiteResultWriter");
111            if (allTestNames.size() > 0) {
112                if (xmlNameIndex < allTestNames.size()) {
113                    name = allTestNames.get(xmlNameIndex);
114                } else {
115                    name = allTestNames.get(0);
116                }
117            } else {
118                name = "undefined";
119            }
120            xmlNameIndex++;
121            if (xmlNameIndex >= allTestNames.size()) {
122                xmlNameIndex = 0;
123            }
124            // System.out.println("Got called from XMLSuiteResultWriter returning name - "+name);
125        } else if (isCalledFromMethod(stackTrace, "EmailableReporter")) {
126            if (allTestNames.size() > 0) {
127                if (emailNameIndex < allTestNames.size()) {
128                    name = allTestNames.get(emailNameIndex);
129                } else {
130                    name = allTestNames.get(0);
131                }
132            } else {
133                name = "undefined";
134            }
135            emailNameIndex++;
136            if (emailNameIndex >= allTestNames.size()) {
137                emailNameIndex = 0;
138            }
139            System.out.println("Got called from EmailableReporter returning name -"+name);
140        }
141        if (isCalledFromMethod(stackTrace, "TestHTMLReporter")) {
142            if (allTestNames.size() <= 0) {
143                name = "undefined";
144            } else {
145                if (htmlNameIndex < failedTestNames.size()) {
146                    name = failedTestNames.get(htmlNameIndex);
147                } else {
148                    int htmlPassedIndex = htmlNameIndex - failedTestNames.size();
149                    if (htmlPassedIndex < passedTestNames.size()) {
150                        name = passedTestNames.get(htmlPassedIndex);
151                    } else {
152                        name = "undefined";
153                    }
154                }
155            }
156            htmlNameIndex++;
157            if (htmlNameIndex >= allTestNames.size()) {
158                htmlNameIndex = 0;
159            }
160            System.out.println("Got called from TestHTMLReporter returning name - "+name);
161        }
162        System.out.println("Returning testname as-"+name);
163        return name;
164    }
165
166    private boolean isCalledFromMethod(StackTraceElement[] stackTrace, String checkForMethod) {
167        boolean calledFrom = false;
168        for (StackTraceElement element : stackTrace) {
169            String stack = element.toString();
170            // System.out.println("Rohit the called from value is:"+stack);
171            if (stack.contains(checkForMethod))
172                calledFrom = true;
173        }
174        return calledFrom;
175    }
176
177
178  @Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
179  public void dataProviderTest(String username) {
180      System.out.println("\n\nI'm in dataProviderTest with data-"+username);
181      /* Fail the test if value is L2 - deliberately so that we have failed test in report */ 
182      if(username.contains("L2")) {
183          Assert.fail();
184      }
185
186  }
187
188  @Test(dependsOnMethods = "dataProviderTest", groups= {"L1", "L2", "L3"}, dataProvider = "dp1")
189  public void dataProviderDependentTest(String username) {
190      System.out.println("\n\nI'm in dataProvider DEPENDENT Test with data-"+username);
191
192  }
193
194  //This test consumes data of type list so the name will not be updated in report
195  @Test(groups= {"L1", "L2", "L3"}, dataProvider = "dp2")
196  public void dataListProviderTest(List<String[]> list) {
197      Object[] arr = list.get(0);
198        List<Object> arrList = Arrays.asList(arr);
199        Iterator<Object> iterator = arrList.iterator();
200        while (iterator.hasNext()) {
201
202        String[] data = (String[]) iterator.next();
203        System.out.println("In list test - "+data[0]);
204        }    
205
206  }
207
208  @DataProvider(name="dp1")
209  public Object[][] getDataForTest(ITestContext iTestContext){
210      Object[][] L1 = new Object[][] {
211          {"L1"}, {"L2"}, {"L3"}
212      };
213
214
215      return L1;
216  }
217
218
219  @DataProvider(name="dp2")
220  public Object[][] getDataListForTest(ITestContext iTestContext){
221      List<Object[][]> list = new ArrayList<Object[][]>();
222      Object[][] L1 = new Object[][] {
223          new String [] {"L1", "l1"}, 
224          new String [] {"L1", "l1"}
225      };
226
227     list.add(L1);
228      return new Object[][] { { list } };
229  }
230
231}
232
Full Screen
copy
1public class NinjaTest {
2...
3...
4@AfterMethod (alwaysRun = true)
5public void afterMethod(ITestResult result, Method method) {
6    try {
7        //I have XML test suites organized in directories. 
8        String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
9        String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
10        String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
11        String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
12        String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
13
14        //Total hack to change display name in HTML report  \(^o^)/ 
15        Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
16        methodName.setAccessible(true);
17        methodName.set(result.getMethod(), testMethodName);
18    } catch (Exception e) {
19        // Eh....  ¯\_(ツ)_/¯
20        e.printStackTrace();
21    }
22}
23...
24...
25
Full Screen
copy
1public String getTestName()
2{
3    String name = testName;
4    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();//.toString();
5    if(calledFrom(stackTrace, "XMLSuiteResultWriter"))
6    {
7        name = testNames.size()>0?testNames.get(xmlNameIndex<testNames.size()?xmlNameIndex:0):"undefined";
8        xmlNameIndex++;
9        if(xmlNameIndex>=testNames.size())
10            xmlNameIndex = 0;
11    }
12    else if(calledFrom(stackTrace, "EmailableReporter"))
13    {
14        name = testNames.size()>0?testNames.get(emailNameIndex<testNames.size()?emailNameIndex:0):"undefined";
15        emailNameIndex++;
16        if(emailNameIndex>=testNames.size())
17            emailNameIndex = 0;
18    }
19    if(calledFrom(stackTrace, "TestHTMLReporter"))
20    {
21        if(testNames.size()<0)
22        {
23            name = "undefined";
24        }
25        else
26        {
27            if(htmlNameIndex < testNamesFailed.size())
28            {
29                name = testNamesFailed.get(htmlNameIndex);
30            }
31            else
32            {
33                int htmlPassedIndex = htmlNameIndex - testNamesFailed.size();
34                if(htmlPassedIndex < testNamesPassed.size())
35                {
36                    name = testNamesPassed.get(htmlPassedIndex);
37                }
38                else
39                {
40                    name = "undefined";
41                }
42            }
43        }
44        htmlNameIndex++;
45        if(htmlNameIndex>=testNames.size())
46            htmlNameIndex = 0;
47    }
48    return name;
49}
50
51private boolean calledFrom(StackTraceElement[] stackTrace, String checkForMethod)
52{
53    boolean calledFrom = false;
54    for(StackTraceElement element : stackTrace)
55    {
56        String stack = element.toString();
57        if(stack.contains(checkForMethod))
58            calledFrom = true;
59    }
60    return calledFrom;
61}
62
Full Screen
copy
1//oversimplified for demontration purposes
2public class TestParameters {
3    private String testName = null;
4    private String testDescription = null;
5
6    public TestParameters(String name,
7                          String description) {
8        this.testName = name;
9        this.testDescription = description;
10    }
11
12    public String getTestName() {
13        return testName;
14    }
15    public String getTestDescription() {
16        return testDescription;
17    }
18}
19
20public class SampleTest implements ITest {
21    // Has to be set to prevent NullPointerException from reporters
22    protected String mTestCaseName = "";
23
24    @DataProvider(name="BasicDataProvider")
25    public Object[][] getTestData() {
26        Object[][] data = new Object[][] {
27                { new TestParameters("TestCase1", "Sample test 1")},
28                { new TestParameters("TestCase2", "Sample test 2")},
29                { new TestParameters("TestCase3", "Sample test 3")},
30                { new TestParameters("TestCase4", "Sample test 4")},
31                { new TestParameters("TestCase5", "Sample test 5") }
32        };
33        return data;
34    }
35
36    @BeforeMethod(alwaysRun = true)
37    public void testData(Method method, Object[] testData) {
38        String testCase = "";
39        if (testData != null && testData.length > 0) {
40            TestParameters testParams = null;
41            //Check if test method has actually received required parameters
42            for (Object testParameter : testData) {
43                if (testParameter instanceof TestParameters) {
44                    testParams = (TestParameters)testParameter;
45                    break;
46                }
47            }
48            if (testParams != null) {
49                testCase = testParams.getTestName();
50            }
51        }
52        this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
53    }
54
55    @Override
56    public String getTestName() {
57        return this.mTestCaseName;
58    }
59
60    @Test(dataProvider="BasicDataProvider")
61    public void testSample1(TestParameters testParams){
62        //test code here
63    }
64
65    @Test(dataProvider="BasicDataProvider")
66    public void testSample2(TestParameters testParams){
67        //test code here
68    }
69
70    @Test
71    public void testSample3(){
72        //test code here
73    }
74}
75
Full Screen
copy
1<testng-results skipped="0" failed="0" total="5" passed="5">
2  <suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
3    <test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
4        <test-method 
5            status="PASS" 
6            signature="testSample1(org.example.test.TestParameters)[pri:0, instance:[email protected]]"
7            test-instance-name="testSample1(TestCase5)"
8            name="testSample1" 
9            duration-ms="1014"
10            started-at="<some-time-before>" 
11            data-provider="BasicDataProvider" 
12            finished-at="<some-time-later>" >
13            <!-- excluded for demonstration purposes -->
14        </test-method>
15        <!-- the rest of test results excluded for brevity -->
16    </test>
17  </suite>
18</testng-result>
19
Full Screen

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.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)