1

I have developed a Java app that will automatically download a file from a certain website. It is working fine if you will run it manually in cmd on my local machine and on the server. However, when I tried to run the Java app using Task Scheduler set to run every day in Windows Server 2016 I get a bunch of errors in Web Driver. The task scheduler sometimes works on random days and sometimes it doesn't and it throws any of these errors.

org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: chrome not reachable

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.id: i0116 (tried for 600 second(s) with 500 milliseconds interval)

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist

Java version of my workstation and Windows server

openjdk version "11.0.13" 2021-10-19 LTS OpenJDK Runtime Environment Corretto-11.0.13.8.1 (build 11.0.13+8-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.13.8.1 (build 11.0.13+8-LTS, mixed mode)

Edge browser version of my workstation and Windows server

Version 97.0.1072.55 (Official build) (64-bit)

Webdriver version of my workstation and Windows server

Version 97.0.1072.55

Task Scheduler configurations:

  • Run whether user is logged on or not
  • Run with highest privileges
  • Wake the computer to run this task
  • Allow task to be run on demand
  • Run task as soon as possible after a scheduled start is missed

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main.ReportsViewMain</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
    </build>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-devtools-v96</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Getting the driver Java Class

package MyWebDriver;

import Entity.FilePaths;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class GetDriver {

    public WebDriver getDriver(
            WebDriver browser,
            FilePaths filePaths,
            boolean isHeadlessDriver,
            String downloadPath,
            String expectedUrl, PrintWriter out, String dateTimeFormat){

        while (browser == null){
            int webdriverRetry = 10;
            int count = 0;
            if (count < webdriverRetry){
                try {
                    // Setting the driver executable
                    System.setProperty("webdriver.edge.driver", filePaths.getWebDriverPath());

                    // Start the web browser session
                    EdgeOptions op = new EdgeOptions();

                    op.addArguments("--no-sandbox"); // Bypass OS security model, must be the very first option to run
                    op.addArguments("inprivate"); // Open in private mode
                    op.addArguments("disable-infobars"); // disabling infobars
                    op.addArguments("--disable-extensions"); // disabling extensions
                    op.addArguments("--disable-gpu"); // applicable to windows os only
                    op.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems

                    if (isHeadlessDriver) {
                        op.addArguments("--headless"); // run the selenium without GUI, downloading will not work

                        // Workaround to download a file while in headless mode
                        Map<String, Object> prefs = new HashMap<>();
                        prefs.put("download.default_directory", downloadPath);
                        op.setExperimentalOption("prefs", prefs);
                    }
                    else {
                        op.addArguments("start-maximized"); // open Browser in maximized mode}
                    }
                    browser = new EdgeDriver(op);
                    browser.get(expectedUrl);

                    if (browser != null) {
                        out.println(DateTimeFormatter.ofPattern(dateTimeFormat)
                                .format(LocalDateTime.now())
                                + " INFO: Web driver successfully initialized");
                        System.out.println("INFO: Web driver successfully initialized");
                        break;
                    }
                    else {
                        throw new Exception("Failed to load web driver (GET-DRIVER CLASS) ");
                    }
                }
                catch (Exception e){
                    out.println(DateTimeFormatter.ofPattern(dateTimeFormat)
                            .format(LocalDateTime.now())
                            + " ERROR: Cannot initialized web driver, " +
                            "forcing to kill all Edge browsers and Web Drivers  " + e);
                    try {
                        Runtime.getRuntime().exec("taskkill /F /IM msedgedriver.exe /T");
                        Runtime.getRuntime().exec("taskkill /F /IM msedge.exe /T");
                        browser = null;
                    } catch (Exception err){
                        out.println(DateTimeFormatter.ofPattern(dateTimeFormat)
                                .format(LocalDateTime.now()) + " ERROR: Cannot execute task kill (GET-DRIVER CLASS)");
                    }

                    if (count < webdriverRetry){
                        out.println(DateTimeFormatter.ofPattern(dateTimeFormat)
                                .format(LocalDateTime.now())
                                + " INFO: Web driver will retry to initialized, Retry Count: " + count);
                        out.println(" ");
                        count ++;
                    }else if(count > webdriverRetry){
                        out.println(DateTimeFormatter.ofPattern(dateTimeFormat)
                                .format(LocalDateTime.now())
                                + " ERROR: Maximum retry limit has been reached");
                        out.println(" ");
                    }
                    System.out.println("ERROR: Cannot initialized web driver " + e);
                }
            } // End if
        } // End while
        return browser;
    }
}

The above Java class will retry to load the web driver session. 100% this will work when I'm running this on my local machine via CMD and on the Windows Server. But if I will run this via task scheduler there's a chance that the web driver session cannot be initialized.

Based on my investigation and theory It's failing due to windows doesn't have a GUI when using task scheduler.

So my question is there a workaround for windows to run the task scheduler and allow the Java app that I've created to allow automatic GUI interaction.

0 Answers0