简体   繁体   中英

Unable to create local windows directory structure using Selenium

Trying to follow the examples outlined here and here to ensure that the directory I'm putting my results reports in is always present for a given user.

new File(sampleFolder).mkdir();

Where sampleFolder is showing in the Eclipse debugger as "C:\\Users\\CurrentUser\\workspace\\Automation_Framework//Reports//output//TestCasesHtmlReports//"

and is populated from the variable definition

public static String sampleFolder = System.getProperty("user.dir") + "//Reports//output//TestCasesHtmlReports//";

However when I run the script the folder structure is NOT being created, any ideas what I'm doing wrong? Do I need to run Eclipse as an administrator?

You need to make two changes:

Change 1:

You are inputting the slashes incorrectly. I am not sure how the '//' gets parsed. On windows the "\\" gets resolved to a "\\" because the first backslash is used as a escape character.

You could compose the File path in a standard way as below. File.separator is Platform dependent default name-separator character as String. For windows, it's '\\' and for unix it's '/'

 public static String sampleFolder = System.getProperty("user.dir") + File.separator + "Reports" + File.separator + "output" + File.separator+ "TestCasesHtmlReports";

Information about Java separator's in this thread .

Change 2:

You need to use File.mkdirs instead of File.mkdir

The problem is with the API you are using. File.mkdir Javadoc reads

public boolean mkdir()

Creates the directory named by this abstract pathname.

Returns:

true if and only if the directory was created; false otherwise

File.mkdirs Javadoc reads

public boolean mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Returns:

true if and only if the directory was created, along with all necessary parent directories; false otherwise

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM