Selenide IllegalArgumentException: Failed to create folders

The error “java.lang.IllegalArgumentException: Failed to create folder” in Selenide typically happens when Selenide tries to save screenshots, page sources, or logs, but it cannot create the required directories due to permission issues or incorrect paths.

exceptions-notes

Selenium and Selenide are primarily used for performing browser UI testing and automating the workflows in the browser. But sometimes, we may need to create a desktop app that performs certain business functions by navigating a web application in the browser. This is commonly seen usecase in business applications that do not provide API access to fetch/update their data.

Selenide is an excellent choice for such applications which works extremely well in automating the browser workflows. But, in runtime, Selenide requires certain directories to be present to store the output/screenshots captured during the execution.

1. The Problem

The error “java.lang.IllegalArgumentException: Failed to create folder” in Selenide typically happens when Selenide tries to save screenshots, page sources, or logs, but it cannot create the required directories due to permission issues or incorrect paths.

By default, in the development environment such as an IDE, Selenide uses the project root directory as the base folder and creates required subdirectories into it. So we will never face this issue in the development environment.

But suppose, we have to distribute this utility application as an installable application or exe. When we install the application, it is generally installed in ‘c:/program files‘ and the operating system will not allow to create subdirectories into it for security reasons. So when we will launch the application, we will get the following IllegalArgumentException: Failed to create folder error.

2025-02-17 15:26:30 com.example.app.processor.BrowserManager ERROR [153] : An error occurred during the workflow execution:
java.lang.IllegalArgumentException: Failed to create folder 'C:\Program Files\myapp\build\downloads\1739786190288_10940_51'
	at com.codeborne.selenide.impl.FileHelper.ensureFolderExists(FileHelper.java:48) ~[myapp-1.0.0-jar-with-dependencies.jar:?]
	...
	...
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:?]
	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[?:?]
	at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: java.nio.file.AccessDeniedException: C:\Program Files\GST IMS Tool\build
	at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) ~[?:?]
....

2. The Reason

As mentioned above, the error occurs because the operating system does not allow Selenide to create folders due to security restrictions.

In the development environment, generally, we do not work in secured system folders so there is no issue faced. But in in an installed application, Selenide tries to create folders into system directories which is not permitted hence this error occurs.

3. The Solution

Solution 1: Stop Generating Execution Results

The first method is pretty straightforward. Do not generate all execution results such reports, logs and screenshots. If the output is not generated, there is no need for the subdirectories to create itself.

import com.codeborne.selenide.Configuration;

public class TestConfig {

    static {

        // Disable screenshot capturing
        Configuration.screenshots = false;

        // Disable saving page source
        Configuration.savePageSource = false;

        // Prevent storing reports (Windows equivalent of /dev/null is NUL)
        Configuration.reportsFolder = "NUL"; 
    }
}

Solution 2: Configure Directory Paths with Write Permission

Another solution to the problem is to write the generated reports/screenshots in a directory where the application has the write permissions.

We can configure the pre-existing sub-directories’ path, and even create these sub-directories into the application code itself.

// ............. Inside AppConstants.Java

public static final String BASE_DATA_DIR = System.getProperty("user.home") +
    File.separator + "AppData" +
    File.separator + "Local" +
    File.separator + APP_NAME;

// .............. Inside BrowserManager.java

static {

	//Setup web driver. Chrome in this case.

	createDirectories();
}

private static void createDirectories() {

  List<String> requiredDirs = Arrays.asList(
      "downloads", 
      "reports/tests",
      "reports/screenshots",
      "reports/page-sources",
      "chrome_data"
    );

    requiredDirs.forEach(dir -> {
      try {
          Files.createDirectories(Paths.get(BASE_DATA_DIR, dir));
      } catch (IOException e) {
          log.error("Failed to create directory: {}", dir, e);
      }
    });
}

You can refactor/rearrange the above code as per your requirements but the overall logic remains the same. This code, when the application starts, will create the necessary dependencies in C:\Users\myUser\AppData\Local\myApp folder which generally has write permissions for writing application cache and data files.

4. Conclusion

In this short Java tutorial, we discussed a common problem seen in an installed Java application that uses Selenide to run browser-based workflows. The solution will help in creating the mandatory directories used for storing the execution results.

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.