Reputation: 309
I am writing the cucumber capybara tests to check if the form has correct elements populated. I am stuck how to find the selected element value displayed in the dropdown. Here is the code
<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>
I wanted to find if the dropdown has "2 x 2.0 GHz Cores" selected or not. I know there is way to find it through the "value" but I dont want to find it by value rather I wanted to find out by actual displayed value on page. Something like this
expect( find(:css, 'select#server_request_cores').value ).to eq('2 x 2.0 GHz Cores')
Upvotes: 0
Views: 1604
Reputation: 49890
The best way to do this is to use the matchers capybara provides
expect(page).to have_select('server_request_cores', selected: '2 x 2.0 GHz Cores')
This reads better and will use capybaras automatic waiting behavior in case the selection is dynamically changing
Upvotes: 4
Reputation: 309
I think I found out the answer. I tried the below code and it worked.
expect(find_field('server_request_cores').find('option[selected]').text).to eq('2 x 2.0 GHz Cores')
Upvotes: 2