Capybara click_button 可以工作,但如何评估 JSON 响应?

发布于 2024-12-25 13:38:21 字数 725 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

薄荷港 2025-01-01 13:38:21

您应该通过使用 @javascript 来标记您的场景/功能作为 JavaScript 功能运行:

@javascript
Scenario: Title goes here

这将告诉 Cucumber 运行 selenium-webdriver gem,它将启动一个真正的浏览器(Firefox )并在该浏览器中运行测试。浏览器当然会评估 JavaScript,然后 Capybara 将能够看到它输出的内容。

You should mark your scenario / feature to run as a JavaScript feature by tagging it with @javascript:

@javascript
Scenario: Title goes here

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.

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