As we know it is very important to know the reason behind test failure we need to capture screenshot when test case fails. The reasons behind test failure can be a bug in application or error in our automation scripts so to know the exact problem it is very important to capture screenshot on test case failure.
I have already wrote a post on how to take screenshot you can refer that Post HERE. In this post we will see how to capture screenshot in case of test case failure.
1. ITestResult Interface - this will provide us the test case execution status and test case name.
2. @AfterMethod - An annotation of TestNG which will execute after
every test execution irrespective of test case pass or fail @AfterMethod will
always execute.
Lets take look complete code -
package captureScreenshot; // Import all classes and interface import java.io.File; import library.Utility; 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 FacebookScreenshotExample { // Create Webdriver reference WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe"); driver=new ChromeDriver();
@Test public void captureScreenshot() throws Exception { // Initiate Chrome browser // Maximize the browser driver.manage().window().maximize(); // Pass application url driver.get("http://www.facebook.com"); // Here we are forcefully passing wrong id so that it will fail our testcase driver.findElement(By.xpath(".//*[@id='emailasdasdas']")).sendKeys("QA Automated"); } // It will execute after every test execution @AfterMethod public void tearDown(ITestResult result) { // Here will compare if test is failing then only it will enter into if condition if(ITestResult.FAILURE==result.getStatus()) { try { // Create refernce of TakesScreenshot TakesScreenshot ts=(TakesScreenshot)driver; // Call method to capture screenshot File source=ts.getScreenshotAs(OutputType.FILE); // Copy files to specific location here it will save all screenshot in our project home directory and // result.getName() will return name of test case so that screenshot name will be same FileUtils.copyFile(source, new File("./ScreenShots/"+result.getName()+".png")); System.out.println("Screenshot taken"); } catch (Exception e) { System.out.println("Exception while taking screenshot "+e.getMessage()); } } // close application driver.quit(); } }
Very useful information, Thanks for sharing with us. Tally course in marathahalli
ReplyDeleteNice Bolg on How to Capture Screenshot when test failes with Selenium Webdriver, we are professionals on web development, Web design , and Software development.
ReplyDeleteTally Institute in Delhi, Noida & Gurgaon with Lowest Fees in Delhi, Ncr. Compare all things-Fees, Location, reviews, feedbacks, Duration of Course. Call Now & Get Free Demo Classes- +91-9311002620, +91-11-40504400.
ReplyDeleteMore Info- Tally Institute in Delhi, Noida & Gurgaon
Felt happy to read your blog. This kind conceptual content written on different blogs always appreciated. Most of the points can be learned from your blog.
ReplyDeleteThanks
Affiliate marketing guide
Very Interesting content!! Thankyou!!
ReplyDeleteCleck here for Azure Training