skip to Main Content

I have the following container up and running on Windows

docker pull selenium/standalone-chrome
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

The program takes screenshot from a website and writes to a docker filesystem but I do not see that file in /dev/shm folder. Nor do I see any error when the program executes.

//Uses Selenium Grid Hub, the browser is running with a Docker container
//docker pull selenium/standalone-chrome
//docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
//http://localhost:4444/. It reflects Selenium Grid UI
@Override
public void run(String... args) {
    try {
        ChromeOptions chromeOptions = new ChromeOptions();
        URL url = new URL("http://localhost:4444/wd/hub");
        RemoteWebDriver driver = new RemoteWebDriver(url, chromeOptions);
        driver.get("https://www.scaler.com/topics/");
        log.info("Title: {} ", driver.getTitle());
        pause(5);

        boolean screenshotSuccessFlag = screenshot(driver, "screenshot.png");

        driver.quit();
    } catch (Exception e) {
        log.error("An error occurred: {} ", e.getMessage());
    }
}

private void pause(Integer seconds) {
    try {
        TimeUnit.SECONDS.sleep(seconds);
    } catch (InterruptedException e) {
        log.error("Exception while pausing for {} seconds: ", seconds);
    }
}

private boolean screenshot(WebDriver driver, String pathname) {
    try {
        File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src, new File(pathname));
        return true;
    } catch (IOException e) {
        log.error("Error while taking screenshot: {}", e.getMessage());
        return false;
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Realized that since the code is not containerized it puts the screenshot in the same local Windows filesystem. Was able to find the file in my C:/


  2. FileUtils.copyFile(src, new File(pathname));
    This line will copy the contents of src in the file that is defined by the pathname variable. Since you are only passing the filename "screenshot.png" as the pathname the file "screenshot.png" will be created in the current directory. Can you please try passing the values as /dev/shm/screenshot.png in the pathname variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search