Ruby on rails 使用rspec、capybara和poltergeist的测试返回空html和空屏幕截图
所以我已经为此挣扎了很长一段时间了,我似乎不知道我们到底出了什么问题,也找不到太多可能导致这个问题的原因 我对Ruby和Rails以及测试/行为驱动的开发相对较新,我正试图通过Poltergeist使用PhantomJS编写一些验收(浏览器)测试,使用Rspec和Capybara。我相信有些人也把这种集成测试称为集成测试(它们可能是从某种角度来看的),但这完全是另一种讨论 我有一个非常简单的功能,我不能做我想做的事情:Ruby on rails 使用rspec、capybara和poltergeist的测试返回空html和空屏幕截图,ruby-on-rails,rspec,capybara,phantomjs,poltergeist,Ruby On Rails,Rspec,Capybara,Phantomjs,Poltergeist,所以我已经为此挣扎了很长一段时间了,我似乎不知道我们到底出了什么问题,也找不到太多可能导致这个问题的原因 我对Ruby和Rails以及测试/行为驱动的开发相对较新,我正试图通过Poltergeist使用PhantomJS编写一些验收(浏览器)测试,使用Rspec和Capybara。我相信有些人也把这种集成测试称为集成测试(它们可能是从某种角度来看的),但这完全是另一种讨论 我有一个非常简单的功能,我不能做我想做的事情: require 'feature_helper' feature 'Log
require 'feature_helper'
feature 'Logging in', :js => true do
scenario 'with incorrect credentials' do
visit '/login'
puts page.html
save_and_open_page
page.driver.render('_screenshot.png', :full => true)
page.html.should have_selector("title", :text => "hi")
end
end
所以。简单,对。它只需转到/login
并将HTML内容扔给我,我还想使用保存和打开页面
查看页面,我想让它截图。我添加了一个简单的应该有_选择器
,以使测试失败,从而尝试获得更多反馈
myfeature\u helper.rb的相对内容:
require 'spec_helper'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
:debug => true,
:inspector => true
})
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
FakeWeb.allow_net_connect = %r[^https?://(127.0.0.1|localhost)] # allow phantomjs/poltergeist requests
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.before :each do
# Set the hostname to something with test
@host = "test.iome:3003"
host! @host
Capybara.default_host = Capybara.app_host = "http://#{@host}/"
Capybara.server_port = 3003
Capybara.reset_sessions!
# Start the database cleaner
config.use_transactional_fixtures = false
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'pry'
require 'fakeweb'
FakeWeb.allow_net_connect = false
还有我的spec\u helper.rb
:
require 'spec_helper'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
:debug => true,
:inspector => true
})
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
FakeWeb.allow_net_connect = %r[^https?://(127.0.0.1|localhost)] # allow phantomjs/poltergeist requests
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.before :each do
# Set the hostname to something with test
@host = "test.iome:3003"
host! @host
Capybara.default_host = Capybara.app_host = "http://#{@host}/"
Capybara.server_port = 3003
Capybara.reset_sessions!
# Start the database cleaner
config.use_transactional_fixtures = false
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'pry'
require 'fakeweb'
FakeWeb.allow_net_connect = false
一切都相当简单
现在,在我的控制台中,我看到以下内容:
{"name"=>"visit", "args"=>["http://test.iome:3003//login"]}
{"response"=>{"status"=>"fail"}}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
<html><head></head><body></body></html>
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
{"name"=>"render", "args"=>["_screenshot.png", true]}
{"response"=>true}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
before(:each) do
@original_host = Capybara.app_host
Capybara.app_host = 'http://does-not-exist.lvh.me'
visit '/login'
end
after(:each) do
Capybara.app_host = @original_host
end
{“name”=>“visit”,“args”=>[“http://test.iome:3003//login"]}
{“响应”=>{“状态”=>“失败”}
{“name”=>“body”,“args”=>[]}
{“响应”=>“”
{“name”=>“body”,“args”=>[]}
{“响应”=>“”
{“name”=>“render”,“args”=>[“\u screenshot.png”,true]}
{“响应”=>true}
{“name”=>“body”,“args”=>[]}
{“响应”=>“”
另外,屏幕截图只是一个白色的空白页面。当我跟踪我的log/test.log
文件时,我看不到正在执行请求。我已尝试将方法visit
更改为get
,这将发出请求,但不会更改任何结果
我完全不知道这可能是什么,这相当令人沮丧:(
关于版本的最终信息:
- rspec 2.10.0
- 水豚1.1.4
- 恶鬼1.0.3
- ruby 1.8.7
- 轨道3.2.13
不幸的是,我们仍然使用ruby 1.8.7,但正在努力提高该版本。不过,我认为这不应该影响测试
任何帮助都将不胜感激!因此,最终我从一位同事那里得到了帮助,我们设法修复了它。我们为此使用了lvh.me
域,因为对该域的任何请求都将在localhost中解决,允许您毫无问题地使用子域。您可能还可以使用hostname.127.0.0.1.xip.io
。
我们的spec\u helper.rb
现在看起来像这样:
# Use capybara in combination with poltergeist for integration tests
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'rack_session_access/capybara'
Capybara.default_driver = :poltergeist
Capybara.always_include_port = true
Capybara.app_host = 'http://application-test.lvh.me' # Any lvh.me domain resolves to localhost
Capybara.default_wait_time = 8 # How long capybara should look for html elements
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = 'spec/vcr_cassettes'
config.hook_into :fakeweb
config.ignore_localhost = true
config.configure_rspec_metadata!
config.ignore_hosts 'codeclimate.com'
end
require 'fakeweb'
FakeWeb.allow_net_connect = false
因为我们挂接VCR以记录第一次运行集成测试期间发出的任何请求,所以您的所有集成测试或功能都应包含以下代码:
before(:all) do
FakeWeb.allow_net_connect = true
end
after(:all) do
FakeWeb.allow_net_connect = false
end
如果要在规格期间更改子域,可以使用以下命令:
{"name"=>"visit", "args"=>["http://test.iome:3003//login"]}
{"response"=>{"status"=>"fail"}}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
<html><head></head><body></body></html>
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
{"name"=>"render", "args"=>["_screenshot.png", true]}
{"response"=>true}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}
before(:each) do
@original_host = Capybara.app_host
Capybara.app_host = 'http://does-not-exist.lvh.me'
visit '/login'
end
after(:each) do
Capybara.app_host = @original_host
end
现在可以使用页面制作屏幕截图。在规范期间保存屏幕截图。希望这有帮助。如果您只需进入浏览器并键入URL(即http://test.iome:3003/
)您得到响应了吗?当我运行测试时:是的。不过我现在似乎得到了更多。这是新的一天,所以新的事情正在发生:)我相信用于测试的端口仍在使用中,导致应用程序在启动时超时。nginx似乎还在运行。page.html
属性现在也有了内容。然而,虽然屏幕截图文件是制作出来的,但以前是空白的,但现在根本不创建文件。此外,我还向登录所需的数据库中添加了一些信息,但这些信息对应用程序不可用。将让您知道更新。您可以为控制器操作添加代码吗?我遇到了相同的问题,希望进行比较!:D