How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver
How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver:
Earlier I have posted a detailed post on how to capture a screenshot using Selenium WebDriver. If you have missed it, you could check the detailed post on how to capture screenshot using Selenium WebDriver.
If a script fails, we need to know where was the error in script. Solution for this is to capture a screenshot of webpage when the test case fails. We could easily identify where exactly the script got failed by seeing the screenshot.
To achieve this, we could place the entire code in try-catch block. Which means placing the test steps in try block and screen capture statement in catch block. If a test step fails in the try block then it goes to the catch block and capture a screenshot of the web page.
Below mentioned script shows how to capture a screenshot of failed test cases using Selenium WebDriver.
package softwareTestingMaterial; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class CaptureScreenshot { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); try{ driver.get("https://www.softwaretestingmaterial.com"); driver.navigate().refresh(); //driver.findElement(By.xpath("//*[@id='cse-search-box']/div/input[4]")).sendKeys("agile"); //Statement with correct Xpath driver.findElement(By.xpath("//*[@id='cse']")).sendKeys("agile"); //Statement with incorrect Xpath }catch(Exception e){ File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("D:\\SoftwareTestingMaterial.png")); } driver.close(); driver.quit(); } }
Another way of capturing screenshot of failed test cases using Selenium WebDriver is to use ITestResult Interface.
ITestResult interface provides the test case execution status and test case name
We do place the screen capture steps in a separate method using AfterMethod TestNG Annotation. If we use TestNG AfterMethod annotation then this particular method (AfterMethod) executes after every test execution.
Below mentioned script shows how to capture a screenshot using ITestResult Interface.
package softwareTestingMaterial; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; public class ScreenshotFailedCases { // Create Webdriver reference static WebDriver driver; @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.softwaretestingmaterial.com"); driver.navigate().refresh(); //driver.findElement(By.xpath("//*[@id='cse-search-box']/div/input[4]")).sendKeys("agile"); //Statement with correct Xpath driver.findElement(By.xpath("//*[@id='cse']")).sendKeys("agile"); //Statement with incorrect Xpath } @AfterMethod //AfterMethod annotation - This method executes after every test execution public void screenShot(ITestResult result){ //using ITestResult.FAILURE is equals to result.getStatus then it enter into if condition if(ITestResult.FAILURE==result.getStatus()){ try{ // To create reference of TakesScreenshot TakesScreenshot screenshot=(TakesScreenshot)driver; // Call method to capture screenshot File src=screenshot.getScreenshotAs(OutputType.FILE); // Copy files to specific location // result.getName() will return name of test case so that screenshot name will be same as test case name FileUtils.copyFile(src, new File("D:\\"+result.getName()+".png")); System.out.println("Successfully captured a screenshot"); }catch (Exception e){ System.out.println("Exception while taking screenshot "+e.getMessage()); } } driver.quit(); } }
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
in the first code box, you wrote “cactch” instead of catch in the try catch section.
Good catch LDionsio. Thanks. Have modified that.
Hi Rajkumar
thanks a lot ur explanation is very useful for interview purpose
Thanks for your kind words Shamsher Khan
Hi raj,,
when you are using ITestResult interface we need to implement that interface right…with out implementing interface can we directly use in screenshot method
Thanks
We can use. I have mentioned two methods in the article.