连接拒绝在Docker中运行selenium的错误

当尝试dockeriseseleniumEnd2Endtesting使用seleniumdocker图像'selenium/ standalone'我得到错误:错误检索从selenium服务器的新会话连接被拒绝! selenium服务器是否启动? seleniumselenium服务器启动根据控制台输出..任何想法?

FROM selenium/standalone-chrome USER root # installing node RUN apt-get update RUN apt-get install -y curl RUN curl -sL https://deb.nodesource.com/setup_7.x | bash RUN apt-get install -y nodejs RUN node -v RUN npm -v # Installing Yarn #RUN rm -r /usr/local/bin/yarn RUN npm install -g -y yarn ENV PATH $PATH:/usr/local/bin/yarn #copying files WORKDIR /app COPY . . # debug RUN ls -alh . #installing yarn RUN yarn install EXPOSE 4444 RUN yarn CMD yarn test 

问题是你解决这个问题的方法。 看到你从Selenium浏览器的Selenium selenium/standalone-chromeinheritance你的图像。 现在是这张图片你正在添加你的testing,并指定CMD来运行testing。

当您构build并启动此映像时,您不会获得任何浏览器,因为您已经覆盖了CMD来运行testing。 当我们在docker中构build时,我们将依赖服务放在不同的容器中。 在大多数情况下,每个容器最好运行1个服务/进程。 在你的情况下,运行testing时,浏览器服务器进程丢失,所以这是拒绝连接的原因。

所以你需要在这里运行两个容器。 一个selenium/standalone-chrome和一个为您的testing。

此外,您的图像应该inheritance节点:而不是从selenium铬图像。 在构build图像时,不应该有node -vnpm -v命令。 他们在最终图像中创build额外的图层

 FROM node:7 USER root # installing node RUN apt-get update && apt-get install -y curl # Installing Yarn RUN npm install -g -y yarn ENV PATH $PATH:/usr/local/bin/yarn #copying files WORKDIR /app COPY . . # debug #installing yarn RUN yarn install RUN yarn CMD yarn test 

现在您需要创build一个docker-compose文件来运行同时具有testing和chrome的组合

 version: '3' services: chrome: image: selenium/standalone-chrome tests: build: . depends_on: - chrome 

安装docker-compose并运行docker-compose up命令来运行上述组合。 另外在你的testing中,确保使用URL为http://chrome:4444/wd/hub并使用Remote webdriver而不是本地驱动程序。

我使用selenium/节点铬图像,但解决它是确保我的chromedriver + selenium服务器+守夜设置为最新版本在我的package.json