使用 Capybara 测试纯 JavaScript 应用程序

发布于 2024-12-10 12:37:26 字数 1885 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-12-17 12:37:26

此处乔纳斯·尼克拉斯

您需要 require 'capybara/rspec' 并设置 :type =>; :请求。
请参阅 Capybara 自述文件中的“将 Capybara 与 RSpec 结合使用”部分。
/乔纳斯

这里是 Github 上的一个工作示例。

Here is the original answer from Jonas Nicklas.

You need to require 'capybara/rspec' and set :type => :request.
See the Capybara README section on "Using Capybara with RSpec".
/Jonas

Here is a working example on Github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文