首页 > 解决方案 > 尝试通过 Selenium Docker 映像上的 Chrome 驱动程序与 Intern 一起运行 javascript 单元测试的问题

问题描述

我正在尝试通过 Selenium Docker 映像上的 Chrome 驱动程序与 Intern 一起运行我的 javascript 单元测试(注意:这些测试通过 Intern 和 Selenium 的本地版本对我来说运行良好)。到目前为止,我已经完成了以下 5 个步骤:

  1. 下拉独立 Chrome 图像: docker pull selenium/standalone-chrome

  2. 运行一个独立的 Chrome 容器: docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

  3. 使用 DockerFile 将 Chrome 驱动程序手动安装到独立 Chrome 映像上:

# We need wget to set up the PPA and xvfb to have a virtual screen and unzip to install the Chromedriver
RUN sudo apt-get install -y wget xvfb unzip

# Set up the Chrome PPA
RUN sudo wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

# Update the package list and install chrome
RUN sudo apt-get update -y
RUN sudo apt-get install -y google-chrome-stable

# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_VERSION 2.19
ENV CHROMEDRIVER_DIR /chromedriver
RUN sudo mkdir $CHROMEDRIVER_DIR

# Download and install Chromedriver
RUN sudo wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
RUN sudo unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR

# Put Chromedriver into the PATH
ENV PATH $CHROMEDRIVER_DIR:$PATH 
  1. 设置我的实习生配置文件:
        proxyPort: 9000,

        // A fully qualified URL to the Intern proxy
        proxyUrl: 'http://localhost:9000/',

        // Default desired capabilities for all environments. Individual capabilities can be overridden by any of the
        maxConcurrency: 3,

        // Whether or not to start Sauce Connect before running tests
        useSauceConnect: false,
        
        capabilities: {
            'selenium-version': '4.0.0',
            'seleniumProtocol': 'WebDriver',
            'browserName': 'chrome', 
            'platform': 'Linux',
            'version': '95.0',
            'ignoreProtectedModeSettings': true,
            'chromeOptions': {
              'args': [ '-incognito', '--no-sandbox', '--disable-dev-shm-usage', '--headless' ]
            }
        },

        environments: [
            { browserName: "chrome", 'platform': 'Linux', 'version': '95.0'}
        ],

        runnerClientReporter: {
            writeHtml: false
        },
    
        tunnel: 'NullTunnel',
    
        webdriver: {
            host: 'localhost',
            port: 4444,
        }, 
  1. 运行引发以下错误的 javascript 测试:
[exec] [10:39:17] Starting 'intern-tests'...
     [exec] (node:80584) Warning: Accessing non-existent property 'VERSION' of module exports inside circular dependency
     [exec] (Use `node --trace-warnings ...` to show where the warning was created)
     [exec] SUITE ERROR
     [exec] UnknownError: [POST http://localhost:4444/wd/hub/session/b63ae473e271e0927ede8816720cf81e/url / {"url":"http://localhost:9000/__intern/client.html?config=intern-config%2Fintern-config.js&reporters=%7B%22writeHtml%22%3Afalse%2C%22id%22%3A%22WebDriver%22%7D&basePath=%2F&initialBaseUrl=%2F&rootSuiteName=chrome%2095.0%20on%20Linux%20-%20unit%20tests&sessionId=b63ae473e271e0927ede8816720cf81e"}] unknown error: net::ERR_CONNECTION_REFUSED
     [exec]   (Session info: headless chrome=95.0.4638.54)
     [exec] Build info: version: '4.0.0', revision: '3a21814679'
     [exec] System info: host: '64d03736d73e', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.47-linuxkit', java.version: '11.0.11'
     [exec] Driver info: driver.version: unknown

基于错误说Driver info: driver.version: unknown我尝试了上面的第 3 步(手动安装 Chrome 驱动程序),所以我不确定这个错误是否已经完全与 Chrome 驱动程序有关,我认为这可能是正确设置。

标签: dockerseleniumgoogle-chromeselenium-webdriverselenium-chromedriver

解决方案


完全删除第 3 步

像 selenium//standalone-chrome 这样的官方 selenium docker 镜像已经安装了浏览器和相应的驱动程序。

更新第 4 步的配置

删除以下变量的所有痕迹及其各自的值:

platform; version;

还注意到代理配置

// A fully qualified URL to the Intern proxy
        proxyUrl: 'http://localhost:9000/',

基于上述,您声明代理已安装在您的本地计算机上,但是当您将驱动程序请求发送到 chrome 节点时,Chrome 浏览器将翻译代理是在 localhost:9000 上实现的,这是它所在的平台安装在(即 Docker 容器)上。

您需要使用托管代理和 Docker 容器的本地计算机的 ip 更新实习代理配置(即 proxyUrl: 'http://192.168.111.222:9000/

您还可以使用您选择的域名(例如 my-machine)对其进行更新,但您需要使用以下命令更新 Docker 命令:

--add-host=my-machine:<ip-address>

所以你的 docker run cmd 看起来像这样:

docker run -d -p 4444:4444 -v /dev/shm:/dev/shm --add-host=my-machine:192.168.111.222 selenium/standalone-chrome

本地机器主机文件更新:

127.0.0.1       my-machine

实习配置:

// A fully qualified URL to the Intern proxy
        proxyUrl: 'http://my-machine:9000/',

推荐阅读