Home » Selenium » Taking Screenshot in Selenium .

Taking Screenshot in Selenium .

Before starting this blog just read “Installation Of Selenium Web driver in Eclipse” to understand Selenium Installation.

1. Now Create new java project let’s say “SeleniumTest” in eclipse.

2. Now create package let’s say “selenium.abhijeet” in “SeleniumTest” project.

3. Now create class i.e. “Selniumtest” in “selenium.abhijeet” package.

4. Now copy the below code and Run the project . That’s It.


package selenium.abhijeet;

import java.io.File;
import java.io.IOException;
import java.util.Random;

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 Selniumtest {

//Driver Definition .
static WebDriver driver = new FirefoxDriver();

// Test Case Name which is used to create folder in Screenshot folder. You can manage this name and used in the function.
static String testcasename = “SampleTest”;

//Main Method.
public static void main(String[] args) {

// Launching Application through Selenium Webdriver.
driver.get(“Your Application URL“);

//Calling Screenshot method.
Selniumtest screenshot= new Selniumtest();

// Method to Capture Active screen .
public void ScreenshotCapture()
// Random method for getting random number so that It can be used to give screen shot name .
//You can use anything instead of random like Sequence from 1,2,3,… by incrementing variable value.
Random rand = new Random();
int Randomnumber = rand.nextInt(1000);

// Now taking screenshot using the following code.
File screenshotfile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try {
// Give path where you want to store your screenshot.
FileUtils.copyFile(screenshotfile, new File(“c:\\Screenshot\\”+testcasename+”\\Screenshot” + Randomnumber+”.jpg”));
} catch (IOException e) {



5. Screenshot folder created in C Drive .

6. And in that folder different folders will be created depending upon Test Case names which you have given.

7. In the folder having TestCase Name screenshot will be store.

You can make folder structure and naming conventions in screenshot captures as per you requirements.

Note: You can use this screenshot code as per you need and requirement.

Suggestions and Feedback are most welcome.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s