How to include a crash screenshot in testNG report

I am currently taking screenshots of my test crashes as follows:

@AfterMethod(alwaysRun=true) public void catchExceptions(ITestResult result){ Calendar calendar = Calendar.getInstance(); SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); String methodName = result.getName(); if(!result.isSuccess()){ File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(scrFile, new File((String) PathConverter.convert("failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png"))); } catch (IOException e1) { e1.printStackTrace(); } } } 

Is it possible to include my own screenshots in the TestNG report link or Fig. If so, how?

All I have found is an online framework. But since I already take screenshots, I do not want to use another infrastructure.

+4
source share
1 answer

Yes, you can include a link to your screenshot in the testng report.

You need to call the org.testng.Reporter.log method to write hyperlinks to testng reported either by annotating your test class or the parent of all test classes using @Listeners ({yourListener.class}) or by adding a listener to your testng.xml .

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="default"> <listeners> <listener class-name="ScreenshotListener" /> </listeners> <test name="Test"> <packages> <package name="someTests.*"/> </packages> </test> </suite> 

First you must create the Listener class and add it to testng. You can get detailed information about this at testng.org. Search for a listener.

Once you create this class, you must write a method in it that overrides the ontestfailure method. The code inside this method will be executed whenever testng identifies a failure.

You can call the screen capture method and use Reporter.log to place a hyperlink to this screenshot. You can then find this link in the failed test file details section.

 import java.io.*; import java.util.*; import java.text.*; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.testng.*; public class ScreenshotListener extends TestListenerAdapter { @Override public void onTestFailure(ITestResult result) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); String methodName = result.getName(); if(!result.isSuccess()){ File scrFile = ((TakesScreenshot)SomeStaticWebDriver.driver).getScreenshotAs(OutputType.FILE); try { String reportDirectory = new File(System.getProperty("user.dir")).getAbsolutePath() + "/target/surefire-reports"; File destFile = new File((String) reportDirectory+"/failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png"); FileUtils.copyFile(scrFile, destFile); Reporter.log("<a href='"+ destFile.getAbsolutePath() + "'> <img src='"+ destFile.getAbsolutePath() + "' height='100' width='100'/> </a>"); } catch (IOException e) { e.printStackTrace(); } } } } 
+15
source

Source: https://habr.com/ru/post/918572/


All Articles