Ravi Sharma
Ravi Sharma

Reputation: 254

What are the other patterns of SimpleDateFormat method to name screenshot file in Appium Automation testing using Selenium?

I am using Appium for testing Android App using selenium. I have used a method to take screenshot of the testcases. It is running fine. The Image file is saved with name of the dateformat using SimpleDateFormat. If I change the pattern of the dateformat it shows this error:

java.io.IOException: The filename, directory name, or volume label syntax is incorrect

My current pattern which is working fine is in the code is dd-MMM-yyyy__hh_mm_ssaa and it saves my file name as 13-Jul-2017__01_07_01PM. It is not saved on SD card but at the location of my project.

I want to change name to 13-Jul-2017_01:01:01 PM.

here is my method for screenshot:

public void takeScreenShot() {

         String destDir;
         DateFormat dateFormat;

          // Set folder name to store screenshots.
          destDir = "screenshots";
          // Capture screenshot.
          File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
          // Set date format to set It as screenshot file name.
          dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
          // Create folder under project with name "screenshots" provided to destDir.
          new File(destDir).mkdirs();
          // Set file name using current date time.
          String destFile = dateFormat.format(new Date()) + ".png";

          try {
           // Copy paste file at destination folder location
           FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
          } catch (IOException e) {
           e.printStackTrace();
          }
    }  

What other patterns I can use?

Upvotes: 1

Views: 648

Answers (2)

Jainish Kapadia
Jainish Kapadia

Reputation: 2611

Try this format, I am using this format for verification of my pass/fail test cases.

Date d = new Date();
System.out.println(d.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png"));

Upvotes: 3

Robin Jonsson
Robin Jonsson

Reputation: 2851

First of all take a look at the documentation of using SimpleDateFormat

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

But to achieve the wanted output, your pattern should look like this:

dd-MMM-yyyy_hh:mm:ss aa

But as far as I know, you can't have : in a filename. So you need some other format

Upvotes: 2

Related Questions