Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. public class MyReporterClass implements IReporter {
  2.  
  3. @Override
  4. public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
  5. //Iterating over each suite included in the test
  6. for (ISuite suite : suites) {
  7. //Following code gets the suite name
  8. String suiteName = suite.getName();
  9. //Getting the results for the said suite
  10. Map<String, ISuiteResult> suiteResults = suite.getResults();
  11. for (ISuiteResult sr : suiteResults.values()) {
  12. ITestContext tc = sr.getTestContext();
  13. System.out.println("Passed tests for suite '" + suiteName +
  14. "' is:" + tc.getPassedTests().getAllResults().size());
  15. System.out.println("Failed tests for suite '" + suiteName +
  16. "' is:" + tc.getFailedTests().getAllResults().size());
  17. System.out.println("Skipped tests for suite '" + suiteName +
  18. "' is:" + tc.getSkippedTests().getAllResults().size());
  19. }
  20. }
  21. }
  22. }
  23.  
  24. public class ExtentReportsClass{
  25. public static String getScreenshot(WebDriver driver, String screenshotName) throws Exception {
  26. //below line is just to append the date format with the screenshot name to avoid duplicate names
  27. String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
  28. TakesScreenshot ts = (TakesScreenshot) driver;
  29. File source = ts.getScreenshotAs(OutputType.FILE);
  30. //after execution, you could see a folder "FailedTestsScreenshots" under src folder
  31. String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/"+screenshotName+dateName+".png";
  32. File finalDestination = new File(destination);
  33. FileUtils.copyFile(source, finalDestination);
  34. //Returns the captured file path
  35. return destination;
  36. }
  37. }
  38.  
  39. @AfterMethod
  40. public void getResult(ITestResult result) throws IOException{
  41. if(result.getStatus() == ITestResult.FAILURE){
  42. logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
  43. logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
  44. //To capture screenshot path and store the path of the screenshot in the string "screenshotPath"
  45. String screenshotPath = ExtentReportsClass.getScreenshot(driver, result.getName());
  46. //To add it in the extent report
  47. logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
  48. }else if(result.getStatus() == ITestResult.SKIP){
  49. logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
  50. }
  51.  
  52. <listeners>
  53. <listener class-name="packagename.MyReporterClass" />
  54. </listeners>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement