【问题标题】:Find out the value selected in capybara by displayed option通过显示的选项找出在 capybara 中选择的值
【发布时间】:2016-01-14 06:38:40
【问题描述】:

我正在编写黄瓜水豚测试以检查表单是否填充了正确的元素。我被困在如何找到下拉菜单中显示的选定元素值。这是代码

<select class="selectpicker form-control" id="server_request_cores" name="server_request[cores]"><option value="1">1 x 2.0 GHz Core</option>
<option selected="selected" value="2">2 x 2.0 GHz Cores</option>
<option value="4">4 x 2.0 GHz Cores</option>
<option value="8">8 x 2.0 GHz Cores</option>
<option value="12">12 x 2.0 GHz Cores</option>
<option value="16">16 x 2.0 GHz Cores</option>
</select>

我想查看下拉菜单是否选择了“2 x 2.0 GHz Cores”。我知道有办法通过“价值”找到它,但我不想通过价值找到它,而是想通过页面上的实际显示值来找到它。像这样的

expect( find(:css, 'select#server_request_cores').value ).to eq('2 x 2.0 GHz Cores')

【问题讨论】:

    标签: cucumber capybara


    【解决方案1】:

    最好的方法是使用水豚提供的匹配器

    expect(page).to have_select('server_request_cores', selected: '2 x 2.0 GHz Cores')
    

    这读起来更好,并且会在选择动态变化的情况下使用水豚自动等待行为

    【讨论】:

      【解决方案2】:

      我想我找到了答案。我尝试了下面的代码,它成功了。

      expect(find_field('server_request_cores').find('option[selected]').text).to eq('2 x 2.0 GHz Cores')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-05
        • 1970-01-01
        • 2020-04-23
        相关资源
        最近更新 更多