如何将 Capybara 配置为在 dockerized Selenium Grid 中进行 运行 测试?
How to configure Capybara to run tests in a dockerized Selenium Grid?
我有一套测试想在 dockerized Selenium Grid 中执行。测试是使用 RSpec 和 Capybara 在 Ruby 中编写的。同样值得注意的是:我正在使用 dinghy 作为 docker-machine.
的包装器
几周前,我构建了一个 proof of concept,但使用 Nightwatch 而不是 RSpec+Capybara。这很好用,但事实证明让 Capybara 使用 dockerized Selenium Grid 很困难。
我试过很多配置都没有成功。我认为最接近成功配置的是以下...
# docker-compose.yml
web:
image: web:latest # built on my machine
environment:
VIRTUAL_HOST: app.under.test
rspec:
build: .
dockerfile: Dockerfile
environment:
APP_HOST: app.under.test
volumes:
- .:/usr/src/app
links:
- web
- hub
hub:
image: selenium/hub:latest
environment:
VIRTUAL_HOST: selenium.hub.docker
ports:
- 4444:4444
node-firefox:
image: selenium/node-firefox:latest
environment:
VIRTUAL_HOST: firefox.docker
links:
- hub
node-chrome:
image: selenium/node-chrome:latest
environment:
VIRTUAL_HOST: chrome.docker
links:
- hub
# Dockerfile for the rspec image
FROM instructure/ruby-passenger:2.3
USER root
ENV APP_HOME /usr/src/app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile Gemfile.lock $APP_HOME/
RUN chown -R docker:docker $APP_HOME
USER docker
RUN bundle install --quiet --jobs 8
USER root
COPY . $APP_HOME
RUN chown -R docker:docker $APP_HOME
USER docker
# spec/example_test.rb:
require 'rspec'
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
RSpec.configure do |config|
config.include Capybara::DSL
end
def setup
url = 'http://selenium.hub.docker'
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
Capybara.app_host = ENV['APP_HOST']
Capybara.register_driver :remote_browser do |app|
Capybara::Selenium::Driver.new(
app,
:browser => :remote,
url: url,
desired_capabilities: capabilities
)
end
Capybara.default_driver = :remote_browser
Capybara.javascript_driver = :remote_browser
end
describe 'This is an example' do
it 'and it works' do
setup
visit '/'
expect(page.title).to eq 'Home'
end
end
但是这个 ^ 配置在开始测试时会产生以下错误:
1) This is an example and it works
Failure/Error: visit '/'
Selenium::WebDriver::Error::WebDriverError:
unexpected response, code=200, content-type="text/html"
<html><head><title>Selenium Grid2.0 help</title></head><body>You are using grid 2.52.0<br>Find help on the official selenium wiki : <a href='https://github.com/SeleniumHQ/selenium/wiki/Grid2' >more help here</a><br>default monitoring page : <a href='/grid/console' >console</a></body></html>
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:85:in `create_response'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/default.rb:90:in `request'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:645:in `raw_execute'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:123:in `create_session'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:87:in `initialize'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/common/driver.rb:59:in `new'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/common/driver.rb:59:in `for'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver.rb:86:in `for'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:13:in `browser'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:45:in `visit'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/session.rb:232:in `visit'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/example_test.rb:31:in `block (2 levels) in <top (required)>'
有什么想法吗?我错过了什么?
更新
知道了!
def setup
url = 'http://selenium.hub.docker/wd/hub'
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
Capybara.app_host = "http://#{ENV['APP_HOST']}"
Capybara.run_server = false
Capybara.register_driver :remote_browser do |app|
Capybara::Selenium::Driver.new(
app,
:browser => :remote,
url: url,
desired_capabilities: capabilities
)
end
Capybara.default_driver = :remote_browser
Capybara.javascript_driver = :remote_browser
end
密钥---除了 Capybara.run_server = false
并在 app_host---was 前添加 http://
并在 url 上指定 /wd/hub
。
工作解决方案here.
对于水豚设置,您需要担心一些事情 -
1 - 水豚连接到浏览器的地方 - 在大多数人的情况下,这是本地的,但您使用的是远程,并且您似乎已正确配置
2 - 浏览器尝试连接的位置 - 基础 url 由 Capybara.app_host 设置 - 所以 Capybara.app_host = "http://<ip/domain 命名应用程序将在 运行ning on> - 你将其设置为 ENV['APP_HOST'] 设置为 app.under.test - 我假设这是一个解析为可访问的 ip/domain 名称水豚机器上的界面正在 运行 上?
3 - Capybara 绑定正在测试的应用程序 - 这是通过 Capybara.server_host 和 Capybara.server_port 设置的 - server_host 默认为 127.0.0.1 并且端口是随机选择的。这很可能是您的问题所在,因为机器上的 127.0.0.1 运行ning Capybara 通常无法从其他机器访问 您需要将其更改为与 app.under.test 匹配的外部可用 ip来自#2。如果您的防火墙配置需要,您可能还需要设置端口。
我有一套测试想在 dockerized Selenium Grid 中执行。测试是使用 RSpec 和 Capybara 在 Ruby 中编写的。同样值得注意的是:我正在使用 dinghy 作为 docker-machine.
的包装器几周前,我构建了一个 proof of concept,但使用 Nightwatch 而不是 RSpec+Capybara。这很好用,但事实证明让 Capybara 使用 dockerized Selenium Grid 很困难。
我试过很多配置都没有成功。我认为最接近成功配置的是以下...
# docker-compose.yml
web:
image: web:latest # built on my machine
environment:
VIRTUAL_HOST: app.under.test
rspec:
build: .
dockerfile: Dockerfile
environment:
APP_HOST: app.under.test
volumes:
- .:/usr/src/app
links:
- web
- hub
hub:
image: selenium/hub:latest
environment:
VIRTUAL_HOST: selenium.hub.docker
ports:
- 4444:4444
node-firefox:
image: selenium/node-firefox:latest
environment:
VIRTUAL_HOST: firefox.docker
links:
- hub
node-chrome:
image: selenium/node-chrome:latest
environment:
VIRTUAL_HOST: chrome.docker
links:
- hub
# Dockerfile for the rspec image
FROM instructure/ruby-passenger:2.3
USER root
ENV APP_HOME /usr/src/app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile Gemfile.lock $APP_HOME/
RUN chown -R docker:docker $APP_HOME
USER docker
RUN bundle install --quiet --jobs 8
USER root
COPY . $APP_HOME
RUN chown -R docker:docker $APP_HOME
USER docker
# spec/example_test.rb:
require 'rspec'
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
RSpec.configure do |config|
config.include Capybara::DSL
end
def setup
url = 'http://selenium.hub.docker'
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
Capybara.app_host = ENV['APP_HOST']
Capybara.register_driver :remote_browser do |app|
Capybara::Selenium::Driver.new(
app,
:browser => :remote,
url: url,
desired_capabilities: capabilities
)
end
Capybara.default_driver = :remote_browser
Capybara.javascript_driver = :remote_browser
end
describe 'This is an example' do
it 'and it works' do
setup
visit '/'
expect(page.title).to eq 'Home'
end
end
但是这个 ^ 配置在开始测试时会产生以下错误:
1) This is an example and it works
Failure/Error: visit '/'
Selenium::WebDriver::Error::WebDriverError:
unexpected response, code=200, content-type="text/html"
<html><head><title>Selenium Grid2.0 help</title></head><body>You are using grid 2.52.0<br>Find help on the official selenium wiki : <a href='https://github.com/SeleniumHQ/selenium/wiki/Grid2' >more help here</a><br>default monitoring page : <a href='/grid/console' >console</a></body></html>
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:85:in `create_response'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/default.rb:90:in `request'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:645:in `raw_execute'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:123:in `create_session'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/remote/bridge.rb:87:in `initialize'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/common/driver.rb:59:in `new'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver/common/driver.rb:59:in `for'
# /home/docker/.gem/ruby/2.3.0/gems/selenium-webdriver-2.52.0/lib/selenium/webdriver.rb:86:in `for'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:13:in `browser'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:45:in `visit'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/session.rb:232:in `visit'
# /home/docker/.gem/ruby/2.3.0/gems/capybara-2.6.2/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/example_test.rb:31:in `block (2 levels) in <top (required)>'
有什么想法吗?我错过了什么?
更新
知道了!
def setup
url = 'http://selenium.hub.docker/wd/hub'
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
Capybara.app_host = "http://#{ENV['APP_HOST']}"
Capybara.run_server = false
Capybara.register_driver :remote_browser do |app|
Capybara::Selenium::Driver.new(
app,
:browser => :remote,
url: url,
desired_capabilities: capabilities
)
end
Capybara.default_driver = :remote_browser
Capybara.javascript_driver = :remote_browser
end
密钥---除了 Capybara.run_server = false
并在 app_host---was 前添加 http://
并在 url 上指定 /wd/hub
。
工作解决方案here.
对于水豚设置,您需要担心一些事情 -
1 - 水豚连接到浏览器的地方 - 在大多数人的情况下,这是本地的,但您使用的是远程,并且您似乎已正确配置
2 - 浏览器尝试连接的位置 - 基础 url 由 Capybara.app_host 设置 - 所以 Capybara.app_host = "http://<ip/domain 命名应用程序将在 运行ning on> - 你将其设置为 ENV['APP_HOST'] 设置为 app.under.test - 我假设这是一个解析为可访问的 ip/domain 名称水豚机器上的界面正在 运行 上?
3 - Capybara 绑定正在测试的应用程序 - 这是通过 Capybara.server_host 和 Capybara.server_port 设置的 - server_host 默认为 127.0.0.1 并且端口是随机选择的。这很可能是您的问题所在,因为机器上的 127.0.0.1 运行ning Capybara 通常无法从其他机器访问 您需要将其更改为与 app.under.test 匹配的外部可用 ip来自#2。如果您的防火墙配置需要,您可能还需要设置端口。