通过显示的选项找出水豚中选择的值

Find out the value selected in capybara by displayed option

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

<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 内核”。我知道有办法通过 "value" 找到它,但我不想按值找到它,而是想按页面上的实际显示值找到它。像这样

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

我想我找到了答案。我尝试了下面的代码并且有效。

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

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

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

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