Wil*_*och 5 ajax jquery cucumber capybara phantomjs
我很难绕过这个问题,所以任何帮助都将不胜感激.
我想要做的就是在我的项目上测试一个简单的基于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
Run Code Online (Sandbox Code Playgroud)
测试:
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
Run Code Online (Sandbox Code Playgroud)
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.')
})
Run Code Online (Sandbox Code Playgroud)
好吧,所以这个特定的测试运行得很好,直到我们到达我们应该将用户重定向到欢迎屏幕的程度.我曾尝试一切我所能,看看发生了什么事情在里面onSuccess,onFailure回调,但无济于事.这就像代码甚至没有被执行.
我只是从测试运行中得到以下输出:
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'
Run Code Online (Sandbox Code Playgroud)
如果我提出异常并不重要,它就不会被提起.console.log()回调中的调用也没有.
有没有人见过这个?如果是这样,有解决方法吗?如果您需要更多信息,请问,我将非常乐意提供.
小智 0
根据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
Run Code Online (Sandbox Code Playgroud)
从那里,您只需将其加载到您的测试框架中;对于 Rspec,这可以在 spec/config 文件中完成,也可以通过简单地将一些代码添加到模块文件的末尾来完成:
RSpec.configure do |config|
config.include WaitForAjax, type: :feature
end
Run Code Online (Sandbox Code Playgroud)
确保spec/support/**/*.rb在您的配置文件中包含 require ,但您可能无论如何都应该这样做。
http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara
当然,根据上面的博客文章,如果您只是寻找欢迎页面独有的选择器,则可能根本不需要,也可能根本不需要,具体取决于您构建页面的方式。