我没有让capybara使用我的模块化sinatra应用程序。我创建了一个小型测试应用程序并遇到了同样的问题。我总是得到504超时错误,我认为应用程序并没有被Capybara真正加载,无论在accept_helper中是否为Capybara.app = Testapp
行。
浏览器Firefox在测试运行时打开,但不会转到任何网址,并且始终显示空白网站。
当我使用rspec进行相同的测试时,它可以工作(第二个)。
我做错了什么?
我的设置:
OS: Win 7 Professional 64-bit
Ruby version: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
Firefox version: 40.0.3
gem versions
- sinatra (1.4.6)
- rspec (3.3.0)
- capybara (2.7.1)
- selenium-webdriver (2.53.4)
- thin (1.5.1)
- rack (1.6.4)
- rack-test (0.6.3)
应用结构:
test_sinatra_capybara
|
|- testapp.rb
|- spec
| |- spec_helper.rb
| |- acceptance_helper.rb
| |- testapp_spec.rb
|- views
| |- index.slim
testapp.rb
require 'sinatra'
require 'sinatra/base'
require 'slim'
class Testapp < Sinatra::Base
get '/' do
slim :index
end
end
index.slim
#test-id
| Willkommen
spec_helper.rb
require 'rspec'
require 'rack/test'
require_relative '../testapp'
Testapp.environment = :test
module RSpecMixin
include Rack::Test::Methods
def app() Testapp end
end
RSpec.configure do |config|
config.include RSpecMixin
# Use color in STDOUT
config.color = true
# Use the specified formatter
config.formatter = :documentation
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
acceptance_helper.rb
require File.dirname(__FILE__) + '/spec_helper'
require 'capybara'
require 'capybara/rspec'
Capybara.default_driver = :selenium
Capybara.app = Testapp
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
end
testapp_spec.rb
require File.dirname(__FILE__) + '/acceptance_helper'
describe 'URLs that require login' do
it "start page capybara" do
# I tried both
visit '/'
# visit 'http:localhost:4567'
page.should have_content("Willkommen")
end
it "start page rspec" do
get "/"
expect(last_response.body).to include("Willkommen")
end
end
正如我所说,上次测试正在进行中:
Finished in 12.91 seconds (files took 2.54 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/testapp_spec.rb:4 # URLs that require login start page capybara
第一次测试时出错:
Selenium::WebDriver::Error::WebDriverError:
unexpected response, code=504, content-type="text/html"
答案 0 :(得分:0)
运行确切的代码发布对我来说很好,我猜这意味着两件事之一。您正在使用的Firefox版本已过时(请确保您使用的是47.0.1)或者您使用的代理服务器或防火墙干扰了对127.0.0.1的请求:端口取代。默认的Capybara配置在127.0.0.1:<random port&gt;上启动服务器。处理被测应用程序的请求。如果这些连接在其他地方被阻止或代理,显然会阻止事情正常工作。如果您需要绑定到不同的接口或设置固定端口以绕过企业防火墙/代理限制,您可以通过设置Capybara.server_host和Capybara.server_port来实现此目的