How to attach a Screenshot in ReportNG Test

3.1k Views Asked by At

I need some help attaching a screenshot for failure test cases in ReportNG. Can some one explain me how exactly this is done. I'm new to Selenium and Java. I am using Maven as a build tool.

3

There are 3 best solutions below

1
On

Once you have taken a screenshot and stored at some location, you can insert it as a link in the test report as the testng report is a html document.

Reporter.log("<a href=" + URL+ ">click to open screenshot</a>");

URL - location on either local or network drive

1
On

You can use below script and call the method in every class where ever you required.

 public void screenCapture() throws IOException{
          System.setProperty("org.uncommons.reportng.escape-output", "false");
          File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
          String Path = "E:\\Automation\\testproject\\Screenshots\\";
          File screenshotName = new File(Path +driver.getTitle()+".png");
          //Now add screenshot to results by copying the file
          FileUtils.copyFile(scrFile, screenshotName);
          Reporter.log("<br>  <img src='"+screenshotName+"' height='100' width='100' /><br>");
          Reporter.log("<a href="+screenshotName+"></a>");

          {
0
On

@Cagy79 set this system property and you will be able to create link in ReportNG.

System.setProperty("org.uncommons.reportng.escape-output", "false");