There are many times when more than one result matches with the actual result. So, we get the ambiguous match. Capybara provides some options to handle this situation.
Suppose we have to perform some assertion on date field then in that case if we do not use any capybara option then we will get ambiguous match. We have the following HTML:
<select class="form-control custom-select" required="" name="effectiveday">
<option value="">Select Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
Now, if we use the following query then more than one match will be found:
@session.find('option', text: '5')
Capybara will search all the options that contains the text '5'. So, it will found two options ie 5 and 15. In order to get rid of this ambiguity, we will use ':prefer_exact' match option.
@session.find('option', text: '5', match: :prefer_exact)
'prefer_exact' will only search the exact match and hence only one option is returned. We can also use another Capybara option to avoid the abmiguous match.
@session.find('option', text: '5', match: :first)
':first' option will only return the first option that contains the option value equal to five.
0 Comment(s)