Ruby-on-rails 获取用于水豚测试的下拉列表的选择值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11498575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:41:33  来源:igfitidea点击:

Get select value of dropdown for capybara testing

ruby-on-railsrspeccapybara

提问by Brandon

I have to write tests for a web site. I am trying to get the selected value of a dropdown box. So far i can get the contents of the dropdown by doing

我必须为一个网站编写测试。我正在尝试获取下拉框的选定值。到目前为止,我可以通过执行获取下拉列表的内容

find_field('restrictions__rating_movies').text

returns - Don't Allow Movies G PG M R13 R15 R16 R18 R RP16 Allow All Movies

返回 - 不允许电影 G PG M R13 R15 R16 R18 R RP16 允许所有电影

I can get the value of the selected object.

我可以获得所选对象的值。

find_field('restrictions__rating_movies').value

returns - 1000

回报 - 1000

This does not help me much though because i am trying to get the text of the selected item from a drop down box.

但这对我没有多大帮助,因为我试图从下拉框中获取所选项目的文本。

<select class="" id="restrictions__rating_movies" name="restrictions[][rating_movies]">            
<option value="0">Don't Allow Movies</option>
<option value="100">G</option>
<option value="200">PG</option>
<option value="300">M</option>
<option value="325">R13</option>
<option value="350">R15</option>
<option value="375">R16</option>
<option value="400">R18</option>
<option value="500">R</option>
<option value="600">RP16</option>
<option value="1000" selected="selected">Allow All Movies</option></select>

in this case shown i need to get the value 'Allow All Movies' I have tried many different combinations of the above two examples.

在这种情况下,我需要获得值“允许所有电影”我已经尝试了上述两个示例的许多不同组合。

采纳答案by Brandon

find_field('restrictions__rating_movies').find('option[selected]').text

回答by gylaz

There's a have_selectmatcher if you use Capybara with Rspec:

have_select如果您将 Capybara 与 Rspec 一起使用,则有一个匹配器:

expect(page).to have_select('my-select', selected: 'Option 2')

回答by Vijay Chouhan

Very simple way to get value of selected option is:

获取所选选项值的非常简单的方法是:

find("#restrictions__rating_movies").value

This will return selected select option value.

这将返回选定的选择选项值。

回答by Abinoam Jr.

If you only need to assert if a field is selected with a given option, the straightforward answer is

如果您只需要断言是否使用给定选项选择了一个字段,那么直接的答案是

#Find a select box by (label) name or id and assert the given text is selected
When /^select box "([^"]*)" is selected with "([^"]*)"$/ do |dropdown, selected_text|    
  assert page.has_select?(dropdown, selected: selected_text)
end

Source: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_select%3F-instance_method

来源:http: //rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_select%3F-instance_method

But the title of your question is "Get select value for dropdown". And I've run into a similar problem where I would like not only to assert the selection, but also retrieve the text and value of the selected field. I've found no straight way on API. The easiest way I've found was: #all("option").find &:selected?

但是你的问题的标题是"Get select value for dropdown"。我遇到了一个类似的问题,我不仅要断言选择,还要检索所选字段的文本和值。我在 API 上没有找到直接的方法。我发现的最简单的方法是:#all("option").find &:selected?

When /^ select box "([^"]*)" is selected with "([^"]*)"$/ do |dropdown, selected_text|
  sb = find_field(dropdown)
  sb_selected = sb.all("option").find &:selected?
  msg = "Selected: #{sb_selected.text.inspect} - value:#{sb_selected.value.inspect}"
  assert page.has_select?(dropdown, selected: selected_text), msg
end

This gives me a more comprehensive error message when the assertion fails.

当断言失败时,这会给我一个更全面的错误消息。

If there's multiple selections you can use #select in place of #find, as in #all("option").select &:selected?. It will return an Array.

如果有多个选择,您可以使用 #select 代替 #find,如#all("option").select &:selected?. 它将返回一个数组。

This answer doesn't rely on the 'option[selected]' trick as the previous ones, so it works even if the selection is done by Javascript (which was the reason why the previous answers didn't work for me at all).

这个答案不依赖于之前的'option[selected]'技巧,所以即使选择是由Javascript完成的,它也能工作(这就是为什么之前的答案对我根本不起作用的原因)。

Tested on:

测试:

capybara (2.2.1)
capybara-webkit (1.1.0)
cucumber (1.3.14)
cucumber-rails (1.4.0)

回答by Adam Ehven

If you want to find the current selected text, without assuming what it might be so that you can just compare it to an expectation, the following works even if the selection was made by JS (so that there is no 'option[selected]').

如果您想找到当前选定的文本,而不假设它可能是什么,以便您可以将其与期望进行比较,即使选择是由 JS 进行的,以下内容也有效(因此没有 'option[selected]' )。

First I find the value of the select, then I find the text of the option with that value:

首先我找到选择的值,然后我找到具有该值的选项文本:

  def selected(selector)
    value = find(selector).value
    text = find(selector).find("option[value='#{value}']").text
  end

回答by Frost

Would something like this work?

这样的东西会起作用吗?

within("//select[@id='restrictions__rating_movies']") do
  find_field("//option[@selected='selected']").text
end