protected void addTest(Class<?> clazz, ITestResult result) { try { TestCase test_case = new TestCase( result.getStatus(), clazz.getName(), getMethodName(result), result.getStartMillis(), result.getEndMillis()); switch (result.getStatus()) { case ITestResult.FAILURE: case ITestResult.SKIP: Throwable ex = result.getThrowable(); if (ex != null) { String failure_type = ex.getClass().getName(); String failure_msg = ex.getMessage(); String stack_trace = printException(ex); test_case.setFailure(failure_type, failure_msg, stack_trace); } else test_case.setFailure("exception", "SKIPPED", null); break; } synchronized ( this) { // handle concurrent access by different threads, if test methods are run in // parallel DataOutputStream output = tests.get(clazz); test_case.writeTo(output); } } catch (Exception e) { error(e.toString()); } }
/** * After invocation. * * @param method the method * @param result the result * @see org.testng.IInvokedMethodListener#afterInvocation(org.testng.IInvokedMethod, * org.testng.ITestResult) */ @Override public final void afterInvocation(final IInvokedMethod method, final ITestResult result) { if (method.isTestMethod()) { if (result.getStatus() == ITestResult.SKIP) { endTestReporting("skipped"); } else if (result.getStatus() == ITestResult.FAILURE) { endTestReporting("failed"); } else if (result.getStatus() == ITestResult.SUCCESS) { endTestReporting("passed"); } } }
public void takeScreenShotOfPageOnFailure(ITestResult testResult) throws IOException { if (testResult.getStatus() == ITestResult.FAILURE) { System.out.println("testResult.getStatus = " + testResult.getStatus() + " -> failure"); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String randomUUID = UUID.randomUUID().toString(); String randomHEX = Long.toHexString(Double.doubleToLongBits(Math.random())); FileUtils.copyFile( scrFile, new File( "/home/dima/Downloads/-autotest-data/" + driver.getTitle() + randomHEX + ".jpg")); } }
@AfterMethod public void printError(ITestResult result) { if (result.getStatus() == ITestResult.FAILURE) { String clsName = result.getTestClass().getName(); clsName = clsName.substring(clsName.lastIndexOf(".") + 1); System.out.println("Test " + clsName + "." + result.getName() + " FAILED"); } }
protected String getSeleniumLogIdentification(ITestResult result) { final String failure = TestInfo.STATUSES.get(result.getStatus()).toUpperCase(); final String started = TestInfo.STATUSES.get(ITestResult.STARTED).toUpperCase(); String testDescription = TestLoggingUtils.getTestDescription(result); testDescription = testDescription.replaceFirst(failure, started); testDescription = testDescription.replaceFirst("\\[[^\\]]+\\] ", ""); return testDescription; }
private void generateForResult(ITestResult ans, ITestNGMethod method, int resultSetSize) { Object[] parameters = ans.getParameters(); boolean hasParameters = parameters != null && parameters.length > 0; if (hasParameters) { tableStart("result", null); m_out.print("<tr class=\"param\">"); for (int x = 1; x <= parameters.length; x++) { m_out.print("<th>Parameter #" + x + "</th>"); } m_out.println("</tr>"); m_out.print("<tr class=\"param stripe\">"); for (Object p : parameters) { m_out.println("<td>" + Utils.escapeHtml(p.toString()) + "</td>"); } m_out.println("</tr>"); } List<String> msgs = Reporter.getOutput(ans); boolean hasReporterOutput = msgs.size() > 0; Throwable exception = ans.getThrowable(); boolean hasThrowable = exception != null; if (hasReporterOutput || hasThrowable) { if (hasParameters) { m_out.print("<tr><td"); if (parameters.length > 1) { m_out.print(" colspan=\"" + parameters.length + "\""); } m_out.println(">"); } else { m_out.println("<div>"); } if (hasReporterOutput) { if (hasThrowable) { m_out.println("<h3>Test Messages</h3>"); } for (String line : msgs) { m_out.println(line + "<br/>"); } } if (hasThrowable) { boolean wantsMinimalOutput = ans.getStatus() == ITestResult.SUCCESS; if (hasReporterOutput) { m_out.println("<h3>" + (wantsMinimalOutput ? "Expected Exception" : "Failure") + "</h3>"); } generateExceptionReport(exception, method); } if (hasParameters) { m_out.println("</td></tr>"); } else { m_out.println("</div>"); } } if (hasParameters) { m_out.println("</table>"); } }
public boolean retry(ITestResult result) { // TODO Auto-generated method stub if (retryCount < maxRetryCount) { System.out.println( "Retrying test with " + result.getName() + " with status " + getResultStatusName(result.getStatus())); retryCount++; return true; } return false; }
@AfterMethod(alwaysRun = true) public void addFailureToHttpLog(ITestResult result) { if (httpLog != null && result.getStatus() == ITestResult.FAILURE) { // Add the failure details after the HTTP trace so it's clear what test it belongs to. httpLog.println(">>> [FAILURE] Test: " + result.getName()); Throwable thrown = result.getThrowable(); if (thrown != null) { httpLog.append(thrown.getLocalizedMessage()); httpLog.println(); } httpLog.println(); } }
// Below method returns 'true' if the test method has to be retried else 'false' // and it takes the 'Result' as parameter of the test method that just ran public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { System.out.println( "Retrying test " + result.getName() + " with status " + getResultStatusName(result.getStatus()) + " for the " + (retryCount + 1) + " time(s)."); retryCount++; return true; } return false; }
private void printTestResults(ITestResult result) { String status = null; switch (result.getStatus()) { case ITestResult.SUCCESS: status = "PASS"; break; case ITestResult.FAILURE: status = "FAIL"; break; case ITestResult.SKIP: status = "SKIP"; break; } System.out.println("Test result:" + status); }
@AfterMethod public void tearDown(ITestResult result) { if (result.getStatus() == ITestResult.FAILURE) { // String screenshot_path = Utility.captureScreenshot(driver, result.getName()); File scrfile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String screenshot_path = "C:\\Storage\\Report\\" + scrfile.getName(); File tmp = new File(screenshot_path); try { FileUtils.copyFile(scrfile, tmp); } catch (Exception ex) { System.out.println("Exception::"); } System.out.println("Path:" + screenshot_path); String image = logger.addScreenCapture(screenshot_path); logger.log(LogStatus.FAIL, "Title verification", image); } report.endTest(logger); report.flush(); }
private void printTestResults(ITestResult result) { Reporter.log("TestName = " + method, true); Reporter.log("Test Method resides in " + result.getTestClass().getName(), true); if (result.getParameters().length != 0) { String params = null; for (Object parameter : result.getParameters()) { params += parameter.toString() + ","; } Reporter.log("Test Method had the following parameters : " + params, true); } String status = null; switch (result.getStatus()) { case ITestResult.SUCCESS: status = "Pass"; break; case ITestResult.FAILURE: status = "Failed"; break; case ITestResult.SKIP: status = "Skipped"; } Reporter.log("Test Status: " + status, true); }
@Override public boolean retry(ITestResult tr) { if (tr.getAttributeNames().contains("retry") == false) { if (retryCount < retryMaxCount) { logger.info( "Retrying " + tr.getName() + " with status " + tr.getStatus() + " for the " + (retryCount + 1) + " of " + retryMaxCount + " times."); retryCount++; return true; } } logger.debug("Skipping retry!"); return false; }
@AfterMethod(alwaysRun = true) public void printDocumentOnFailure(ITestResult result) { if (result.getStatus() == ITestResult.FAILURE) { System.err.println(generatedDocument); } }
/** * This methods logs test statistics after every test method. * * @param context * @param result */ public void testStatistics(ITestContext context, ITestResult result) { logger.info(result.getName() + " " + result.getStatus()); total_test_in_current_suite = context.getAllTestMethods().length; skip_test = context.getSkippedTests().size(); /* * int passed_test = context.getPassedTests().size(); int failed_test = * context.getFailedTests().size(); */ int testResult = result.getStatus(); switch (testResult) { case 1: // check for failedRetry = true. It means last time this method got // failed and this time got passed so lets modify passed and failed // count if (failedRetry) { passed_test++; failed_test--; failedRetry = false; } passed_test++; break; case 2: // if failedRetry is false ie. this is the result of fresh test // method and not previously failed test method. if (!failedRetry) { failed_test++; } failedRetry = true; failedRetryTimes++; // it will have 1 and 2 corresponding to retry // times if (failedRetryTimes == 3) { failedRetry = false; failedRetryTimes = 0; } break; case 3: skip_test++; break; default: logger.info("Unexpected Test Result"); } int net_test = total_test_in_current_suite - (passed_test + failed_test + skip_test); int methods_executed = passed_test + failed_test; logger.info("############################################"); logger.info("Number of Test Methods Executed :: " + methods_executed); logger.info("Number of Passed Tests :: " + passed_test); logger.info("Number of Failed Tests :: " + failed_test); logger.info("Number of Skipped Tests :: " + skip_test); logger.info( "Execution time spent so far :: " + timeSpentSoFar(start_time, System.currentTimeMillis())); logger.info("############################################"); logger.info("Test Method yet to run :: " + net_test); int time_remaining = net_test * 5; logger.info("Expected time to finish :: " + time_remaining + " mins"); float failed_ratio = checkFailedThreshold(failed_test, total_test_in_current_suite); float failed_skipped_ratio = checkFailedThreshold((failed_test + skip_test), total_test_in_current_suite); logger.info("% of Failed Test Methods " + failed_ratio); logger.info("% of Failed and Skipped Test Methods " + failed_skipped_ratio); if (failed_ratio >= 5.5) { System.err.println( "Stopping the Test Execution as % of Failed Tests has become more than threshold"); System.exit(0); // TO DO: Stop the selenium server. In case TestNG is running test // methods/classes/tests in parallel, then we have to ensure that // all threads all closed and system exist // gracefully. } }