@Override public void onTestStart(ITestResult result) { logLine(); logger.info( String.format( "Testing going to start for: %s.%s %s", result.getTestClass().getName(), result.getName(), Arrays.toString(result.getParameters()))); NDC.push(result.getName()); }
@Override public void onTestSuccess(ITestResult tr) { super.onTestSuccess(tr); Logger.log("======================================================="); Logger.log("测试方法 【" + tr.getName() + "】 执行成功!"); Logger.log("======================================================="); }
private static String calculateTestMethodName(ITestResult iTestResult) { Object[] parameters = iTestResult.getParameters(); String name = iTestResult.getName(); if (parameters == null || parameters.length == 0) { return name; } StringBuilder builder = new StringBuilder(name).append(" ("); for (int i = 0; i < parameters.length; i++) { Object parameter = parameters[i]; if (parameter == null) { builder.append("null"); } else { try { builder.append(parameter.toString()); } catch (Exception e) { builder.append("Unstringable object"); } } if (i < parameters.length - 1) { builder.append(", "); } } builder.append(")"); return builder.toString(); }
@Override public void onTestFailure(ITestResult tr) { // System.out.print("\033[31m"); // set red font color System.out.println("F"); System.out.println(tr.getTestClass().getName() + "." + tr.getName() + " FAILED"); // System.out.print("\033[0m"); // reset to normal }
@Override public void onTestStart(ITestResult result) { logsResults.clear(); StringBuilder builder = new StringBuilder(); String testName = result.getName().toString(); String className = result.getTestClass().getName().toString(); Method testMethod = result.getMethod().getConstructorOrMethod().getMethod(); builder.append("<table>" + "<h1>Class: <em>" + className + "." + testName + " </em></h1>"); if (testMethod.isAnnotationPresent(RelatedIssue.class)) { String issueID = testMethod.getAnnotation(RelatedIssue.class).issueID(); String jiraUrl = jiraPath + issueID; builder.append( "<tr class=\"step\"><td>Known failure</td><td><h1><em>" + testName + " - " + "<a href=\"" + jiraUrl + "\">" + issueID + "</a> " + testMethod.getAnnotation(RelatedIssue.class).comment() + "</em></h1></td><td> <br/> </td></tr>"); } else { builder.append( "<tr class=\"step\"><td> </td><td><h1><em>" + testName + "</em></h1></td><td> <br/> </td></tr>"); } CommonUtils.appendTextToFile(logPath, builder.toString()); System.out.println(className + " " + testName); }
@Override public void onTestStart(ITestResult tr) { super.onTestStart(tr); logger.info(tr.getName() + " Start"); testResult.setDate(ts.getDate("-")); testResult.setStartTime(ts.getTime(":")); }
@Override public void onTestSkipped(ITestResult tr) { super.onTestSkipped(tr); Logger.log("======================================================="); Logger.log("测试方法 【" + tr.getName() + "】 执行跳过"); Logger.log("======================================================="); }
private void onTestFinished(ITestResult iTestResult, TestResult.ResultType resultType) { Object testId; TestStartEvent startEvent = null; synchronized (lock) { testId = tests.remove(iTestResult); if (testId == null) { // This can happen when a method fails which this method depends on testId = idGenerator.generateId(); Object parentId = testMethodToSuiteMapping.get(iTestResult.getMethod()); startEvent = new TestStartEvent(iTestResult.getStartMillis(), parentId); } } if (startEvent != null) { // Synthesize a start event resultProcessor.started( new DefaultTestMethodDescriptor( testId, iTestResult.getTestClass().getName(), iTestResult.getName()), startEvent); } if (resultType == TestResult.ResultType.FAILURE) { resultProcessor.failure(testId, iTestResult.getThrowable()); } resultProcessor.completed( testId, new TestCompleteEvent(iTestResult.getEndMillis(), resultType)); }
private Map<String, ITestResult> toMap(List<ITestResult> results) { Map<String, ITestResult> result = new HashMap<String, ITestResult>(); for (ITestResult tr : results) { result.put(tr.getName(), tr); } return result; }
@AfterMethod(alwaysRun = true) public void catchExceptions(ITestResult result) throws IOException { String methodname = result.getName(); if (!result.isSuccess()) { WebCommonMethods.screenshot(methodname); } WebCommonMethods.quit(); // Calling function close to quit browser instance }
@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"); } }
private String getMethodContext(ITestResult result) { // String browser = result.getTestContext().getCurrentXmlTest() // .getParameter("browser"); String testClasss = result.getTestClass().getName(); String name = result.getName(); String rs = testClasss + " | " + name; System.out.println("rs--->" + rs); return rs; }
public void onTestStart(ITestResult tr) { System.out.println("TestCase started now====" + tr.toString()); // extent = new // ExtentReports("D:\\Git\\Ideal-Automation\\Ideal_Application\\src\\com\\ideal\\selenium\\util\\1.testreport\\testreport.html"); logger = extent.startTest(tr.getName()); LogStatus status = logger.getRunStatus(); System.out.println(status); }
private void logEndOfTest(ITestResult result, String outcome) { logger.info( String.format( "Testing going to end for: %s.%s(%s) %s", result.getTestClass().getName(), result.getName(), Arrays.toString(result.getParameters()), outcome)); NDC.pop(); logLine(); }
protected static String getMethodName(ITestResult tr) { String method_name = tr.getName(); Object[] params = tr.getParameters(); if (params != null && params.length > 0) { String tmp = null; if (params[0] instanceof Class<?>) tmp = ((Class<?>) params[0]).getSimpleName(); else if (params[0] != null) tmp = params[0].getClass().getSimpleName(); if (tmp != null) method_name = method_name + "-" + tmp; } return method_name; }
private void log(ITestResult tr, String RESULT) { System.out.print( String.format( "\t%s.%s %s (%dms)\n", tr.getTestClass().getName(), tr.getName(), RESULT, tr.getEndMillis() - tr.getStartMillis())); if (++m_count % 40 == 0) { System.out.println(""); } }
@Override public void onTestFailure(ITestResult tr) { super.onTestFailure(tr); Logger.log("======================================================="); Logger.log("测试方法 【" + tr.getName() + "】 执行失败"); if (DriverBase.driver != null) { // 截图 String name = tr.getName() + "_" + String.valueOf(System.currentTimeMillis()); DriverBase.driver.screenShot(name); Logger.log( "请参考:<a href=\"../screenshot/" + name + ".jpg" + "\" style=\"color:red;\">" + name + ".jpg" + "</font></a>"); } Logger.log("失败原因为:" + tr.getThrowable().getMessage()); Logger.log("======================================================="); }
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; }
public void onTestFailure(ITestResult result) { Reporter.setCurrentTestResult(result); try { outputDirectory.mkdirs(); File outFile = File.createTempFile("TEST-" + result.getName(), ".png", outputDirectory); outFile.delete(); selenium.captureScreenshot(outFile.getAbsolutePath()); Reporter.log("<a href='" + outFile.getName() + "'>screenshot</a>"); } catch (Exception e) { e.printStackTrace(); Reporter.log("Couldn't create screenshot"); Reporter.log(e.getMessage()); } Reporter.setCurrentTestResult(null); }
private void write2LogFile(ITestResult iTestResult, File testFolder) { BufferedWriter out = null; try { if (testFolder == null) { LogUtils.log("Can not write to file test folder is null"); return; } String output = SGTestNGReporter.getOutput(); if (StringUtils.isEmpty(output)) { LogUtils.log("nothing to write to log file"); return; } String parameters = TestNGUtils.extractParameters(iTestResult); File testLogFile = new File( testFolder.getAbsolutePath() + "/" + iTestResult.getName() + "(" + parameters + ").log"); if (!testLogFile.createNewFile()) { LogUtils.log( "Failed to create log file [" + testLogFile + "];\n log output: " + Reporter.getOutput()); return; } FileWriter fstream = new FileWriter(testLogFile); out = new BufferedWriter(fstream); out.write(output); } catch (Exception e) { LogUtils.log("Failed to write to log file result - " + iTestResult, e); } finally { SGTestNGReporter.reset(); if (out != null) { try { out.close(); } catch (IOException e) { LogUtils.log("Failed closing stream", e); // ignore } } } }
@AfterMethod public void catchExceptions(ITestResult result) { System.out.println("result" + result); String methodName = result.getName(); System.out.println(methodName); if (!result.isSuccess()) { try { File scrFile = driver.getScreenshotAs(OutputType.FILE); FileUtils.copyFile( scrFile, new File("C:\\MacmillanProjects\\target\\TestArtefacts\\Test_failing-shot.jpg")); } catch (IOException e1) { e1.printStackTrace(); } } }
@Override public void onTestSkipped(ITestResult tr) { super.onTestSkipped(tr); String format = "* " + tr.getName() + " has been Skipped *"; String printString = "\n"; int i = 0; while (i < format.length()) { printString += "*"; i++; } printString += "\n" + format + "\n"; i = 0; while (i < format.length()) { printString += "*"; i++; } printString += "\n"; System.out.println(printString); }
public void onTestStart(ITestResult iTestResult) { TestDescriptorInternal testInternal; Object parentId; synchronized (lock) { testInternal = new DefaultTestMethodDescriptor( idGenerator.generateId(), iTestResult.getTestClass().getName(), iTestResult.getName()); Object oldTestId = tests.put(iTestResult, testInternal.getId()); assert oldTestId == null : "Apparently some other test has started but it hasn't finished. " + "Expect the resultProcessor to break. " + "Don't expect to see this assertion stack trace due to the current architecture"; parentId = testMethodToSuiteMapping.get(iTestResult.getMethod()); assert parentId != null; } resultProcessor.started( testInternal, new TestStartEvent(iTestResult.getStartMillis(), parentId)); }
@Override public void onTestFailure(ITestResult result) { logEndOfTest(result, "FAILED"); if (BaseUITestClass.getDRIVER() != null) { byte[] scrFile = ((TakesScreenshot) BaseUITestClass.getDRIVER()).getScreenshotAs(OutputType.BYTES); try { String filename = OSUtil.getPath( "target", "surefire-reports", "screenshots", String.format( "%s.%s.png", result.getTestClass().getRealClass().getSimpleName(), result.getName())); FileUtils.writeByteArrayToFile(new File(filename), scrFile); } catch (IOException e) { logger.info("Saving screenshot FAILED: " + e.getCause()); } } logger.info(ExceptionUtils.getStackTrace(result.getThrowable())); logLine(); }
@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; }
/** * 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. } }
public void onTestSuccess(ITestResult tr) { logger.log(LogStatus.PASS, tr.getName()); System.out.println("Result success"); }
public void onTestSkipped(ITestResult tr) { logger.log(LogStatus.SKIP, tr.getName()); System.out.println("Result skipped"); }