title: how to test / interact with AJAX autocomplete and Capybara / Poltergeist
body: <p>I am trying to interact with an external website at: <a href="http://is.gd/LtgYEk" rel="nofollow noreferrer">http://is.gd/LtgYEk</a>
  I need to be able to fill in the input with id="textOrigen" here is the html</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;p&gt;
      &lt;label class="form-label"&gt;Departing from:&lt;/label&gt;
      &lt;span class="label-field"&gt;
          &lt;input type="text" autocomplete="off" onblur="onblur2('textOrigen');" onfocus="initID('textOrigen');" size="17" id="textOrigen" name="text" maxlength="40" style="position:static;color: #505050;"&gt;
          &lt;select style="display:none" onchange="clearValidate(); Origen();" class="validate[dynamic]" id="cmbOrigen" name="cmbOrigen"&gt;
              &lt;option value="-1" selected="selected"&gt;Origin&lt;/option&gt;
          &lt;/select&gt;
          &lt;label class="label-error" id="lblerrorOrigen"&gt;&lt;/label&gt; 
      &lt;/span&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>I put together a simple ruby script using 'capybara/poltergeist'</p>
  
  <p>I am unable to replicate the browser behavior, which is:
      on click the input field default value is highlighted thus being deleted as you start typing.</p>
  
  <p>I lost track of all different variations I tried, but tried many. I found another <a href="https://stackoverflow.com/questions/20830194/why-cant-my-capybara-poltergeist-test-select-from-a-jquery-autocomplete-field">SO post</a> which seemed somewhat useful but it didn't help </p>
  
  <p>This is the last revision of the method to fill this field:</p>
  
  <pre><code>def session.fill_autocomplete(field, options = {})
    execute_script %Q{ $('##{field}').trigger('focus') }
    fill_in field, with: options[:with]
    execute_script %Q{ $('##{field}').trigger('focus') }
    execute_script %Q{ $('##{field}').trigger('keydown') }
    selector = %Q{#output div:contains('#{options[:with]}')}
    execute_script "$(\"#{selector}\").mouseenter().click()"
  end
  </code></pre>
  
  <p>As I wrote the script is very simple, the only other relevant bit is when the session is instantiated with:</p>
  
  <pre><code>session = Capybara::Session.new(:poltergeist)
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>