问题
Im trying to configure system tests to work with headless chrome in selenium. I have the following capybara configuration:
# 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
and the following Dockerfile (no database because i'm using the host for this):
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
I've followed several sources online on how to setup headless_chrome testing, however they all revert to the above configuration. Trying to run capybara it shows the following error and I can't seem to succesfully debug it.
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)
The above docker file contains the latest chrome and chromedriver versions, respectively 67 and 2.39. I've tried older versions aswell with the same above error, eg 66 and 2.38, complying to the version support on http://chromedriver.chromium.org/downloads.
Has anyone seen this error before?
回答1:
It seems like the default selenium_chrome_headless settings of capybara aren't sufficient for running in a docker container. I have solved it by changing my spec/support/capybara.rb settings to the following:
# 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
Especially "--disable-dev-shm-usage" should not be forgotten, as it solves limited resource problems in docker, as noted in: https://github.com/GoogleChrome/puppeteer/issues/1834
Edit: I haven't made any changes to the above Dockerfile
来源:https://stackoverflow.com/questions/50610316/capybara-headless-chrome-in-docker-returns-devtoolsactiveport-file-doesnt-exist