Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 水豚-如何查看是否选择了下拉元素?_Selenium_Drop Down Menu_Webdriver_Capybara_Html Select - Fatal编程技术网

Selenium 水豚-如何查看是否选择了下拉元素?

Selenium 水豚-如何查看是否选择了下拉元素?,selenium,drop-down-menu,webdriver,capybara,html-select,Selenium,Drop Down Menu,Webdriver,Capybara,Html Select,我的HTML是 <select id="auto_policy_autos_attributes_0_ownership" name="auto_policy[autos_attributes][0][ownership]"> <option value="Owned">Owned</option> <option value="Financed">Financed</option> <option value="Le

我的HTML是

<select id="auto_policy_autos_attributes_0_ownership" name="auto_policy[autos_attributes][0][ownership]">
  <option value="Owned">Owned</option>
  <option value="Financed">Financed</option>
  <option value="Leased" selected="selected">Leased</option></select>
正确,但我如何查看它是否已被检查

我试过了

find('select#auto_policy_autos_attributes_0_ownership option[value="Leased" selected="selected"]')
但我明白了

Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: 
An invalid or illegal selector was specified
我对未来抱有希望

'select#auto_policy_autos_attributes_0_ownership option[value="Leased"], selected')).to be
但我得到了一个假阳性结果

'select#auto_policy_autos_attributes_0_ownership option[value="Owned"], selected')).to be
返回true,即使我已选择使用

select 'Leased', from: 'auto_policy_autos_attributes_0_ownership'
我可以看到它在浏览器中工作。

您可以使用:

一个选择是

expect(has_select?('auto_policy_autos_attributes_0_ownership', selected: 'Leased')).to be true

这正是水豚选择匹配器的设计目的

expect(page).to have_select('auto_policy_autos_attributes_0_ownership', selected: 'Leased')
它描述了您要检查的内容——一个带有特定选项的特定select元素,并在发生故障时提供一条漂亮的错误消息

如果你真的想坚持使用谓词匹配器,你也可以这样做

expect(find(:option, 'Leased')).to be_selected 

尽管该选项可以出现在页面上的任何选择中,除非您使用了内部块或将查找范围限定为特定选择。

但我更喜欢alecxe as be_selected更具描述性,尤其是当它失败时(当它起作用时!)
expect(page).to have_select('auto_policy_autos_attributes_0_ownership', selected: 'Leased')
expect(find(:option, 'Leased')).to be_selected