我正在try 使用Capybara(2.1.0)从下拉菜单中 Select 一个项目.

我想按数字 Select (意思是 Select 第二个、第三个等选项).

我疯狂地在谷歌上搜索各种各样的东西,但没有运气.

我可以使用以下值来 Select 它:

 find("option[value='4c430d62-f1ba-474f-8e8a-4452c55ea0a8']").click

但我不想使用这种方法b/c,它的值会发生变化,这会使我的测试变得脆弱.

下拉列表的HTML是:

<td class="value">
    <select name="organizationSelect" id="organizationSelect" class="required">
     <option value="NULL">Choose...</option>
     <option value="4c430d62-f1ba-474f-8e8a-4452c55ea0a8">&nbsp;Institution1</option>
     <option value="e1a4efa7-352d-410a-957e-35c8a3b92944">&nbsp;Institution / test</option>
    </select>
</td>

我也试过:

  option = find(:xpath, "//*[@id='organizationSelect']/option[2]").text  
  select(option, :from => organizationSelect)

但它会导致这个错误:

Ambiguous match, found 2 elements matching option "Institution" (Capybara::Ambiguous)

那么如何从下拉列表中 Select 第一、第二、第三等选项(使用水豚)?

推荐答案

如果你看一下the source of the select method,你会发现当你传递一个from键时,它基本上是:

find(:select, from, options).find(:option, value, options).select_option

换句话说,它会找到你感兴趣的<select>,然后在其中找到<option>,然后在<option> node 上调用select_option.

你已经基本完成了前两件事,我只是重新安排一下.然后你可以在最后钉上select_option法:

find('#organizationSelect').find(:xpath, 'option[2]').select_option

Ruby相关问答推荐

Ruby中链表大小调用的时间复杂度

如何使用 gsub 删除返回字符串中的/和/i?

BasicObject 的 singleton_class 的超类如何以及为什么是 Ruby 中的 Class 类?

碰撞检测测试中对象的 Nil 类

判断数组中的字符串是否包含另一个数组中的子字符串

如何在 Shopify 脚本编辑器中显示数组值?

重写 Enum#inject 以在 Ruby 中将符号作为参数

Rack::Request - 如何获取所有标头?

`Range#include?` 和 `Range#cover?` 有什么区别?

Ruby 局部变量未定义

Sublime Text 2 中的 RubyTest

Coffeescript 中等效的 Ruby .times

我如何 expect期望在 RSpec 中引发异常的东西?

`respond_to?` 与 `respond_to_missing?`

无法将 RVM 安装的 Ruby 与 sudo 一起使用

你能在 Python 的核心类型上修改补丁方法吗?

Ruby 数组中的奇数(或偶数)条目

如何合并 Ruby 哈希

Ruby 将 CSV 文件读取为 UTF-8 和/或将 ASCII-8Bit 编码转换为 UTF-8

Ruby Activerecord IN 子句