Adding Screenshot To Testng
Solution 1:
To have the screenshot embedded in the index.html report I used relative paths as:
System.setProperty("org.uncommons.reportng.escape-output", "false");
Reporter.log(
"<a title= \"title\" href=\"../path/from/target/"+ fileName +"\">"+"<img width=\"418\" height=\"240\" alt=\"alternativeName\" title=\"title\" src=\"../surefire-reports/html/screenShots/"+fileName+"\">
</a>");
In this case, the Screenshot is displayed in the OutputReport not in the main index page with the stacktrace of the fails which is a bit anoying. But at least images and links are working.
I edit myself to add the complete solution, setting the property "org.uncommons.reportng.escape-output" as false we are passing the html code instead of the text.
I recommend to use ReportNG where the screenshots are attached correctly to the test failure with the complete stack trace:
Solution 2:
Ok apparantly I was looking at the wrong file. I was looking at the emailable-report.html while reporter.log sends everything to index.html. In the index.html file everything is working fine using the code in my first post.
Solution 3:
if you want to save your image both as file(jpg/png etc ..) and base64 format try below complete code .
Base64 format is recommended for emailable reports .
Filesrc= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Saving image to current working directory , you can ignore this step if dont want to save file
FileUtils.copyFile(src, newFile("./demotest.png"));
StringfileName= System.getProperty("user.dir") + "/demotest.png";
Reporter.setEscapeHtml(false); //This need to be set as falsebyte[] fileContent = FileUtils.readFileToByteArray(newFile(fileName));
StringencodedString= Base64.getEncoder().encodeToString(fileContent);
Stringpath="<img src=\"data:image/png;base64, " + encodedString + "\" width=\"300\" height=\"350\" />";
Reporter.log(path);
or if you want only Base64 file format then ..
Stringsrc= ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
Stringpath="<img src=\"data:image/png;base64, " + src + "\" width=\"300\" height=\"350\" />";
Reporter.log(path);
See Output:
Post a Comment for "Adding Screenshot To Testng"