【问题标题】:Get selected value of dropdown with capybara使用 capybara 获取下拉列表的选定值
【发布时间】:2016-09-21 11:37:16
【问题描述】:

我正在尝试使用下一个代码获取下拉框的选定值:

scenario 'USA as default country' do
  expect(page).to have_select('report_country_code', selected: 'United States')
end

该字段的 Html 是下一个:

<select class="form-control country required" name="report[country_code]" id="report_country_code">
  <option value="US">United States</option>
  ....
  ....
  <option value="ZW">Zimbabwe</option>
</select>

错误看起来像:

expected to find select box "report_country_code" with "United States" selected but there were no matches. Also found "United States .... Zimbabwe", which matched the selector but not all filters.

【问题讨论】:

  • have_select(..., selected: '...') 是正确的,但错误是说您的页面有select 标签但未选择"United States"。实际上,在您的示例 HTML 中,“美国”选项没有 selected 属性,这可以解释错误。
  • @shioyama,谢谢,现在清楚了。但是为什么我可以测试这个表格而不选择国家(这个字段是必需的)?当我在浏览器中访问 /new 表单时,我在该字段中看到“美国”..
  • 我想它之所以被选中是因为它是选项列表中的第一个。
  • 由于元素上有一个“必需”类而不是必需属性,我假设您使用 JS 库来进行客户端验证,而不是让浏览器处理它。在这种情况下,当在支持 JS 的浏览器中运行时,JS 库可能会默认分配第一个选项。如果您使用不会发生的 rack-test 驱动程序运行测试(不支持 JS),则不会选择任何内容。
  • 非常感谢!我改变了我的测试逻辑。

标签: ruby-on-rails ruby-on-rails-4 rspec capybara


【解决方案1】:

有同样的问题。 How to install lxml on Ubuntu 帮助了我

sudo apt-get install libxml2-dev

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 2015-05-22
    • 1970-01-01
    • 2010-11-08
    • 2018-05-04
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多