水豚选择单选按钮不起作用

Capybara Choose radio button not working

我可能已经这样做了 100 次,但我一直无法理解为什么它告诉我它找不到这个对象。这是我的 html:

PS 我已经确认我在正确的页面上,并且在我执行 save_and_open_screenshot

时可以看到顾问

HTML

<div class="panel">
  <%= form.label :advisor, class: "panel__label" %>
  <%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio advisor" %>
</div>

<div class="panel">
  <%= form.label :client, class: "panel__label" %>
  <%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio client" %>
</div>

测试

choose('advisor')

错误

Failure/Error: choose('advisor')

 Capybara::ElementNotFound:
   Unable to find radio button "advisor"

截图

根据提供的信息,我如何 select 我的功能规范的单选按钮?

choose 按名称、ID 或标签文本定位单选按钮 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#choose-instance_method。而不是您指定输入的 value (我假设第二个值应该是 "client" 而不是您拥有的 "advisor" )。由于您没有 ID,并且名称会不明确,因此您可以使用 option 参数(匹配值)来缩小到正确的无线电。因此

choose('trade_request_submission[advisor_or_client]', option: 'advisor')

或可能

choose(option: 'advisor')  # if you don't have any other radios with that value

另一种选择是指定标签文本,但这需要将 id 添加到与标签上的 for 属性相匹配的 input 元素,以正确地 link 它们,在你可以做哪种情况

choose('Advisor')