Tuesday, 15 March 2016

How to take screenshots on failure in selenium webdriver

How to take screenshots on failure in selenium webdriver:
============================================
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import org.apache.commons.io.FileUtils;

import org.testng.annotations.Test;         

public class webDriverTakeScreenshot {
   public WebDriver driver;               

   @Test
   public void runDummyTest() throws Exception {    
      driver = new FirefoxDriver();
      driver.get("http://rameshselenium.blogspot.in/");

      try {               
         driver.findElement(By.id("")).click();
      }
      catch(Exception e) {    
         System.out.println("Element Not Found");   
         takeScreenshot();     
      }     
   }

   public void takeScreenshot() throws Exception {             
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(scrFile, new File(C:\\test\\failed-test.png));     
   }
}

No comments:

Post a Comment