3
<select class="business_group" multiple="multiple" name="SelectedBusinessGroups">
  <option value="Partners">Partners</option>
  <option value="Press">Press</option>
  <option value="ProductToolbox">Product Toolbox</option>
  <option selected="selected" value="Promotional">Promotional</option>
  <option value="Sponsors">Sponsors</option>
</select>

由于命名的属性:selected意味着单击该选项。我想检查是否从选项列表中选择了“促销”。我怎样才能做到这一点?

我试过

assert @browser.option(:text => "Promotional").attribute_value("selected").exists? == true

但它不起作用。

4

1 回答 1

4

您有几个选项可以检查所选选项。

使用选项#selected?

选项有一个内置方法可以告诉您它们是否被选中 - 请参阅Option#selected?

@browser.option(:text => "Promotional").selected?
#=> true

@browser.option(:text => "Press").selected?
#=> false

使用 Select#selected?

选择列表具有用于检查是否选择了选项的内置方法 - Select#selected?。请注意,这仅根据其文本检查选项。

ddl = @browser.select(:class => 'business_group')

ddl.selected?('Promotional')
#=> true

ddl.selected?('Press')
#=> false

使用 Select#selected_options

Select#selected_options方法将返回选定选项的集合。您遍历此集合以查看是否包含您想要的选项。这使您可以通过文本以外的方式检查选项。

selected = @browser.select(:class => 'business_group').selected_options
selected.any?{ |o| o.text == 'Promotional' }
#=> true

使用元素#attribute_value

如果属性存在,该attribute_value方法将属性值作为字符串返回。如果属性不存在,则返回 nil。

#Compare the attribute value
@browser.option(:text => "Promotional").attribute_value("selected") == 'true'
#=> true

#Compare the attribute value presence
@browser.option(:text => "Promotional").attribute_value("selected").nil? == false
#=> true
于 2013-04-23T16:32:13.793 回答