title: How to use capybara to select a select2 drop-down field
body: <p>There is help online for using select2 with capybara (see links below), but no help in particular as far as I have seen for select2 drop-down fields.  I have tried all kinds of things, including trying to fill in the field when :js => false (using something along the lines of <code>find(:xpath, "//input[@id='product_manufacturer_id']").set "Test product manufacturer"</code>) or variations on the solutions that work on other select2 fields (see links given below). Nothing is working for me so far.</p>
  
  <p><strong>Notes on my configuration:</strong></p>
  
  <ul>
  <li>Using select2 version 3</li>
  <li>Using capybara WebKit</li>
  <li>This particular field is also using simple form, and is an association (so has <code>f.association</code>...). Particularly, <code>ProductManufacturer</code> instances are in a <code>has_many</code> relationship with products. </li>
  <li>The select2 drop-down field is supposed to be dynamically populated by <code>ProductManufacturer</code> instances which match the query text (i.e., the text you type into the search field). </li>
  </ul>
  
  <p><em>In case it is helpful to see how I am implementing the factory:</em></p>
  
  <p>Here is my factory file:</p>
  
  <pre><code>factory :product do
    name "Test product"
    url { Faker::Name.name.parameterize }
    access_level 1
    product_manufacturer
  end
  
  factory :product_manufacturer do
    name "Test product manufacturer"
  
    factory :product_manufacturer_with_product do
      transient do
        products_count 1
      end
  
      after(:create) do |product, evaluator|
        create_list(:product,
                     evaluator.products_count,
                     product: product)
      end
    end
  end
  </code></pre>
  
  <p>And then, before the test starts, I run:</p>
  
  <pre><code>@product_manufacturer = create(:product_manufacturer)
  </code></pre>
  
  <p><strong>My latest attempt:</strong> 
  My helper method (which is working for my other select2 fields):</p>
  
  <pre><code>def select2_choose(id, options)
    page.execute_script  "$('#{id}').trigger('keydown').val('{options[:query]}').trigger('keyup');"
    find(".select2-result-label", :text =&gt; options[:choose]).click
  end
  </code></pre>
  
  <p>And then how I implement it:</p>
  
  <pre><code>select2_choose( "#s2id_autogen7", 
    :query =&gt; @product_manufacturer.name[0...-2], 
    :choose =&gt; @product_manufacturer.name)
  </code></pre>
  
  <p>Which outputs the following error message: </p>
  
  <pre><code>Failure/Error: create_product
  Capybara::ElementNotFound:
     Unable to find css ".select2-result-label" with text "Test product manufacturer"
  </code></pre>
  
  <p>(which basically means that it has found and clicked on the drop-down box, and it has put in the query text, "Test product manufacture". But select2 was not finding the option from the database to find it.)</p>
  
  <p>Note that I have successfully used factory_girl to generate my ProductManufacturer instance object <code>@product_manufacturer</code>, and calling things such as <code>puts @product_manufacturer</code> is successful, returning the instance object: <code>ProductManufacturer:0x007f0145f9cb38&gt;</code>.</p>
  
  <p>Here is a screenshot from right before the test fails:
  <a href="https://i.stack.imgur.com/I7BXC.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/I7BXC.jpg" alt="screenshot"></a></p>
  
  <p><strong>Other questions that are related but do not fully solve this issue:</strong></p>
  
  <p><em>- Selecting select2 drop downs (but not in capybara):</em></p>
  
  <p><a href="https://stackoverflow.com/questions/20134085/how-to-select-option-in-drop-down-using-capybara">How to select option in drop down using Capybara</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26074414/unable-to-select-item-in-select2-drop-down">Unable to select item in Select2 drop down</a></p>
  
  <p><em>- Selecting select2 options in capybara (but not with drop downs):</em></p>
  
  <p><a href="https://stackoverflow.com/questions/12771436/how-to-test-a-select2-element-with-capybara-dsl">How to test a Select2 element with capybara DSL?</a> (Note: I successfully used answers from here to select non-drop-down select2 fields)</p>
  
  <p><em>- selecting select2 drop-down using selenium</em></p>
  
  <p><a href="https://stackoverflow.com/questions/13728551/selenium-select2-command-for-drop-down-box">Selenium Select2 command for drop-down box</a></p>