尝试通过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. 设置我的Internet配置文件:
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.version:unknown的错误,我尝试了上面的步骤3(手动安装Chrome驱动程序(,所以我不确定这个错误是否会再出现在Chrome驱动程序上,我认为这可能设置正确。

完全删除步骤3

selenium//独立chrome等官方selenium docker图像已安装浏览器和相应的驱动程序。

更新步骤4的配置

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

platform; version;

还注意到代理配置

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

基于以上内容,您声明代理已安装在本地机器上,但当您将驱动程序请求发送到chrome节点时,chrome浏览器将转换为代理已在localhost:9000上实现,localhost:9000是它安装的平台(,即Docker容器(。

您需要使用托管您的代理和Docker容器的本地机器的ip(即proxyUrl:'http://192.168.111.222:9000/(

你也可以用你选择的域名(例如我的机器(更新它,但你需要用更新你的Docker命令

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

所以你的docker运行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/',

最新更新