Capybara click_button 可以工作,但如何评估 JSON 响应?
Soo,如果 Capybara 可以与 DOM 交互,并且我的应用程序通过 AJAX 提交表单并返回一些 JSON,那么 Capybara 能看到吗?我没有找到任何方法来获取 Capybara API 中的 AJAX 响应。
这是我正在做的一个例子:
# Cucumber step_definition
Then(/^I should be able to create a household$/) do
click_link 'Next'
page.should have_selector 'form#household-form'
fill_in 'Name', with: name = Faker::Name.last_name
click_button 'Create'
page.wait_until do
page.evaluate_script('$.active') == 0 # really awesome hack to wait for ajax
end
@user.reload.households.first.name.should eq name
end
我不想再次访问数据库来测试结果,而是做类似的事情:
xhr.response_data.should be_json
验证我的 json 响应是否是我认为应该的。
我只是在这里进行试验,试图适应 Cucumber 和 Capybara。
Soo, if Capybara can interface with the DOM and my app makes a form submit via AJAX and returns some JSON, can Capybara see that at all? I'm not finding any way to get to the AJAX response in the Capybara API.
Here's an example of kinda what I'm doing:
# Cucumber step_definition
Then(/^I should be able to create a household$/) do
click_link 'Next'
page.should have_selector 'form#household-form'
fill_in 'Name', with: name = Faker::Name.last_name
click_button 'Create'
page.wait_until do
page.evaluate_script('$.active') == 0 # really awesome hack to wait for ajax
end
@user.reload.households.first.name.should eq name
end
I'd rather not hit the database again to test the result, but instead do something like:
xhr.response_data.should be_json
Verifying that my json response is what I thought it should be.
I'm just experimenting here, trying to get used to Cucumber and Capybara.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该通过使用
@javascript
来标记您的场景/功能作为 JavaScript 功能运行:这将告诉 Cucumber 运行 selenium-webdriver gem,它将启动一个真正的浏览器(Firefox )并在该浏览器中运行测试。浏览器当然会评估 JavaScript,然后 Capybara 将能够看到它输出的内容。
You should mark your scenario / feature to run as a JavaScript feature by tagging it with
@javascript
:What this will do is to tell Cucumber to run the selenium-webdriver gem which will launch a real browser (Firefox) and run the test inside that browser. The browser will of course evaluate the JavaScript and then Capybara will be able to see what it outputs.