1

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?

Ravi Sharma
  • 244
  • 2
  • 14
  • Possible duplicate of [What characters allowed in file names on Android?](https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android) – Robin Topper Jul 13 '17 at 09:39
  • I am not saving it on SD card but that the project location. I edited the question. Thanks – Ravi Sharma Jul 13 '17 at 09:41

2 Answers2

3

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"));
Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29
  • Your pattern is working. It gives the output as **2017-07-13 15-26-15**. Thanks but I am looking for format **2017-07-13 15:26:15** – Ravi Sharma Jul 13 '17 at 09:58
  • Windows can't recognize the pattern `:` in date format. So, it's better you should go with my answer. Now it's your choice. When you use this pattern `yyyy-MM-dd HH:mm:ss` your script may getting the screen shot but, Hours, minute and seconds will displaying wrong. Check it if you want to verify my words. – Jainish Kapadia Jul 13 '17 at 10:05
  • Thanks @Jainish. I used this pattern `yyyy-MM-dd HH:mm:ss` it is not generating screenshot. I think I should go with `yyyy-MM-dd HH-mm-ss`. – Ravi Sharma Jul 13 '17 at 10:09
2

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

Robin Jonsson
  • 2,761
  • 3
  • 22
  • 42