Wednesday, 16 December 2015

Screenshots For Selenium Reports In Jenkins and Remote Systems


Its well known that we can capture screen during test execution of selenium. It can be done during any moment of script execution or can be configured for failure screens using test frameworks like TestNG. These screen captures can also be made available in our automation reports. In case of TestNG, we can use the 'Reporter.log' function to add the screen to the TestNG report.

1
2
3
String screenshotName = getClass().getSimpleName() + "_" + testResult.getName() + "_" + getCurrentDateTimeStamp() + ".png";
File scrFile = ((TakesScreenshot)driver()).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./build/reports/tests/screenshots/"+screenshotName)); 

Here we can see that the screen capture gets stored at a folder called 'screenshots' which in turn is located from the root folder './build/reports/tests/'

The above code will work fine as long as your scripts are running from your local machines. Now comes the tricky part.  What is we need to run these automated scripts from a remote machine or in CI tools like Jenkins? We cannot always assure the folder structure in the remote systems. It may happen that Jenkins or the Selenium Grid may insert a specific folder and thus while execution the report path may get different. So the screenshot file will not get rendered in our TestNG report.

A simple but effective solution to tackle such a scenario is to save your screenshots in BASE64 format instead of png files (by default selenium saves screen captured as '*.png' file). Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. So you get to save your screen as a 'Text/String' format instead of a file. Now you can call this string in your report to get the very same image displayed in your report. Since it doesn't need to be stored in any folders unlike images, we can be rest assured that the report where ever is being populated will have the screenshots included and viewed.

This can be done as follows:
  

1
2
3
String screenshotBase64 = ((TakesScreenshot)driver()).getScreenshotAs(OutputType.BASE64);
screenShotList.add(screenshotBase64);
setMapList("SCREEN_NAME", screenShotList);

Note the usage of 'OutputType.BASE64' in the above code snippet. This save the screen capture as a string to the variable 'screenshotBase64'. Now this can be added to a Java list and can be called during report generation.


1
test.log(LogStatus.INFO, "Snapshot below: " + test.addScreenCapture("data:image/jpeg;base64, "+getMapList("SCREEN_NAME").get(count)));

The above java code gets the screen captured as string from the List and adds the same to the report. Note that there is no need to mention the path of the screen as it is stored as a base64 type string. This helps to display the screen captures as is in automation reports.

Hope that was helpful. Stay tuned for more.

9 comments:

  1. Your blog is extremely very descriptive and informative for those who want information regarding Remote Systems. I cannot stop myself to thank you. Please keep sharing this.

    Home Automation Vancouver | vancouver security | Best Security Vancouver

    ReplyDelete
  2. Thanks for your informative article on software testing. Your post helped me to understand the future and career prospects in software testing. Keep on updating your blog with such awesome article. Best software testing training institute in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete

  3. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.


    Manual testing training in Chennai


    Selenium testing training in Chennai

    ReplyDelete
  4. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.
    Best Digital marketing company in Chennai
    Best Digital marketing services in Chennai

    ReplyDelete
  5. I keep on reading your blog post.. This was still amazing. Thanks a lot for sharing this unique informative post with us.. I really enjoyed by reading your blog post

    App Store Optimization Services in Chennai

    ReplyDelete
  6. I have own car so now I am back out there trying to show him how to do car repairs. Your blog is an excellent source of info, thanks.

    bike spa services in mumbai
    house cleaning services in mumbai
    car wash services in mumbai

    ReplyDelete
  7. Very nice post here thanks for it I always like and search such topics and everything connected to them. Keep update more information..
    Back to original

    ReplyDelete
  8. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    Digital Marketing Company in India

    ReplyDelete