{"id":43053,"date":"2016-12-26T16:04:50","date_gmt":"2016-12-26T10:34:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=43053"},"modified":"2016-12-27T10:30:51","modified_gmt":"2016-12-27T05:00:51","slug":"how-to-add-screenshots-to-testng-report","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-add-screenshots-to-testng-report\/","title":{"rendered":"How to Add Screenshots to TestNG Report?"},"content":{"rendered":"<p style=\"text-align: justify;\">Taking screenshots during <a title=\"Automation Testing\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\">testing<\/a>\u00a0is often considered a good practice. Adding a screenshot to the test reports provides complete clarity and visibility of the application such as if the application is working smoothly or something in the application needs to be fixed. It also makes the report meaningful and presentable.<\/p>\n<p style=\"text-align: justify;\">While carrying out <a title=\"Manual Testing\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\">manual testing<\/a> one can take a screenshot by using &#8220;PrtScr&#8221; command or by using any screenshot capturing application. However, incase of <a title=\"What, When &amp; How of Automation Testing\" href=\"http:\/\/www.tothenew.com\/blog\/the-when-how-of-test-automation\/\">automation testing<\/a>, the process of taking screenshot and including it in the report is quite different.<\/p>\n<p style=\"text-align: justify;\">Read this blog to understand how to take a screenshot during automation testing and adding it it to the report.<\/p>\n<p style=\"text-align: justify;\"><strong>A brief about TestNG<\/strong><\/p>\n<p style=\"text-align: justify;\">T<a title=\"Soft Assertions in Selenium using TestNG\" href=\"http:\/\/www.tothenew.com\/blog\/soft-assertions-in-selenium-using-testng\/\">estNG is a powerful and easy-to-use testing framework<\/a>. It is designed in such a manner that it covers various categories of tests such as Unit, Integration, System etc. The official definition of TestNG is :<\/p>\n<blockquote>\n<p style=\"text-align: center;\"><strong><em>TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use.<\/em><\/strong><\/p>\n<\/blockquote>\n<p><strong>A task to take screenshot<\/strong><\/p>\n<ol>\n<li>Access http:\/\/google.com<\/li>\n<li>Search for cars<\/li>\n<li>Verify if there is any link containing\u00a0the word &#8220;car&#8221;<\/li>\n<li>Take screenshot<\/li>\n<\/ol>\n<p><strong>Follow the below code to take screenshots\u00a0<\/strong><\/p>\n<pre title=\"Take Screenshot\"><em>File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);\r\n\/\/The below method will save the screen shot in d drive with name \"screenshot.png\"\r\ntimeStamp = new SimpleDateFormat(\"yyyyMMdd_HHmmss\").format(Calendar.getInstance().getTime());\r\nscreenShotName = new File(\"D:\\\\MyTest\\\\Screenshots\\\\\"+timeStamp+\".png\");\r\nFileUtils.copyFile(scrFile, screenShotName);<\/em><\/pre>\n<p><strong>Adding screenshots to the report<\/strong><\/p>\n<pre title=\"Add it to report\"><em>String filePath = screenShotName.toString();\r\nString path = \"&lt;img src=\"\\\"file:\/\/\"\" alt=\"\\\"\\\"\/\" \/&gt;\";\r\nReporter.log(path);<\/em><\/pre>\n<p><strong>Complete Working Code<\/strong><\/p>\n<pre><em>public class TestNGDefaultReport {\r\nstatic WebDriver driver;\r\n@BeforeSuite\r\npublic void setup(){\r\nSystem.setProperty(\"webdriver.chrome.driver\",\"D:\\\\MyTest\\\\chromedriver.exe\");\r\n\r\nSystem.setProperty(\"org.uncommons.reportng.escape-output\", \"false\");\r\ndriver = new ChromeDriver();\r\n}\r\n \r\n@BeforeMethod\r\npublic void beforeEachMethod(){\r\ndriver.get(\"http:\/\/google.com\");\r\n}\r\n \r\n\/\/Test case 1\r\n@Test\r\npublic void cars() throws Exception {\r\nSystem.out.println(\"I am Test method and I am searching for cars\");\r\ndriver.findElement(By.name(\"q\")).sendKeys(\"Cars\");\r\n \r\ndriver.findElement(By.name(\"btnG\")).click();\r\n \r\n\/\/Wait for the results to appear\r\nThread.sleep(2000);\r\ntakeScreenshot();\r\nif(driver.findElement(By.partialLinkText(\"car\")).isDisplayed()){\r\nAssert.assertTrue(true); \r\n}\r\nelse{\r\nAssert.assertTrue(false);\r\n}\r\n}\r\n \r\n@AfterSuite\r\npublic void endOfSuite(){\r\nSystem.out.println(\"I am the end of suite\");\r\ndriver.quit();\r\n}\r\n \r\npublic static void takeScreenshot() throws Exception {\r\nString timeStamp;\r\nFile screenShotName;\r\nFile scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);\r\n\/\/The below method will save the screen shot in d drive with name \"screenshot.png\"\r\ntimeStamp = new SimpleDateFormat(\"yyyyMMdd_HHmmss\").format(Calendar.getInstance().getTime()); \r\nscreenShotName = new File(\"D:\\\\MyTest\\\\Screenshots\\\\\"+timeStamp+\".png\");\r\nFileUtils.copyFile(scrFile, screenShotName);\r\n \r\nString filePath = screenShotName.toString();\r\nString path = \"&lt;img src=\"\\\"file:\/\/\"\" alt=\"\\\"\\\"\/\" \/&gt;\";\r\nReporter.log(path);\r\n \r\n}\r\n}\r\n<\/em><\/pre>\n<p><strong>TestNG Reports<\/strong><\/p>\n<p style=\"text-align: justify;\">TestNG provides certain predefined listeners. These listeners are by default added to any test execution. Hence, different HTML and XML reports are generated for any test execution. The report is generated by default under the folder named <strong>test-output<\/strong>. You can view the report by opening <strong>index.html.<\/strong><\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>You make you reports more meaningful and presentable by adding screenshots to your reports.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking screenshots during testing\u00a0is often considered a good practice. Adding a screenshot to the test reports provides complete clarity and visibility of the application such as if the application is working smoothly or something in the application needs to be fixed. It also makes the report meaningful and presentable. While carrying out manual testing one [&hellip;]<\/p>\n","protected":false},"author":850,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":161},"categories":[1818,1],"tags":[2943,1561,3194,25,4330,3950,4329],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/43053"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/850"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=43053"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/43053\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=43053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=43053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=43053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}