2013-07-09 22 views
5

所以我一直在努力与此相当长一段时间了,我似乎无法弄清楚我们出了什么问题,并且找不到什么可能会导致这个问题。使用rspec,水豚和poltergeist的测试返回空html和空屏幕截图

我对Ruby和Rails以及测试/行为驱动开发相对较新,并试图使用Rspec和Capybara使用PhantomJS通过Poltergeist编写一些接受(浏览器)测试。我相信有些人也称这种集成测试(他们可能从某种角度来看),但这是另外一个讨论。

我有一个非常简单的功能,我不能做什么,我想:

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内容,以及我想看到使用save_and_open_page的页面,我希望它采取截图。我添加了一个简单的should have_selector为了让测试失败,试图获得更多的反馈。

feature_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 

,也是我spec_helper.rb

# 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>"} 

此外,截图仅仅是一个白色和空白页。当我尾随我的log/test.log文件时,我看不到请求正在执行。我已经尝试将方法visit更改为get,并且会提出请求,但不会更改任何结果。

我已经完全用完的东西,这可能是想法,它是相当令人沮丧,:(

最终信息则有关版本:

  • rspec的2.10.0
  • 1.1.4水豚
  • 骚灵1.0.3
  • 红宝石1.8.7
  • 轨3.2.13

不幸的是,我们仍然在红宝石1.8.7,但正在努力使该版本上涨。不过,我认为这不应该影响测试。

任何帮助将不胜感激!

+0

如果你只是去你的浏览器并输入URL(即'http://test.iome:3003 /'),你会得到一个回应? –

+0

当我运行测试时:是的。不过,我现在似乎还多了一点点。这是新的一天,所以新的事情正在发生:)我相信用于测试的端口仍在使用中,导致应用程序在引导时超时。看起来nginx还在运行。 “page.html”属性现在也有内容。但是,虽然屏幕截图文件已经制作完成,但之前空白,但它现在根本不会创建文件。另外,我向登录所需的数据库添加了一些信息,但这些信息对应用程序不可用。会让你知道更新。 – Mark

+0

您可以添加控制器操作的代码吗?我遇到同样的问题,想要比较! :d –

回答

1

所以最终我得到了一位同事的帮助,并且我们设法解决了这个问题。我们为此使用了lvh.me域,因为对该域的任何请求都将在本地主机中解析,从而使您可以毫无问题地使用子域。您也可以使用hostname.127.0.0.1.xip.io

我们spec_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 

如果你想在你的规格更改子域,您可以使用以下命令:

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 

制作截图现在可以在规格期间使用page.save_screenshot完成。希望这可以帮助。