3

-Failure Screenshot are visible in Extent_Reports on my local machine. But not able to view the failure screenshot in Extent_Reports on other Computer/Machine.

-When i trigger build from Jenkins, After build successful, Sending email to:Recipient List

To Capture Screenshot

    public String captureScreen(String fileName) {
    if(fileName =="") {
        fileName="Screenshot";  }

    File destFile=null;
    Calendar calendar =Calendar.getInstance() ;
    SimpleDateFormat formater= new SimpleDateFormat("dd_MM_yyy_hh_mm_ss");
    File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    try {

         String reportDirectory = "/src/main/java/com/test/automation/Demo/screenshot/";
         //String reportDirectory= new File(System.getProperty("user.dir")).getAbsolutePath()+"./src/main/java/com/test/automation/Demo/screenshot/";
         destFile= new File((String)reportDirectory + fileName +"-" + formater.format(calendar.getTime())+ ".png");
         FileUtils.copyFile(srcFile,destFile );
         //This will help us to link screen shot in Extent report
         Reporter.log("<a href='"+destFile+ "'><img src='" +destFile+"' height='100' width='100'/></a>");
         //Reporter.log("<a href='"+destFile.getAbsolutePath()+ "'><img src='" +destFile.getAbsolutePath()+"' height='100' width='100'/></a>");
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    return destFile.toString();
}

For generating Extent reports with screenshots for Failure test cases

 public void getresult(ITestResult result) {

if(result.getStatus()==ITestResult.FAILURE) 
{
  test.log(LogStatus.ERROR, result.getName()+" Test case FAILED due to below issues: "+result.getThrowable());
  String screen = captureScreen("");
  test.log(LogStatus.FAIL," Failure Screenshot : "+ test.addScreenCapture(screen));
    }}

3 Answers3

2

If You're using remoteWebDriver than it must be augmented before you can use the screenshot capability. Did You try to

WebDriver driver = new RemoteWebDriver();
driver = new Augmenter().augment(driver);

// or for mobile driver

androidDriver.setFileDetector(new LocalFileDetector()); 
//this is needed when using remoteDriver

Here is how I take screenshot for ExtentReport

File scrFile = driver.getScreenshotAs(OutputType.FILE);

String dest = System.getProperty("user.dir") + "/resources/screenshots/" + dataMethod.getAndroidDriver().getSessionId() + ".png";

File destination = new File(dest);
try {
    FileUtils.copyFile(scrFile, destination);

    // this is just utility which takes screenshot and copy it to desired destination 

     dataMethod.setScreenshotPath(destination.getAbsolutePath());
} catch (IOException e) {
     e.printStackTrace();
}

And on code failure:

@Override
public synchronized void onTestFailure(ITestResult result) {
    setTestEndTime(result);

    ExtentTest extentTest = methodData.getExtentTest();           
    extentTest.addScreenCaptureFromPath(methodData.getScreenshotPath());
}

Hope this will help.

Kovacic
  • 1,473
  • 6
  • 21
2

I didn't used Extent reports, i have my own implementation for reports. But i am expecting is there is issue with src

<img src='" +destFile+"' height='100' width='100'/></a>");

Here, destFile brings location of image or screenshot which is related to your machine. the same should not be works for others. We have to use relative path, see this

https://www.w3schools.com/html/html_filepaths.asp

And also make sure that when sharing reports, it should contains all requires files and folders also.

murali selenium
  • 3,847
  • 2
  • 11
  • 20
1

Normally, the issue happens as the local files are not allowed to be loaded. So even we put relative or absolute path, that seems not work for many cases. So I try to take base64screenshot instead, and it displays quite good in Extent Report. To have the screenshot in folder report, just need to take screenshot as usual.

public static String getBase64Screenshot(WebDriver driver, String screenshotName) throws IOException {
    String encodedBase64 = null;
    FileInputStream fileInputStream = null;
    TakesScreenshot screenshot = (TakesScreenshot) driver;
    File source = screenshot.getScreenshotAs(OutputType.FILE);
    String destination = windowsPath + "\\FailedTestsScreenshots\\"+screenshotName+timeStamp+".png";
    File finalDestination = new File(destination);
    FileUtils.copyFile(source, finalDestination);

    try {
        fileInputStream =new FileInputStream(finalDestination);
        byte[] bytes =new byte[(int)finalDestination.length()];
        fileInputStream.read(bytes);
        encodedBase64 = new String(Base64.encodeBase64(bytes));
    }catch (FileNotFoundException e){
        e.printStackTrace();
    }

    return encodedBase64;
}

Call it in failure cases:

public synchronized void onTestFailure(ITestResult result) {
    System.out.println("==="+methodDes + "=== failed!");
    try {
        WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver");
        String base64Screenshot = ExtentManager.getBase64Screenshot(driver, result.getName());
        MediaEntityModelProvider mediaModel = MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build();
        test.get().fail("image:", mediaModel);
    } catch (IOException e) {
        e.printStackTrace();
    }
    test.get().fail(result.getThrowable().getMessage());
}
Jenny
  • 116
  • 1
  • 4
  • I am facing the same problem...If I check the extent report html source, then images are not loaded with error: "Not allowed to load local resource: file:///C:/Program%20Files/Jenkins/workspace/XXX_Automation/Reports/addTest.png". I have tried your suggestion to take base64screenshot instead but in vain. . It's not the content security policy thing. Please note that I have checked the path- It's correct. Suggest me! – Atul KS Jul 23 '19 at 13:07