ubuntu - 为什么 Docker 在我的 Selenium 项目中看不到驱动程序可执行文件?它抛出驱动程序可执行文件不存在

标签 ubuntu selenium-webdriver docker-compose containers

See the stackrace:

[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
...
... TestNG 6.14.3 by C?dric Beust (cedric@beust.com)
...


Local Operating System: LINUX
Local Architecture: amd64
Selected Browser: FIREFOX
Disable AI based Visual Testing: true
Headless Mode: true
Connecting to Selenium Grid: false


Local Operating System: LINUX
Local Architecture: amd64
Selected Browser: FIREFOX
Disable AI based Visual Testing: true
Headless Mode: true
Connecting to Selenium Grid: false

Unable to capture screenshot: null

Local Operating System: LINUX
Local Architecture: amd64
Selected Browser: FIREFOX
Disable AI based Visual Testing: true
Headless Mode: true
Connecting to Selenium Grid: false

Unable to clear cookies: null
Test Execution Time: 0 minutes, 0 seconds
java.lang.NullPointerException
    at com.netsol.configs.AutomationFactory.closeEyes(AutomationFactory.java:151)
    at com.netsol.base.SeleniumBase.closeAutomationObjects(SeleniumBase.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:425)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.615 s <<< FAILURE! - in TestSuite
[ERROR] searchSomethingInGoogle(com.package.tests.features.frontend.TEST_GoogleSearch)  Time elapsed: 0.108 s  <<< FAILURE!
java.lang.IllegalStateException: The driver executable does not exist: /home/automation/myproject
    at com.package.tests.features.frontend.TEST_GoogleSearch.searchSomethingInGoogle(TEST_GoogleSearch.java:25)

[ERROR] closeAutomationObjects(com.package.tests.features.frontend.TEST_Demo)  Time elapsed: 0.175 s  <<< FAILURE!
java.lang.NullPointerException

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   TEST_Demo>SeleniumBase.closeAutomationObjects:101 ? NullPointer
[ERROR]   TEST_GoogleSearch.searchSomethingInGoogle:25->SeleniumBase.getDriver:62 ? IllegalState
[INFO] 
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  23.142 s
[INFO] Finished at: 2019-05-29T09:21:51Z

我需要在 Docker 容器中执行我的 Selenium 测试。我知道 Selenium 提供了集线器和节点容器,但我必须编写自己的 Dockerfile 来构建一个容器,该容器将在没有网格配置的情况下执行测试。基本上,由于某种原因我不能使用网格。我写了一个 Dockerfile。它在构建阶段通过。但是,当我运行容器时,它不会实例化浏览器并抛出异常 java.lang.IllegalStateException: The driver executable does not exist:/home/automation/myproject

另外,请注意,我需要在 headless 模式下执行我的测试,因此在 headless 模式下运行浏览器的配置已经存在于我的项目中。

我已尝试授予 +rwx 权限以访问我下载和解压缩文件的位置。

查看下面我的 Dockerfile:

FROM ubuntu:latest
LABEL name="myproject" \
                author="TAFSEER HAIDER" \
                version="1.0" \
                description="Execute backend and frontend tests using myproject in containers"

    # Install Essential Softwares
RUN apt-get update \
    && apt-get install -y software-properties-common \
    && apt-get -y install openjdk-8-jdk \
    && apt-get install -y maven \
    && apt-get install -y firefox \
    && apt-get install -y unzip \
    && apt-get install -y curl \
    && apt-get install -y xvfb \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/ /tmp/* /var/tmp/*

# Chrome browser to run the tests
ARG CHROME_VERSION=latest
RUN if [ ${CHROME_VERSION} = "latest" ]; then curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub && cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub  && echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list  && mkdir -p /usr/share/desktop-directories  && apt-get -y update && apt-get install -y google-chrome-stable; else curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add && wget https://www.slimjet.com/chrome/download-chrome.php?file=lnx%2Fchrome64_$CHROME_VERSION.deb && dpkg -i download-chrome*.deb || true && apt-get install -y -f && rm -rf /var/lib/apt/lists/*;fi

# Disable the SUID sandbox so that chrome can launch without being in a privileged container
RUN dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
        && echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
        && chmod 755 /opt/google/chrome/google-chrome

RUN if [ ${CHROME_DRIVER_VERSION} != "latest" ]; then mkdir -p /opt/selenium && curl http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip && cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;fi

#Firefox browser to run the tests
ARG FIREFOX_VERSION=latest
RUN FIREFOX_DOWNLOAD_URL=$(if [ $FIREFOX_VERSION = "latest" ]; then echo "https://download.mozilla.org/?product=firefox-$FIREFOX_VERSION-ssl&os=linux64&lang=en-US"; else echo "https://download-installer.cdn.mozilla.net/pub/firefox/releases/$FIREFOX_VERSION/linux-x86_64/en-US/firefox-$FIREFOX_VERSION.tar.bz2"; fi) \
  && apt-get update -qqy \
  && apt-get -qqy --no-install-recommends install firefox \
  && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
  && wget --no-verbose -O /tmp/firefox.tar.bz2 $FIREFOX_DOWNLOAD_URL \
  && apt-get -y purge firefox \
  && rm -rf /opt/firefox \
  && tar -C /opt -xjf /tmp/firefox.tar.bz2 \
  && rm /tmp/firefox.tar.bz2 \
  && mv /opt/firefox /opt/firefox-$FIREFOX_VERSION \
  && ln -fs /opt/firefox-$FIREFOX_VERSION/firefox /usr/bin/firefox




# Add a user for running applications.
RUN apt-get update && apt-get install sudo -y \
    && adduser --disabled-password --gecos '' automation \
    && adduser automation sudo \
    && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/ /tmp/* /var/tmp/* 

# Copy myprojectproject to the specified directory in containerized Ubuntu Linux
COPY --chown=automation . /home/automation/myproject/


# Set working directory
WORKDIR /home/automation/myproject/

RUN chown -R automation:automation /home/automation/myproject\
    && chmod -R 777 /home/automation/myproject\
    && chmod -R +rwx /home/automation/myproject

USER automation

# Clean & compile myproject
RUN mvn clean test-compile

最佳答案

您的代码似乎期望在 /home/automation/myproject 中找到 geckodriver 和 chromedriver

查看您的 Dockerfile,您没有在任何地方安装 CromeDriver,Geckodriver 在 /home/automation/myproject/src/test/resources/webdrivers/selenium_standalone_binaries/linux/marionette/64bit

如果您打印出 ENV 变量的值,将会很有用:

  • webdriver.chrome.driver
  • webdriver.gecko.driver

然后我们可以 100% 确定您的代码期望在哪里找到驱动程序二进制文件,然后您可以检查它们是否确实存在。

关于ubuntu - 为什么 Docker 在我的 Selenium 项目中看不到驱动程序可执行文件?它抛出驱动程序可执行文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357502/

相关文章:

ubuntu - 如何在 Ubuntu (14.04) 上更新 mingw32 中的 gcc?

python - OpenCV VideoCapture 中的延迟/滞后

java - 使用 Selenium Webdriver 查找并切换到正确的框架

java - 类 FluentWait<T> 中的方法 until 不能应用于给定类型

ubuntu - 使用 grep 或 awk 在文本字符串中搜索特定文本

php - 在 PHP 中运行 inkscape

docker-compose 使用包含实验功能的 Dockerfile 构建

python - python版本在Docker构建期间导致问题

docker - 从 docker-compose 网络内部使用其 url 访问另一个服务