0

In my script, I need to take screenshots at multiple locations. I want to save all screenshots in the same folder, with a unique name for each file.

There are answers explaining how to append time/date stamps to the file, which I do not need. The script will run every week and will overwrite the previous week's image files.

I'm using Java with Webdriver. Here is what I have:

String screenshots;
screenshots = "C:/eStore/Projects/Screenshots/Catalog/";  //location for images

File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshots, new File("screenshots + 01Belts.jpg"));
5
  • What are your expectations? Isn't adding System.currentTimeMillis good enough? Commented Mar 15, 2015 at 15:11
  • Perhaps I wasn't clear. I need each screenshot to have a unique file name, not a timestamp. When the script has completed, I want to have 12 saved screenshots - each with a unique filename that describes the image (i.e., 01Belts). The next time I run the script, I expect that each file will be overwritten with a new screenshot - but I still want to retain that same filename each time. Commented Mar 15, 2015 at 21:59
  • So u can add each testcase name i.e. method name in filename? Cause when ever there is failure the method name is checked for result. You can use that methodname for your filename. Is that good? Commented Mar 16, 2015 at 5:32
  • That may work for me. Can you give me an example of what the code would look like for this? Commented Mar 16, 2015 at 17:20
  • I have provided the solution... another thing that i noted why are you having 2 screenshots variable? Is that a typo? I hope so.. Commented Mar 16, 2015 at 18:07

4 Answers 4

1

So as discussed you can get a way through this:

@AfterMethod
public void tearDown(ITestResult result) throws IOException {
    String location = "C:/eStore/Projects/Screenshots/Catalog/";  //location for images
    String methodname = result.getName(); // fetching test method name
    try {
        File screenshots = ((TakesScreenshot) augmentedDriver)
                               .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(
            screenshots,
            new File(location + methodName + "_" + ".png");
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
          driver.quit();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I modified the code a bit to get exactly what I need. try { File screenshots = ((TakesScreenshot)driver) .getScreenshotAs(OutputType.FILE); FileUtils.copyFile( screenshots, new File(location + "05Electrical" +".jpg"));
0

First add time/date to the file name. Then in your code, check if it is a new week. If so, delete all files of the dir.

For the check, you may save a variable to disk of the last running time of your code. Then after starting program, you should check if the saved last running time is not the same week as now time. You should also check time when program is running on a regular basis.

Comments

0

I designed below method to get screenshot file name:

/**
     * Take window screeshot
     * @param fileName
     * @return null
     * @throws IOException 
     */
    public void getScreenShot(String fileName) throws IOException{
        DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy h-m-s");
        Date date = new Date();
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("E:\\selenium logs\\"+fileName+"-"+dateFormat.format(date)+".png"));
    }

for parameter I used below to get current method name:

String name = new Object(){}.getClass().getEnclosingMethod().getName();
getScreenShot(name);

let me know if this works for you.

Comments

0

Try this Sample Program to get the Screenshot of Launched Url:

package com.simple.com;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;    

public class ScreenShot {
    public static WebDriver driver;
    @Parameters({"filename"})
    @Test
    public static void snap(String filename) throws IOException {

        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver-v0.18.0-win32\\geckodriver.exe");
        driver = new FirefoxDriver();

        String url ="https://www.inc.com/peter-economy/17-super-positive-quotes-that-will-inspire-you-to-be-exceptional.html";
        driver.get(url);

        DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy h-m-s");
        Date date = new Date();
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        filename="Super_Selenium";
        FileUtils.copyFile(scrFile, new File("D:\\SeleniumScreenshot_pass\\"+filename+"-"+dateFormat.format(date)+".png"));
        driver.quit();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.