java - Chrome 用于测试无法在 Docker 容器中启动

标签 java docker google-chrome selenium-webdriver selenium-chromedriver

我有以下 Docker 文件,我正在尝试将其与 Chrome 和 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"]

我的java代码是:

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;
    }

但是我收到错误:

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

有谁知道如何使用 Docker 容器使其在“Chrome 测试版”上工作?我是否必须设置一些标志,或者我的方法不正确?

最佳答案

错误消息表明启动浏览器本身时可能存在一些问题。

根据Puppeteer troubleshooting for Chromium您需要确保已安装 Chromium 引擎的所有依赖项。

我的运行 ASP.NET 应用程序的镜像就是这种情况。 在 Dockerfile 中添加一个步骤来安装列表中的所有必需依赖项后,我可以使用 Selenium 启动 Chrome 进行测试,没有任何问题

关于java - Chrome 用于测试无法在 Docker 容器中启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76952961/

相关文章:

java - BufferedReader 和扫描仪 : limit the number of input characters

java - Spring 集成测试消耗大量内存,在 GradleWorkerMain 中使用大量重复线程

docker - 拉取 docker 镜像

javascript - 从 Google Chrome 扩展访问 cookie

google-chrome - 显示内联 block 仍然在 chrome 中断行

java - java中的继承问题

Docker 在 v1 插件注册表 : plugin not found 中找不到插件桥

linux - 如何将主机目录挂载到正在运行的 docker 容器中

ios - iOS 上的所有浏览器都使用 WKWebview 还是 UIWebVIew?

java - 为什么我的程序抛出 StringIndexOutOfBounds 异常?