0

I have the following Docker file that I am trying to get working with Chrome and ChromeDriver.

RUN microdnf install -y unzip
ARG CHROME_VERSION=116.0.5845.96
RUN curl -s -o  /tmp/chrome-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_VERSION/linux64/chrome-linux64.zip \
    && unzip /tmp/chrome-linux64.zip -d /opt \
    && ln -s /opt/chrome-linux64/chrome /usr/local/bin/chrome \
    && rm /tmp/chrome-linux64.zip

## ChromeDriver

ARG CHROME_DRIVER_VERSION=116.0.5845.96
RUN curl -s -o /tmp/chromedriver_linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_DRIVER_VERSION/linux64/chromedriver-linux64.zip \
    && unzip /tmp/chromedriver_linux64.zip -d /opt/selenium \
    && rm /tmp/chromedriver_linux64.zip \
    && mv /opt/selenium/chromedriver-linux64/chromedriver /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION  \
    && chmod 755 /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
    && ln -s /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver

ENV CHROMEDRIVER_PORT 4444
ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1"
ENV CHROMEDRIVER_URL_BASE ''
EXPOSE 4444

EXPOSE 8080
EXPOSE 5005
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
# For Testing
ENTRYPOINT ["java","-jar", "-Xmx600m","/app.jar"]

My java code is:

ChromeDriverService service = new ChromeDriverService.Builder()
                .withVerbose(false)
                .withSilent(true)
                .build();
        try {
            java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.WARNING);
//            service.sendOutputTo(new FileOutputStream("/dev/null"));
            service.sendOutputTo(OutputStream.nullOutputStream());
        } catch (Exception e) {
            LOGGER.error("Unable to suppress output");
        }
        return new ChromeDriver(service, getChromeOptions(useFastStrategy));

private ChromeOptions getChromeOptions(boolean useFastStrategy) {
        ChromeOptions chromeOptions = new ChromeOptions();


        chromeOptions.addArguments(String.format("user-agent=%s", USER_AGENT));
        chromeOptions.addArguments("--log-level=OFF");
        chromeOptions.addArguments("--headless=new");
        List<String> arguments = new LinkedList<>();
        arguments.add("--disable-extensions");
        // disable-infobars no longer works, see the following link for a potential answer:
        // https://stackoverflow.com/questions/57298901/unable-to-hide-chrome-is-being-controlled-by-automated-software-infobar-within
        // also see
        // https://qaautomation.expert/2022/04/01/how-to-disable-infobar-warning-for-chrome-tests-in-selenium/ . Perhaps
        // comment out 'arguments.add("disable-infobars");' in the future or try
        // chromeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation")); // <- maybe try this
        // instead of 'arguments.add("disable-infobars");'
        arguments.add("disable-infobars"); // try enabling this to try and save some cpu
        arguments.add("--headless");
        arguments.add("--disable-gpu");
        arguments.add("--no-sandbox");
        arguments.add("--incognito");
        arguments.add("--disable-application-cache");
        arguments.add("--disable-dev-shm-usage");

        chromeOptions.addArguments(arguments);
        return chromeOptions;
    }

But I am getting an error:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: '22e9e505804a', ip: '172.17.0.2'
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:536)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:108)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
...
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '17.0.2'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:237)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:119)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
...
Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: '22e9e505804a', ip: '172.17.0.2'
...
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '17.0.2'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:237)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:119)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
    ... 112 common frames omitted

Does anyone know how to get this working for a 'Chrome for Testing' with a docker container? Do I have to set some flags, or is my approach incorrect?

HeronAlgoSearch
  • 1,581
  • 2
  • 18
  • 35

1 Answers1

0

Error messages suggest that there may have been some problems when launching the browser itself.

According to Puppeteer troubleshooting for Chromium you need to make sure that you've installed all the dependencies for Chromium engine.

This was the case for my image for running ASP.NET apps. After adding a step in Dockerfile for installing all necessary dependencies from the list, I could launch Chrome for Testing using Selenium with no issues

Jan Kliszcz
  • 1
  • 1
  • 2