A small Tip for – How to take Screen Shot using Selenium WebDriver?

In the previous blog we had discussed in short , ” How to automate a browser !! “. As a beginner like me,  you will be very keen to take screen shot of a particular page.

Here is a simple script for taking screen shot of any URL. I have taken “http://www.flipkart.com/” as mu URL.

package login;
import java.io.File;
import java.io.IOException;
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.firefox.FirefoxDriver;

public class Screen_shot {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

// Initialize WebDriver
WebDriver driver = new FirefoxDriver();
// Wait For Page To Load

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://www.flipkart.com/");
// Maximize Window
driver.manage().window().maximize();
// Take ScreenShot
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot1.png"), true);
// Close Driver
driver.quit();
}
}

Below are few points with images.

  • 13456
  • For the above image you have to Import this package only [  ‘FileUtlis’ (org.apache.commons.io) ]
  • After completion of the script, the PNG file will be stored in your given location. I have given [ “D:\\selenium\\screenshot1.png” ]. Here is the screen shot below.

File has been saved in the format i.e png as mentioned in the script.

  • File has been saved in the format i.e “png” as mentioned in the script. We can save the image in different file type like PNG, JPEG, GIF, BMP.

Hope this helps Beginners like me… 🙂

 

 

One thought on “A small Tip for – How to take Screen Shot using Selenium WebDriver?

Leave a comment