使用 Capybara 测试纯 JavaScript 应用程序
我在使用 Sinatra 和 Capybara 时遇到一些问题。
我想测试一个纯 javascript 应用程序。它只是一个由 Sinatra 提供的简单的 index.html。
require "sinatra"
get "/" do
File.read("public/index.html")
end
举例来说,我想测试这段代码。
$("a.link").click(function(){
$(this).replaceWith("New String");
});
<a href="#link" class="link">Click me!</a>
然后测试看起来像这样。
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
问题是什么也没发生。根据Ryan Bates screencast,如果 js: true
添加到 describe
块中。
这是我的 spec_helper
文件。
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
以下是运行 rspec rspec/request_spec.rb 时的输出。
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
我在 Github 上创建了一个完整示例项目,可以在此处找到: https://github.com/oleander/capybara-js-fails
任何人都知道为什么失败?
I'm having some problems using Sinatra with Capybara.
I want to test a pure javascript application. It's just a plain index.html that is being served by Sinatra.
require "sinatra"
get "/" do
File.read("public/index.html")
end
Let's say for example that I want to test this code.
$("a.link").click(function(){
$(this).replaceWith("New String");
});
<a href="#link" class="link">Click me!</a>
Then the test would look something like this.
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
The problem is that nothing happens. According to Ryan Bates screencast Firefox should start and run the test if js: true
is added to the describe
block.
Here is my spec_helper
file.
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
Here is the output when running rspec rspec/request_spec.rb
.
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
I created an complete example project on Github that can be found here:
https://github.com/oleander/capybara-js-fails
Anyone knows why it fails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此处是 乔纳斯·尼克拉斯。
这里是 Github 上的一个工作示例。
Here is the original answer from Jonas Nicklas.
Here is a working example on Github.