在 Cucumber + Capybara + PhantomJS 在 Rails 中调试 jQuery Ajax

Posted

技术标签:

【中文标题】在 Cucumber + Capybara + PhantomJS 在 Rails 中调试 jQuery Ajax【英文标题】:Debugging jQuery Ajax in Cucumber + Capybara + PhantomJS in Rails 【发布时间】:2013-08-12 03:29:29 【问题描述】:

我很难解决这个问题,所以任何帮助都将不胜感激。

我只想在我的项目中测试一个简单的基于 Ajax 的注册表单。如果表单提交成功,您将被重定向到欢迎页面。如果不是,您会收到与每个违规字段相关的相应验证错误。

出于某种奇怪的原因,Capybara 没有遵循重定向。正在进行 Ajax 调用,我看到在数据库中注册了一个新帐户,但是根本没有调用 onSuccess 回调,或者重定向被忽略。

这是我正在尝试使用的内容(为简洁起见,我已经压缩了代码):

特点:

  Feature: Registration
    In order to obtain a new account
    As a prospective customer
    I must submit a valid registration form.

    @javascript
    Scenario: A valid registration attempt
      Given an account registration form
      When I complete the form with valid values
      Then I should be redirected to the welcome screen 

测试:

Given(/^an account registration form$/) do
  visit("/signup")
  assert current_path == "/signup"
end

When(/^I complete the form with valid values$/) do
  within("#signupForm") do
    fill_in("email",    :with => Faker::Internet.email)
    fill_in("name",     :with => Faker::Name.name)
    fill_in("password", :with => "11111111")

    click_link("signupFormSubmit")
  end
end

Then(/^I should be redirected to the welcome screen$/) do
  assert current_path == "/welcome"
end

JavaScript:

console.log('I am not yet inside you.')

$.post(url, form.serialize(), function(response) 
  // everything went well
  // let's redirect them to the given page
  window.location.replace(response.redirectUrl)
  console.log('I am inside you and it is good.')
, function(response) 
  // collect error responses from API
  // apply error hints to associated fields
  console.log('I am inside you and something went wrong.')
)

好的,所以这个特定的测试运行良好,直到我们应该将用户重定向到欢迎屏幕。我已经尽我所能查看onSuccessonFailure 回调中发生了什么,但无济于事。这就像代码甚至没有被执行。

我刚刚从测试运行中得到以下输出:

Then I should be redirected to the welcome screen # features/step_definitions/registration.rb:51
  Failed assertion, no message given. (MiniTest::Assertion)
  ./features/step_definitions/registration.rb:52:in `/^I should be redirected to the welcome screen$/'
  features/registration.feature:15:in `Then I should be redirected to the welcome screen'

如果我引发异常也没关系,它不会被拾取。在回调中也不会调用console.log()

有人看过吗?如果是这样,是否有解决方法?如果您需要更多信息,请直接询问,我很乐意提供。

【问题讨论】:

【参考方案1】:

根据robots at Thoughtbot 和 Coderwall 的人员,您可以使用辅助方法来执行此操作,放入spec/support。 他们将他们的模块命名为WaitForAjax

# spec/support/wait_for_ajax.rb
module WaitForAjax
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
end

从那里,您只需将其加载到您的测试框架中;对于 Rspec,这可以在 spec/config 文件中完成,也可以通过在模块文件的末尾添加一些代码来完成:

RSpec.configure do |config|
  config.include WaitForAjax, type: :feature
end

确保在您的配置文件中要求 spec/support/**/*.rb,但您可能还是应该这样做。

http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara

当然,根据上面的博客文章,这可能根本不需要也可能根本不需要,这取决于您如何构建页面,如果您只是寻找一个对您的欢迎页面唯一的选择器。

【讨论】:

以上是关于在 Cucumber + Capybara + PhantomJS 在 Rails 中调试 jQuery Ajax的主要内容,如果未能解决你的问题,请参考以下文章

Rails、Cucumber、Capybara:会话不持久

Cucumber 和 Capybara,单击非链接或按钮元素

在 Cucumber + Capybara + PhantomJS 在 Rails 中调试 jQuery Ajax

Cucumber, capybara and selenium - 无需按钮即可提交表单

使用 Cucumber 和 Capybara-Webkit 在 Rails 4 上进行 typeahead.js 测试失败

如何使用rails cucumber,rspec,capybara在视图(dhtml)中测试动态部分?