Search code examples
gradlefileutilstakesscreenshot

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


I have written a simple script to take the screenshot but it is giving java.io.IOException The filename, directory name, or volume label syntax is incorrect what's wrong with the code, or is it anything to do with Gradle version

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package WDM;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class App {

    public static WebDriver createDriver() {
        // TODO : INITIALIZE WEB DRIVER USING WDM
        WebDriverManager.chromedriver().setup();
        System.out.println("Driver version " + WebDriverManager.chromedriver().getDownloadedDriverVersion());
        System.out.println("Driver path " + WebDriverManager.chromedriver().getDownloadedDriverPath());

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless", "--window-size=1920,1200");
        WebDriver driver = new ChromeDriver(options);
        return driver;
    }

    public static void verifySearchBox(WebDriver driver) {
        // TODO: Validate the presence of search box
        driver.get("https://crio-qkart-frontend-qa.vercel.app/");
        WebElement searchBox = driver.findElement(By.cssSelector(".css-11zsshc"));
        if (!searchBox.isDisplayed()) {
            System.out.println("The search box is not visible");
        } else {
            System.out.println("Verified Search Box successfully");
        }

    }

    public static void takeScreenshot(WebDriver driver, String screenshotType, String description) {
        try {
            File theDir = new File("/screenshots");
            if (!theDir.exists()) {
                theDir.mkdirs();
            }

            String timestamp = String.valueOf(java.time.LocalDateTime.now());
            String fileName = String.format("screenshot_%s_%s_%s.png", timestamp, screenshotType, description);

            TakesScreenshot scrShot = ((TakesScreenshot) driver);
            File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);

            File DestFile = new File("screenshots/" + fileName);
            FileUtils.copyFile(SrcFile, DestFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void launchQkartTakeScrnShot(WebDriver driver) {
        driver.get("https://crio-qkart-frontend-qa.vercel.app/");
        takeScreenshot(driver, "QKART Home Page", "Screenshot Of QKART Hom");

    }

    public static void main(String[] args) {
        WebDriver driver = createDriver();
        try {
            // verifySearchBox(driver);
            launchQkartTakeScrnShot(driver);
        } catch (Exception e) {
            System.out.println("Exception Occured " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

This code creates a screenshot folder but not creating a file inside it.

enter image description here


Solution

  • The string timestamp contains the character ":" which is forbidden in windows, so while using the timestamps for naming a file always replace the ":" character with some other valid char. reffer to this