检查选择框与 Capybara 有某些选项选项、Capybara

2023-09-08 10:22:47 作者:別說對芣起

如何使用 Capybara 检查选择框是否将某些值列为选项?它必须与 Selenium 兼容...

How do I use Capybara to check that a select box has certain values listed as options? It has to be compatible with Selenium...

这是我拥有的 HTML:

This is the HTML that I have:

<select id="cars"> 
  <option></option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

这就是我想做的:

Then the "cars" field should contain the option "audi"

推荐答案

尝试使用 capybara rspec 匹配器 have_select(locator, options = {}) 改为:

Try using the capybara rspec matcher have_select(locator, options = {}) instead:

#Find a select box by (label) name or id and assert the given text is selected
Then /^"([^"]*)" should be selected for "([^"]*)"$/ do |selected_text, dropdown|
  expect(page).to have_select(dropdown, :selected => selected_text)
end

#Find a select box by (label) name or id and assert the expected option is present
Then /^"([^"]*)" should contain "([^"]*)"$/ do |dropdown, text|
  expect(page).to have_select(dropdown, :options => [text])
end