@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 onTestFailure(ITestResult result) { ClassInfo classInfo = getClassInfo(result); if (classInfo == null) return; classInfo.failed(result.getStartMillis(), result.getEndMillis()); maybeReport(classInfo); }
@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 }
private void write2ErrorTxtFile(ITestResult iTestResult, File testFolder) { if (testFolder == null) { LogUtils.log("Can not write error.txt - test folder is null"); return; } //noinspection ThrowableResultOfMethodCallIgnored if (iTestResult.getThrowable() == null) { LogUtils.log("nothing to write to error.txt - throwable is null"); return; } // trim if too long //noinspection ThrowableResultOfMethodCallIgnored String errorMsg = iTestResult.getThrowable().toString(); if (errorMsg.length() > 120) { errorMsg = errorMsg.substring(0, 120 - 3) + "..."; } File errorTxtFile = new File(testFolder.getAbsolutePath(), "error.txt"); PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter(new FileWriter(errorTxtFile, true))); out.println(errorMsg); } catch (IOException ioe) { LogUtils.log("Failed to write contents into error.txt file", ioe); } finally { if (out != null) { out.close(); } } }
/** Compare a list of ITestResult with a list of String method names, */ public static void assertTestResultsEqual(List<ITestResult> results, List<String> methods) { List<String> resultMethods = Lists.newArrayList(); for (ITestResult r : results) { resultMethods.add(r.getMethod().getMethodName()); } Assert.assertEquals(resultMethods, methods); }
public synchronized void onTestFailure(ITestResult arg0) { System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " failed."); if (arg0.getThrowable() != null) log.error("Test failed " + getTestDesc(arg0), arg0.getThrowable()); failed.incrementAndGet(); printStatus(); }
@Override public void onTestFailure(ITestResult iTestResult) { if (suiteName.toLowerCase().contains("webui")) { if (testInvocationCounter < maxCount) { testInvocationCounter++; iTestResult.setAttribute("retry", true); } else { LogUtils.log("Number of retries expired."); iTestResult.setStatus(ITestResult.FAILURE); // reset count testInvocationCounter = 1; testMethodName = TestNGUtils.constructTestMethodName(iTestResult); LogUtils.log("Test Failed: " + testMethodName, iTestResult.getThrowable()); File testFolder = DumpUtils.createTestFolder(testMethodName, suiteName); write2LogFile(iTestResult, testFolder); write2ErrorTxtFile(iTestResult, testFolder); } } else { testMethodName = TestNGUtils.constructTestMethodName(iTestResult); LogUtils.log("Test Failed: " + testMethodName, iTestResult.getThrowable()); File testFolder = DumpUtils.createTestFolder(testMethodName, suiteName); write2LogFile(iTestResult, testFolder); write2ErrorTxtFile(iTestResult, testFolder); } super.onTestFailure(iTestResult); }
private void recordResult(ITestResult result, ResultType type, Throwable failure) { // Restore the original stdout/stderr. System.setOut(originalOut); System.setErr(originalErr); // Get the stdout/stderr written during the test as strings. stdOutStream.flush(); stdErrStream.flush(); String stdOut = streamToString(rawStdOutBytes); String stdErr = streamToString(rawStdErrBytes); String className = result.getTestClass().getName(); String methodName = calculateTestMethodName(result); // use exception from suite, if available if (suiteFailureListener.getThrowable() != null) { failure = suiteFailureListener.getThrowable(); stdOut = suiteFailureListener.getStdOut(); stdErr = suiteFailureListener.getStdErr(); } long runTimeMillis = result.getEndMillis() - result.getStartMillis(); results.add( new TestResult(className, methodName, runTimeMillis, type, failure, stdOut, stdErr)); }
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 protected void onFailure(ITestResult result) { super.onFailure(result); List<String> issueList = new LinkedList<String>(); // IssueTracking issueTracking = // result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(IssueTracking.class); IssueTracking issueTracking = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(IssueTracking.class); if (issueTracking != null) { issueList.addAll(asList(issueTracking.value())); } issueTracking = (IssueTracking) result.getMethod().getRealClass().getAnnotation(IssueTracking.class); if (issueTracking != null) { issueList.addAll(asList(issueTracking.value())); } if (!issueList.isEmpty()) { String issues = StringUtils.join(issueList, "\n"); String filenameIdentification = getFilenameIdentification(result); File issueTrackingOutputFile = new File(failuresOutputDir, filenameIdentification + "/issues.txt"); try { FileUtils.writeStringToFile(issueTrackingOutputFile, issues); } catch (IOException e) { throw new RuntimeException(e); } } }
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()); } }
protected void setupStreams(ITestResult result, boolean printMethodName) { String test_name = result.getTestClass().getName(); File dir = new File(output_dir + File.separator + test_name); if (!dir.exists()) dir.mkdirs(); File _tests = new File(dir, TESTS), _stdout = new File(dir, STDOUT), _stderr = new File(dir, STDERR); try { Class<?> clazz = result.getTestClass().getRealClass(); if (!tests.containsKey(clazz)) { DataOutputStream output = new DataOutputStream(new FileOutputStream(_tests, true)); DataOutputStream tmp = tests.putIfAbsent(clazz, output); if (tmp != null) { Util.close(output); output = tmp; } } if (stdout.get() == null) stdout.set(new PrintStream(new FileOutputStream(_stdout, true))); if (stderr.get() == null) stderr.set(new PrintStream(new FileOutputStream(_stderr, true))); if (printMethodName) stdout.get().println("\n\n------------- " + getMethodName(result) + " -----------"); } catch (IOException e) { error(e.toString()); } }
@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"); } }
public synchronized void onTestSkipped(ITestResult arg0) { System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " skipped."); log.info(" Test " + getTestDesc(arg0) + " skipped."); if (arg0.getThrowable() != null) log.error("Test skipped : " + arg0.getThrowable(), arg0.getThrowable()); skipped.incrementAndGet(); printStatus(); }
@Override public void afterInvocation(IInvokedMethod method, ITestResult testResult) { if (!method.isTestMethod()) return; if (testResult.getThrowable() != null) log.errorf( testResult.getThrowable(), "Method %s threw an exception", getTestDesc(testResult)); }
@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 }
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; }
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 afterInvocation(IInvokedMethod method, ITestResult result) { resultT = result; String textMsg = "Completed executing " + returnMethodName(method.getTestMethod()); Reporter.log(textMsg, true); Reporter.setCurrentTestResult(result); if (method.isTestMethod()) { List<Throwable> verificationFailures = TestBase.getVerificationFailures(); // if there are verification failures... if (verificationFailures.size() > 0) { // set the test to failed result.setStatus(ITestResult.FAILURE); // if there is an assertion failure add it to verificationFailures if (result.getThrowable() != null) { verificationFailures.add(result.getThrowable()); } int size = verificationFailures.size(); // if there's only one failure just set that if (size == 1) { result.setThrowable(verificationFailures.get(0)); } else { // create a failure message with all failures and stack traces (except last failure) StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):\n\n"); for (int i = 0; i < size - 1; i++) { failureMessage .append("Failure ") .append(i + 1) .append(" of ") .append(size) .append(":\n"); Throwable t = verificationFailures.get(i); String fullStackTrace = Utils.stackTrace(t, false)[1]; failureMessage.append(fullStackTrace).append("\n\n"); } // final failure Throwable last = verificationFailures.get(size - 1); failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":\n"); failureMessage.append(last.toString()); // set merged throwable Throwable merged = new Throwable(failureMessage.toString()); merged.setStackTrace(last.getStackTrace()); result.setThrowable(merged); } } } }
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 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>"); } }
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; }
@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()); }
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(); }
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")); } }
@Override public void onConfigurationSuccess(ITestResult iTestResult) { super.onConfigurationSuccess(iTestResult); String testName = iTestResult.getTestClass().getName(); String configurationName = iTestResult.getMethod().toString().split("\\(|\\)")[0]; if (isAfter(iTestResult) && !enableLogstash) { DumpUtils.copyBeforeConfigurationsLogToTestDir(testName, suiteName); testName = testMethodName; } if (suiteName == null) { // this is in case the suite has a @BeforeSuite method. which is invoked before // the onStart is. suiteName = System.getProperty("iTests.suiteName", "sgtest"); } LogUtils.log("Configuration Succeeded: " + configurationName); if (enableLogstash && iTestResult.getMethod().isBeforeClassConfiguration()) { initLogstash2(iTestResult); } String newmanTestFolder = System.getProperty("newman.test.path"); if (newmanTestFolder != null) { File testFolder = new File(newmanTestFolder); ZipUtils.unzipArchive(testFolder); try { copyAllFilesToLogDir(testFolder, testFolder); } catch (IOException e) { LogUtils.log("Failed to copy all log files - caught " + e, e); } } else { ZipUtils.unzipArchive(testMethodName, suiteName); } if (enableLogstash && isAfter(iTestResult) && !iTestResult.getMethod().isAfterClassConfiguration() && !iTestResult.getMethod().isAfterSuiteConfiguration()) { testName = testMethodName; } write2LogFile(iTestResult, DumpUtils.createTestFolder(testName, suiteName)); if (isAfter(iTestResult)) { if (enableLogstash) { if (process != null) { killLogstashAgent(1, logstashLogPath); } if (process2 != null && iTestResult.getMethod().isAfterClassConfiguration()) { killLogstashAgent(2, logstashLogPath2); } } } }
/** * 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"); } } }
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(""); } }
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(); } }