Monday, November 19, 2012

How to take screen shot ?

Do you want to take screenshots while automation running? TakesScreenshot interface for capturing a screenshot
of a web page. If take Screenshot when en exception or failure occurred then it will be easy to pin point issue. We can also take screenshot for verifying values, text in webPage.

Using below method you can take screenshots while running the automation.

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;

 public static void takeScreenShot(WebDriver driver,String fileName)  
      {  
           try {  
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
                FileUtils.copyFile(scrFile, new File("C:\\"+fileName+".png"));  
           }   
           catch (Exception e) {  
                e.printStackTrace();  
           }  
      }