2
votes

I'm trying to troubleshoot a feature spec I'm writing to learn Rails & testing. It's an edit_spec for a Rails 4 app. I'm using RSpec and Capybara.

The Problem: Capybara can not find elements from two drop down lists on a form. Here's the relevant parts of my form's source code:

<div class="field">
<label for="appointment_member_id">Member</label><br>
<select id="appointment_member_id" name="appointment[member_id]">
<option value="1">Callis Callis</option>
<option value="2">Rachale Rachale</option>
<option value="3">Hal Hal</option>
<option value="4">Kallis Kallis</option>
<option value="5">Earle Earle</option>
<option value="6">Rudy Rudy</option></select>
</div>
<div class="field">
<label for="appointment_trainer_id">Trainer</label><br>
<select id="appointment_trainer_id"
name="appointment[trainer_id]"> <option value="1">Jacob Jacob</option>
<option value="2">Michele Michele</option>
<option value="3">Alex Alex</option>
<option value="4">Yanni Yanni</option>
<option value="5">Upton Upton</option>
<option value="6">Willham Willham</option></select>
</div> 

In my edit_spec, I tried using "option value" and "select id" to get Capybara to select names from the drop-down lists:

select('4', :from => 'appointment_member_id')
select('2', :from => 'appointment_trainer_id')

The result is a Capybara::ElementNotFound error. Here's the full text of the error message:

updates an appointment successfully with corrected information (FAILED - 1)

Failures:

1) Editing appointments updates an appointment successfully with corrected information
 Failure/Error: select('4', :from => 'appointment_member_id')
 Capybara::ElementNotFound:
   Unable to find option "4"

I researched the issue and documentation for Capybara, and also a Stackoverflow post with similar problem. The poster solved the issue, but couldn't recall how he did it. I'm baffled as to why Capybara can't find the member and trainer from the drop down list? The option values and names appear to be visible on the form. What am I missing? I appreciate any help!

2

2 Answers

2
votes

Capybara simulates user behaviour. So instead of ids and values try to use what the user see:

select 'Kallis Kallis', from: 'Member'
select 'Michele Michele', from: 'Trainer'
1
votes

You could try the code below, or possibly assign the div and id and replace "div.field" with whatever id you choose

within "div.field" do
  find("option[value='4']", text: 'Yanni Yanni').select_option
  click_button 'your_submit_button'
end