2

Currently working with Selenium WebDriver and Code in Java.

I have a situation that I need to create a folder in C: directory and in that folder what ever screenshots I'm taking by the selenium web-driver code it need to store in the folder with timestamps..

If i run the script in a schedule basis daily all the screenshot should be stored in that folder. Please help me.

testing
  • 1,736
  • 15
  • 46
  • 75
  • Please add some research effort or code what you tried – nitin chawda Jul 10 '14 at 06:26
  • WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com/"); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // Now you can do whatever you need to do with it, for example copy somewhere FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png")); – testing Jul 10 '14 at 06:28
  • I tried with this piece of code it is overwriting the screenshot. but i need to store each time – testing Jul 10 '14 at 06:29
  • please give me some ideas – testing Jul 10 '14 at 06:37
  • 1
    Why don't you add some timestamp each time you save a screenshot? `Date date = new Date(); long timeMilli = date.getTime();` – nitin chawda Jul 10 '14 at 06:41
  • I'm getting error as **The method copyFile(File, File) is undefined for the type FileUtils** for this line `FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));` – testing Jul 10 '14 at 06:43
  • check if you have imported correct package. `org.apache.commons.io.FileUtils` – nitin chawda Jul 10 '14 at 06:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57070/discussion-between-nitin-chawda-and-user11111). – nitin chawda Jul 10 '14 at 06:54
  • 1
    You need [commons io libs](http://commons.apache.org/proper/commons-io/) for FileUtils. Save file with timestamp: replace `"c:\\tmp\\screenshot.png"` with `"c:\\tmp\\screenshot-" + new Date() +".png"`. Or use [SimpleDateFormat](http://stackoverflow.com/questions/7150231/simpledateformat-string) to convert date to string. – Hai Lu Jul 10 '14 at 07:13

1 Answers1

0

At the beginning of the test create a time stamped folder to save screenshots using the following code:

    String scrFolder = "c:/Result/"
            + new SimpleDateFormat("yyyy_MM_dd_HHmmss").format(
                    Calendar.getInstance().getTime()).toString();
    new File(scrFolder).mkdir();

Add the following code after the above code to set an environment variable to point to the screenshot folder:

    System.setProperty("scr.folder", scrFolder);

Take screenshot by calling the following method

public static void takeScreenshot(WebDriver driver) throws IOException {
    //get the screenshot folder location from enviroment variable set in beginning of test
    String scrFolder = System.getProperty("scr.folder");
    File scrFile = ((TakesScreenshot) driver)
            .getScreenshotAs(OutputType.FILE);
    //copy screenshot to screenshot folder
    FileUtils.copyFile(
            scrFile,
            new File(scrFolder
                    + "/screenshot"
                    + new SimpleDateFormat("HHmmss").format(
                            Calendar.getInstance().getTime()).toString()
                    + ".png"));
}

Let me know if this helps you.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41