Click here to Skip to main content
15,885,944 members
Articles / All Topics

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
24 Oct 2014CPOL 30.5K   5   2
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.

Java
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… :)

Image 7 Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
India India
I am Sipra Ray, ISTQB certified - Quality Analyst at Mindfire Solutions.
An extrovert by nature ,I like to interact with people and share with all what I learn.
I would love to share and answer queries and gain knowledge from forums like Code Project
Follow up with my blogs at:- siprabugtracker.wordpress.com

Comments and Discussions

 
QuestionCapturing Screenshots for multiple pages in selenium webdriver Pin
Member 123767307-Mar-16 20:31
Member 123767307-Mar-16 20:31 
AnswerRe: Capturing Screenshots for multiple pages in selenium webdriver Pin
MuhammadUSman111-Mar-16 18:59
MuhammadUSman111-Mar-16 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.