Best Testng code snippet using org.testng.Interface ISuite.getResults
Source:MyReporterListener.java  
...144            if (suites.size() > 1) 145            {146                titleRow(suite.getName(), 5);147            }148            Map<String, ISuiteResult> r = suite.getResults();149            for (ISuiteResult r2 : r.values())150            {151                ITestContext testContext = r2.getTestContext();152                String testName = testContext.getName();153                m_testIndex = testIndex;154                resultSummary(suite, testContext.getFailedConfigurations(),155                        testName, "failed", " (configuration methods)");156                resultSummary(suite, testContext.getFailedTests(), testName,157                        "failed", "");158                resultSummary(suite, testContext.getSkippedConfigurations(),159                        testName, "skipped", " (configuration methods)");160                resultSummary(suite, testContext.getSkippedTests(), testName,161                        "skipped", "");162                resultSummary(suite, testContext.getPassedTests(), testName,163                        "passed", "");164                testIndex++;165            }166        }167        m_out.println("</table>");168    }169    170/** Creates a section showing known results for each method */171    172    protected void generateMethodDetailReport(List<ISuite> suites) 173    {174        m_methodIndex = 0;175        for (ISuite suite : suites) {176            Map<String, ISuiteResult> r = suite.getResults();177            for (ISuiteResult r2 : r.values()) {178                ITestContext testContext = r2.getTestContext();179                if (r.values().size() > 0) 180                {181                    m_out.println("<h1><div id='"+testContext.getName()+"'>" + testContext.getName() + "</h1></div>");182                }183                resultDetail(testContext.getFailedConfigurations());184                resultDetail(testContext.getFailedTests());185                resultDetail(testContext.getSkippedConfigurations());186                resultDetail(testContext.getSkippedTests());187                resultDetail(testContext.getPassedTests());188            }189        }190    }191    192    /**193     * @param tests194     */195    private void resultSummary(ISuite suite, IResultMap tests, String testname,196            String style, String details) {197        if (tests.getAllResults().size() > 0) {198            StringBuffer buff = new StringBuffer();199            String lastClassName = "";200            int mq = 0;201            int cq = 0;202            for (ITestNGMethod method : getMethodSet(tests, suite)) {203                m_row += 1;204                m_methodIndex += 1;205                ITestClass testClass = method.getTestClass();206                String className = testClass.getName();207                if (mq == 0) {208                    String id = (m_testIndex == null ? null : "t"209                            + Integer.toString(m_testIndex));210                    titleRow(testname + " — " + style + details, 5, id);211                    m_testIndex = null;212                }213                if (!className.equalsIgnoreCase(lastClassName)) {214                    if (mq > 0) {215                        cq += 1;216                        m_out.print("<tr class=\"" + style217                                + (cq % 2 == 0 ? "even" : "odd") + "\">"218                                + "<td");219                        if (mq > 1) {220                            m_out.print(" rowspan=\"" + mq + "\"");221                        }222                        m_out.println(">" + lastClassName + "</td>" + buff);223                    }224                    mq = 0;225                    buff.setLength(0);226                    lastClassName = className;227                }228                Set<ITestResult> resultSet = tests.getResults(method);229                long end = Long.MIN_VALUE;230                long start = Long.MAX_VALUE;231                for (ITestResult testResult : tests.getResults(method))232                {233                    if (testResult.getEndMillis() > end) 234                    {235                        end = testResult.getEndMillis();236                    }237                    if (testResult.getStartMillis() < start) 238                    {239                        start = testResult.getStartMillis();240                    }241                }242                mq += 1;243                if (mq > 1) 244                {245                    buff.append("<tr class=\"" + style246                            + (cq % 2 == 0 ? "odd" : "even") + "\">");247                }248                String description = method.getDescription();249                String testInstanceName = resultSet250                        .toArray(new ITestResult[] {})[0].getTestName();251                buff.append("<td><a href=\"#m"252                        + m_methodIndex253                        + "\">"254                        + qualifiedName(method)255                        + " "256                        + (description != null && description.length() > 0 ? "(\""257                                + description + "\")"258                                : "")259                        + "</a>"260                        + (null == testInstanceName ? "" : "<br>("261                                + testInstanceName + ")") + "</td>"262                        + "<td class=\"numi\">" + resultSet.size() + "</td>"263                        + "<td>" + start + "</td>" + "<td class=\"numi\">"264                        + (end - start) + "</td>" + "</tr>");265            }266            if (mq > 0) {267                cq += 1;268                m_out.print("<tr class=\"" + style269                        + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");270                if (mq > 1) {271                    m_out.print(" rowspan=\"" + mq + "\"");272                }273                m_out.println(">" + lastClassName + "</td>" + buff);274            }275        }276    }277    278 /** Starts and defines columns result summary table */279    280    private void startResultSummaryTable(String style) 281    {282        tableStart(style, "summary");283        m_out.println("<tr><th>Class</th>"284                + "<th>Method</th><th># of<br/>Scenarios</th><th>Start</th><th>Time<br/>(ms)</th></tr>");285        m_row = 0;286    }287    private String qualifiedName(ITestNGMethod method) 288    {289        StringBuilder addon = new StringBuilder();290        String[] groups = method.getGroups();291        int length = groups.length;292        if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {293            addon.append("(");294            for (int i = 0; i < length; i++) {295                if (i > 0) {296                    addon.append(", ");297                }298                addon.append(groups[i]);299            }300            addon.append(")");301        }302        return "<b>" + method.getMethodName() + "</b> " + addon;303    }304    305    private void resultDetail(IResultMap tests) {306        for (ITestResult result : tests.getAllResults()) {307            ITestNGMethod method = result.getMethod();308            m_methodIndex++;309            //String cname = method.getTestClass().getName();310            //m_out.println("<h2 id=\"m" + m_methodIndex + "\">" + cname + ":"311              //      + method.getMethodName() + "</h2>");312            Set<ITestResult> resultSet = tests.getResults(method);313            generateForResult(result, method, resultSet.size());314            m_out.println("<hr>");315           m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>");316        }317    }318    319    private void generateForResult(ITestResult ans, ITestNGMethod method,320            int resultSetSize) {321        Object[] parameters = ans.getParameters();322        boolean hasParameters = parameters != null && parameters.length > 0;323        if (hasParameters) {324            tableStart("result", null);325            m_out.print("<tr class=\"param\">");326            for (int x = 1; x <= parameters.length; x++) {327                m_out.print("<th>Param." + x + "</th>");328            }329            m_out.println("</tr>");330            m_out.print("<tr class=\"param stripe\">");331            for (Object p : parameters) {332                m_out.println("<td>" + Utils.escapeHtml(Utils.toString(p))333                        + "</td>");334            }335            m_out.println("</tr>");336        }337        List<String> msgs = Reporter.getOutput(ans);338        boolean hasReporterOutput = msgs.size() > 0;339    340        341        Throwable exception = ans.getThrowable();342        boolean hasThrowable = exception != null;343        if (hasReporterOutput || hasThrowable) {344            if (hasParameters) {345                m_out.print("<tr><td");346                if (parameters.length > 1) {347                    m_out.print(" colspan=\"" + parameters.length + "\"");348                }349                m_out.println(">");350            } else {351                m_out.println("<div>");352            }353            if (hasReporterOutput) {354                if (hasThrowable) {355                    //m_out.println("<h3>Test Messages</h3>");356                }357                for (String line : msgs) {358                    m_out.println(line + "<br/>");359                }360            }361            if (hasThrowable) {362                boolean wantsMinimalOutput = ans.getStatus() == ITestResult.SUCCESS;363                if (hasReporterOutput) {364                    m_out.println("<h3>"365                            + (wantsMinimalOutput ? "Expected Exception"366                                    : "Failure") + "</h3>");367                }368                generateExceptionReport(exception, method);369            }370            if (hasParameters) {371                m_out.println("</td></tr>");372            } else {373                m_out.println("</div>");374            }375        }376        if (hasParameters) {377            m_out.println("</table>");378        }379    }380    381    protected void generateExceptionReport(Throwable exception,382            ITestNGMethod method) {383        m_out.print("<div class=\"stacktrace\">");384        385        String str=Utils.stackTrace(exception, true)[0];386        scanner = new Scanner(str);387        String firstLine = scanner.nextLine();388        m_out.println(firstLine);389        m_out.println("</div>");390    }391    /**392     * Since the methods will be sorted chronologically, we want to return the393     * ITestNGMethod from the invoked methods.394     */395    396    private Collection<ITestNGMethod> getMethodSet(IResultMap tests,   ISuite suite) {397        List<IInvokedMethod> r = Lists.newArrayList();398        List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();399        for (IInvokedMethod im : invokedMethods) {400            if (tests.getAllMethods().contains(im.getTestMethod())) {401                r.add(im);402            }403        }404        Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new TestSorter());405        List<ITestNGMethod> result = Lists.newArrayList();406        // Add all the invoked methods407        for (IInvokedMethod m : r) {408            result.add(m.getTestMethod());409        }410        // Add all the methods that weren't invoked (e.g. skipped) that we411        // haven't added yet412        for (ITestNGMethod m : tests.getAllMethods()) {413            if (!result.contains(m)) {414                result.add(m);415            }416        }417        return result;418    }419    420    @SuppressWarnings("unused")421    public void generateSuiteSummaryReport(List<ISuite> suites) {422        tableStart("testOverview", "summary");423        m_out.print("<tr>");   424        tableColumnStart("Test CaseID");425        tableColumnStart("Test Scenarios");426        tableColumnStart("Result");427        tableColumnStart("Start Time-<br/>End Time");428        tableColumnStart("Total<br/>Time");429        tableColumnStart("");430        m_out.println("</tr>");431        NumberFormat formatter = new DecimalFormat("#,##0.0");432        SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yy hh:mm:ss a");433        int qty_tests = 0;434        int qty_pass_m = 0;435        int qty_pass_s = 0;436        int qty_skip = 0;437        int qty_fail = 0;438        int total_pass_s=0;439        int total_fail=0;440        int total_skip=0;441        long time_start = Long.MAX_VALUE;442        long time_end = Long.MIN_VALUE;443        m_testIndex = 1;444        for (ISuite suite : suites)445        {446            if (suites.size() >= 1) 447            {448                titleRow(suite.getName(), 7);449            }450            Map<String, ISuiteResult> tests = suite.getResults();451            for (ISuiteResult r : tests.values()) 452            {453                qty_tests += 1;454                455                456                //Getting Method Name457                ITestContext overview = r.getTestContext();458                459                MethodDetails MD= (MethodDetails) details.get(overview.getName());460                System.out.println("md : "+overview.getName());461                startSummaryRow(MD.getTestCaseID()); 462                 printTestCaseName(overview.getName());463                                464                //get Passed Methods Number...Source:NavigatorPanel.java  
...24    main.pop("img");25    main.pop("a");26    main.pop(D);27    for (ISuite suite : getSuites()) {28      if (suite.getResults().size() == 0) {29        continue;30      }31      String suiteName = "suite-" + suiteToTag(suite);32      XMLStringBuffer header = new XMLStringBuffer(main.getCurrentIndent());33      Map<String, ISuiteResult> results = suite.getResults();34      int failed = 0;35      int skipped = 0;36      int passed = 0;37      for (ISuiteResult result : results.values()) {38        ITestContext context = result.getTestContext();39        failed += context.getFailedTests().size();40        skipped += context.getSkippedTests().size();41        passed += context.getPassedTests().size();42      }43      // Suite name in big font44      header.push(D, C, "suite");45      header.push(D, C, "rounded-window");46      // Extra div so the highlighting logic will only highlight this line and not47      // the entire container48      header.push(D, C, "suite-header light-rounded-window-top");49      header.push("a", "href", "#",50          "panel-name", suiteName,51          C, "navigator-link");52      header.addOptional(S, suite.getName(),53          C, "suite-name border-" + getModel().getStatusForSuite(suite.getName()));54      header.pop("a");55      header.pop(D);56      header.push(D, C, "navigator-suite-content");57      generateInfo(header, suite);58      generateResult(header, failed, skipped, passed, suite, suiteName);59      header.pop("ul");60      header.pop(D); // suite-section-content61      header.pop(D); // suite-header62      header.pop(D); // suite63      header.pop(D); // result-section64      header.pop(D); // navigator-suite-content65      main.addString(header.toXML());66    }67    main.pop(D);68  }69  private void generateResult(XMLStringBuffer header, int failed, int skipped, int passed,70      ISuite suite, String suiteName) {71    //72    // Results73    //74    header.push(D, C, "result-section");75    header.push(D, C, "suite-section-title");76    header.addRequired(S, "Results");77    header.pop(D);78    // Method stats79    int total = failed + skipped + passed;80    String stats = String.format("%s, %s %s %s",81        pluralize(total, "method"),82        maybe(failed, "failed", ", "),83        maybe(skipped, "skipped", ", "),84        maybe(passed, "passed", ""));85    header.push(D, C, "suite-section-content");86    header.push("ul");87    header.push("li");88    header.addOptional(S, stats, C, "method-stats");89    header.pop("li");90    generateMethodList("Failed methods", new ResultsByStatus(suite, "failed", ITestResult.FAILURE),91        suiteName, header);92    generateMethodList("Skipped methods", new ResultsByStatus(suite, "skipped", ITestResult.SKIP),93        suiteName, header);94    generateMethodList("Passed methods", new ResultsByStatus(suite, "passed", ITestResult.SUCCESS),95        suiteName, header);96    }97  private void generateInfo(XMLStringBuffer header, ISuite suite) {98    //99    // Info100    //101    header.push(D, C, "suite-section-title");102    header.addRequired(S, "Info");103    header.pop(D);104    header.push(D, C, "suite-section-content");105    header.push("ul");106    // All the panels107    for (INavigatorPanel panel : m_panels) {108      addLinkTo(header, panel, suite);109    }110    header.pop("ul");111    header.pop(D); // suite-section-content112  }113  private void addLinkTo(XMLStringBuffer header, INavigatorPanel panel, ISuite suite) {114    String text = panel.getNavigatorLink(suite);115    header.push("li");116    header.push("a", "href", "#",117        "panel-name", panel.getPanelName(suite),118        C, "navigator-link ");119    String className = panel.getClassName();120    if (className != null) {121      header.addOptional(S, text, C, className);122    } else {123      header.addOptional(S, text);124    }125    header.pop("a");126    header.pop("li");127  }128  private static String maybe(int count, String s, String sep) {129    return count > 0 ? count + " " + s + sep: "";130  }131  private List<ITestResult> getMethodsByStatus(ISuite suite, int status) {132    List<ITestResult> result = Lists.newArrayList();133    List<ITestResult> testResults = getModel().getTestResults(suite);134    for (ITestResult tr : testResults) {135      if (tr.getStatus() == status) {136        result.add(tr);137      }138    }139    Collections.sort(result, ResultsByClass.METHOD_NAME_COMPARATOR);140    return result;141  }142  private static interface IResultProvider {143    List<ITestResult> getResults();144    String getType();145  }146  private abstract static class BaseResultProvider implements IResultProvider {147    protected ISuite m_suite;148    protected String m_type;149    public BaseResultProvider(ISuite suite, String type) {150      m_suite = suite;151      m_type = type;152    }153    @Override154    public String getType() {155      return m_type;156    }157  }158  private class ResultsByStatus extends BaseResultProvider {159    private final int m_status;160    public ResultsByStatus(ISuite suite, String type, int status) {161      super(suite, type);162      m_status = status;163    }164    @Override165    public List<ITestResult> getResults() {166      return getMethodsByStatus(m_suite, m_status);167    }168  }169  private void generateMethodList(String name, IResultProvider provider,170      String suiteName, XMLStringBuffer main) {171    XMLStringBuffer xsb = new XMLStringBuffer(main.getCurrentIndent());172    String type = provider.getType();173    String image = Model.getImage(type);174    xsb.push("li");175    // The methods themselves176    xsb.addRequired(S, name, C, "method-list-title " + type);177    // The mark up to show the (hide)/(show) links178    xsb.push(S, C, "show-or-hide-methods " + type);179    xsb.addRequired("a", " (hide)", "href", "#", C, "hide-methods " + type + " " + suiteName,180        "panel-name", suiteName);181    xsb.addRequired("a", " (show)", "href", "#",C, "show-methods " + type + " " + suiteName,182        "panel-name", suiteName);183    xsb.pop(S);184    // List of methods185    xsb.push(D, C, "method-list-content " + type + " " + suiteName);186    int count = 0;187    List<ITestResult> testResults = provider.getResults();188    if (testResults != null) {189      Collections.sort(testResults, ResultsByClass.METHOD_NAME_COMPARATOR);190      for (ITestResult tr : testResults) {191        String testName = Model.getTestResultName(tr);192        xsb.push(S);193        xsb.addEmptyElement("img", "src", image, "width", "3%");194        xsb.addRequired("a", testName, "href", "#",195            "hash-for-method", getModel().getTag(tr),196            "panel-name", suiteName,197            "title", tr.getTestClass().getName(),198            C, "method navigator-link");199        xsb.pop(S);200        xsb.addEmptyElement("br");201        count++;...Source:ExtentReporterN.java  
...42		//the reason for boolean option: if you receive results, generate report//if you don't then don't generate4344	45	for(ISuite suite : suites){46		Map<String, ISuiteResult>result = suite.getResults();47		//it obtain the key value it cannot be duplicate , it will map(interface in java) it the location which is48		//the extent report49		//50	51	for(ISuiteResult r : result.values()){ // conditional operator , if is this then the other will execute. 52		ITestContext context =r.getTestContext();53		//continue another loop , what is the context sharing , the value that generate54		buildTestNo(context.getPassedTests(), LogStatus.PASS);55		buildTestNo(context.getFailedTests(), LogStatus.FAIL);56		buildTestNo(context.getSkippedTests(), LogStatus.SKIP);57		//Retrieving status using  result  context58	}59}60extent.flush();//it will take the result add to html code 
...Source:Guru99Reporter.java  
...17            String outputDirectory) {18        // Second parameter of this method ISuite will contain all the suite executed.19        for (ISuite iSuite : arg1) {20         //Get a map of result of a single suite at a time21            Map<String,ISuiteResult> results =    iSuite.getResults();22         //Get the key of the result map23            Set<String> keys = results.keySet();24        //Go to each map value one by one25            for (String key : keys) {26             //The Context object of current result27            ITestContext context = results.get(key).getTestContext();28            //Print Suite detail in Console29             System.out.println("Suite Name->"+context.getName()30                    + "::Report output Ditectory->"+context.getOutputDirectory()31                     +"::Suite Name->"+ context.getSuite().getName()32                     +"::Start Date Time for execution->"+context.getStartDate()33                     +"::End Date Time for execution->"+context.getEndDate());34            35             //Get Map for only failed test cases...Source:ExtentReporterNG.java  
...20			String outputDirectory) {21		extent = new ExtentReports(outputDirectory + File.separator22				+ "Extent.html", true);23		for (ISuite suite : suites) {24			Map<String, ISuiteResult> result = suite.getResults();25			for (ISuiteResult r : result.values()) {26				ITestContext context = r.getTestContext();27				buildTestNodes(context.getPassedTests(), LogStatus.PASS);28				buildTestNodes(context.getFailedTests(), LogStatus.FAIL);29				buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);30			}31		}32		extent.flush();33		extent.close();34	}35	private void buildTestNodes(IResultMap tests, LogStatus status) {36		ExtentTest test;37		if (tests.size() > 0) {38			for (ITestResult result : tests.getAllResults()) {...TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
