我在rails 5应用程序上有一个ruby,其中我使用capybara
和poltergeist
编写了一些功能规范。他们自己运行良好 - 然而,当我在一个码头工人的容器内运行它们时,它们会非常壮观地爆炸。对于docker而言,我是绿色的,我在找出如何自行调试时有点迷失。我发现一些资源利用selenium
但未与capybara
一起使用。
以下是一些配置文件:
Dockerfile
FROM ruby:2.4.1
ENV DIR=/app \
BUNDLE_JOBS=4 \
PHANTOMJS=phantomjs-2.1.1-linux-x86_64
RUN apt-get update -qq && apt-get upgrade -y && apt-get install -y build-essential libpq-dev nodejs
ADD https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOMJS.tar.bz2 /
RUN mv ./$PHANTOMJS/bin/phantomjs /usr/local/bin
RUN rm -rf ./$PHANTOMJS
RUN phantomjs --version
RUN mkdir $DIR
WORKDIR $DIR
ADD Gemfile $DIR/Gemfile
ADD Gemfile.lock $DIR/Gemfile.lock
RUN bundle install
ADD . $DIR
EXPOSE 3000
ADD docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD bundle exec rails s -p 3000 -b 0.0.0.0
搬运工-compose.yml
version: "2"
services:
db:
image: postgres
env_file:
- docker.env
app:
build: .
links:
- db:db
ports:
- "3000:3000"
depends_on:
- db
env_file:
- docker.env
volumes:
- .:/app
command: bundle exec rails s -p 3000 -b '0.0.0.0'
rails_helper.rb
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(
app,
debug: true,
js_errors: true,
host: docker_ip)
end
# Capybara.javascript_driver = :poltergeist
Capybara.javascript_driver = :poltergeist_debug
Capybara.app_host = "http://#{docker_ip}:3000"
Capybara.server_host = '0.0.0.0'
Capybara.server_port = '3000'
每当我使用docker-compose run --rm -e "RAILS_ENV=test" app rspec
在我的docker容器中运行我的功能规范时,我通常会出现以下错误:
36) BookPolicy show? permits non-admins
Failure/Error:
TCPServer.open(custom_host || HOST, port || 0).tap do |server|
@port = server.addr[1]
@host = server.addr[2]
end
Errno::EADDRNOTAVAIL:
Cannot assign requested address - bind(2) for "172.19.0.1" port 0
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/web_socket_server.rb:34:in `initialize'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/web_socket_server.rb:34:in `open'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/web_socket_server.rb:34:in `start_server'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/web_socket_server.rb:26:in `initialize'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/server.rb:25:in `new'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/server.rb:25:in `start'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/server.rb:9:in `initialize'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/driver.rb:40:in `new'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/driver.rb:40:in `server'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/driver.rb:25:in `browser'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/driver.rb:184:in `reset!'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara/session.rb:127:in `reset!'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara.rb:314:in `block in reset_sessions!'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara.rb:314:in `reverse_each'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara.rb:314:in `reset_sessions!'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara/rspec.rb:22:in `block (2 levels) in <top (required)>'
那么我做错了什么?任何帮助表示赞赏。
更新#1:我删除了设置poltergeist驱动程序的host
配置。新的rails_helper.rb
配置如下所示:
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(
app,
debug: true,
js_errors: true)
end
我现在收到以下错误:
1)管理员可以通过通道页面编辑链接更新通道页面翻译 失败/错误:访问book_cover_path(书)
Capybara::Poltergeist::StatusFailError:
Request to 'http://172.19.0.1:36827/books/the-heart-is-deceitful-above-all-things/cover' failed to reach server, check DNS and/or server status
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/browser.rb:377:in `command'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/browser.rb:36:in `visit'
# /usr/local/bundle/gems/poltergeist-1.15.0/lib/capybara/poltergeist/driver.rb:97:in `visit'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara/session.rb:269:in `visit'
# /usr/local/bundle/gems/capybara-2.14.4/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# ./spec/features/passage_mode_spec.rb:13:in `block (2 levels) in <top (required)>'
...所以现在phantomjs无法访问服务器,即使它使用了正确的IP地址和我在Dockerfile
和docker-compose.yml
中公开的端口。