65.9K
CodeProject is changing. Read more.
Home

A Small Tip for How to Take Screen Shot Using Selenium WebDriver?

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Oct 25, 2014

CPOL
viewsIcon

30820

Here is 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!!“. If you are a beginner like me, you will be very keen to take a screen shot of a particular page.

Here is a simple script for taking a 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 formats like PNG, JPEG, GIF, BMP.

Hope this helps beginners like me… :)