Capybara - 如何查看是否选择了下拉元素?
Capybara - how to see if a dropdown element is selected?
我的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>
我可以 select 最多
find('select#auto_policy_autos_attributes_0_ownership option[value="Leased"]')
正确,但如何查看是否已检查?
我试过了
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
returns 是的,即使我已经 select 租用
select 'Leased', from: 'auto_policy_autos_attributes_0_ownership'
我可以看到它在浏览器中工作。
您可以使用 be_selected
matcher:
expect(@session.find('select#auto_policy_autos_attributes_0_ownership option[value="Leased"]')).to be_selected
一个选项是
expect(has_select?('auto_policy_autos_attributes_0_ownership', selected: 'Leased')).to be true
这正是水豚 have_select 匹配器的设计目的
expect(page).to have_select('auto_policy_autos_attributes_0_ownership', selected: 'Leased')
它描述了您要检查的内容 -- 具有特定 selected 选项的特定 select 元素,并在失败时提供了一个很好的错误消息。
如果你真的想坚持使用谓词匹配器,你也可以这样做
expect(find(:option, 'Leased')).to be_selected
尽管该选项可以在页面上的任何 select 中,除非您使用内部块或将查找范围限定为特定 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="Leased" selected="selected">Leased</option></select>
我可以 select 最多
find('select#auto_policy_autos_attributes_0_ownership option[value="Leased"]')
正确,但如何查看是否已检查?
我试过了
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
returns 是的,即使我已经 select 租用
select 'Leased', from: 'auto_policy_autos_attributes_0_ownership'
我可以看到它在浏览器中工作。
您可以使用 be_selected
matcher:
expect(@session.find('select#auto_policy_autos_attributes_0_ownership option[value="Leased"]')).to be_selected
一个选项是
expect(has_select?('auto_policy_autos_attributes_0_ownership', selected: 'Leased')).to be true
这正是水豚 have_select 匹配器的设计目的
expect(page).to have_select('auto_policy_autos_attributes_0_ownership', selected: 'Leased')
它描述了您要检查的内容 -- 具有特定 selected 选项的特定 select 元素,并在失败时提供了一个很好的错误消息。
如果你真的想坚持使用谓词匹配器,你也可以这样做
expect(find(:option, 'Leased')).to be_selected
尽管该选项可以在页面上的任何 select 中,除非您使用内部块或将查找范围限定为特定 select。