检查选择框是否具有水豚的某些选项

发布于 2024-10-25 20:12:20 字数 485 浏览 2 评论 0原文

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

这是我拥有的 HTML:

<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"

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

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>

This is what I want to do:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

明明#如月 2024-11-01 20:12:22

尝试使用水豚 rspec 匹配器 have_select(locator, options = {}) 相反:

#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

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
夏有森光若流苏 2024-11-01 20:12:22

就其价值而言,我将其称为下拉菜单,而不是字段,所以我会写:

Then the "cars" drop-down should contain the option "audi"

为了回答您的问题,这里是实现此功能的 RSpec 代码(未经测试):

Then /^the "([^"]*)" drop-down should contain the option "([^"]*)"$/ do |id, value|
  page.should have_xpath "//select[@id = '#{id}']/option[@value = '#{value}']"
end

如果您想测试该选项文本而不是值属性(这可能会使更具可读性场景),你可以这样写:

  page.should have_xpath "//select[@id = '#{id}']/option[text() = '#{value}']"

For what it's worth, I'd call it a drop-down menu, not a field, so I'd write:

Then the "cars" drop-down should contain the option "audi"

To answer your question, here's the RSpec code to implement this (untested):

Then /^the "([^"]*)" drop-down should contain the option "([^"]*)"$/ do |id, value|
  page.should have_xpath "//select[@id = '#{id}']/option[@value = '#{value}']"
end

If you want to test for the option text instead of the value attribute (which might make for more readable scenarios), you could write:

  page.should have_xpath "//select[@id = '#{id}']/option[text() = '#{value}']"
素年丶 2024-11-01 20:12:22

作为替代解决方案,由于我不熟悉 xpaths,我这样做是为了解决类似的问题:

page.all('select#cars option').map(&:value).should == %w(volvo saab mercedes audi)

它非常简单,但花了我一些时间来弄清楚。

As an alternative solution, and as I'm not familiar with xpaths, I did this to solve a similar problem:

page.all('select#cars option').map(&:value).should == %w(volvo saab mercedes audi)

Its quite simple, but took me some time to figure out.

注定孤独终老 2024-11-01 20:12:22

好吧,因为我在附近并看到了这个问题(并且今天进行了测试),所以决定发布我的方式:

within("select#cars") do
  %w(volvo saab mercedes audi).each do |option|
    expect(find("option[value=#{option}]").text).to eq(option.capitalize)
  end
end

Well, since i was around and saw the question (and been testing today) decided to post my way:

within("select#cars") do
  %w(volvo saab mercedes audi).each do |option|
    expect(find("option[value=#{option}]").text).to eq(option.capitalize)
  end
end
桃气十足 2024-11-01 20:12:22
Then I should see "audi" within "#cars"

应该能解决问题

Then I should see "audi" within "#cars"

should do the trick

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