Docker中的Capybara无头镀铬返回DevToolsActivePort文件不存在

时间:2018-05-30 17:20:47

标签: ruby-on-rails selenium capybara google-chrome-headless

我试图配置系统测试以在硒中使用无头铬。我有以下水豚配置:

# spec/support/capybara.rb

Capybara.server = :puma, { Silent: true }

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :rack_test
  end

  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless, screen_size: [1400, 1400]
  end
end

和以下Dockerfile(没有数据库,因为我正在使用主机):

FROM ruby:2.5.1

RUN apt-get update
RUN apt-get install -y wget git

# Node
RUN curl -sL https://deb.nodesource.com/setup_9.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update

# Essentials
RUN apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn unzip

# Chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get -y install google-chrome-stable

# Chromedriver
RUN wget -q https://chromedriver.storage.googleapis.com/2.39/chromedriver_linux64.zip
RUN unzip chromedriver_linux64.zip -d /usr/local/bin
RUN rm -f chromedriver_linux64.zip

RUN apt-get clean

我已经在网上关注了如何设置headless_chrome测试的几个来源,但是他们都恢复了上述配置。试图运行capybara它显示以下错误,我似乎无法成功调试它。

Selenium::WebDriver::Error::UnknownError:
    unknown error: DevToolsActivePort file doesn't exist
        (Driver info: chromedriver=2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf),platform=Linux 4.16.11-1-ARCH x86_64)

上面的docker文件包含最新的chrome和chromedriver版本,分别为67和2.39。我已尝试旧版本以及相同的上述错误,例如66和2.38,符合http://chromedriver.chromium.org/downloads上的版本支持。

之前有没有人见过这个错误?

1 个答案:

答案 0 :(得分:17)

似乎capybara的默认selenium_chrome_headless设置不足以在docker容器中运行。我通过将spec / support / capybara.rb设置更改为以下内容来解决此问题:

# spec/support/capybara.rb

# Setup chrome headless driver
Capybara.server = :puma, { Silent: true }

Capybara.register_driver :chrome_headless do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new

  options.add_argument('--headless')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')
  options.add_argument('--window-size=1400,1400')

  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :chrome_headless

# Setup rspec
RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :rack_test
  end

  config.before(:each, type: :system, js: true) do
    driven_by :chrome_headless
  end
end

特别是" - 禁用-dev-shm-usage"不应该忘记,因为它解决了码头工人中有限的资源问题,如:https://github.com/GoogleChrome/puppeteer/issues/1834

修改 我还没有对上面的Dockerfile进行任何更改