Thursday, March 7, 2019

Take screenshots in selenium

 package utility;  
 import java.io.File;  
 import java.io.IOException;  
 import java.text.SimpleDateFormat;  
 import java.util.Calendar;  
 import java.util.concurrent.TimeUnit;  
 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.chrome.ChromeDriver;  
 public class TakeScreenshot1 {  
      //Takes screenshot method  
      public static void takepics(WebDriver driver,String filePath) throws IOException{  
           File file=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);  
           File destFile=new File(filePath);  
           FileUtils.copyFile(file, destFile);  
      }  
      public static void main(String[] args) throws IOException {  
           System.setProperty("webdriver.chrome.driver",  
                     "C:/Users/Mutturaj.h/Desktop/Soft/chromedriver_win32/chromedriver.exe");  
           //Time Stamp  
           String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());  
           //File path where screenshot is saved  
           String filePath="C:/Users/Mutturaj.h/workspace/Test/Screenshots/muttu"+""+ timeStamp+".png";  
           WebDriver driver = new ChromeDriver();  
           driver.manage().window().maximize();  
           driver.manage().deleteAllCookies();  
           driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);  
           driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
           driver.get("https://www.amazon.in/");  
           // takepics() method is called after page loaded  
           TakeScreenshot1.takepics(driver,filePath);  
      }  
 }  

No comments:

Post a Comment