Capybara select2 helper

Maybe it could help someone. Wrote a simple capybara helper to set values into select2 fields.

See code below.

Answers


module Features
  module CapybaraHelpers
    def select2(value, element_selector)
      select2_container = first("#{element_selector}")
      select2_container.find(".select2-choice").click

      find(:xpath, "//body").find("input.select2-input").set(value)
      page.execute_script(%|$("input.select2-input:visible").keyup();|)
      drop_container = ".select2-results"
      find(:xpath, "//body").find("#{drop_container} li", text: value).click
    end
  end
end

Then in your code just call something like select2("apple", "#s2id_fruit_id") and capybara will select "apple" value in select2 field with #s2id_fruit_id id.

Thanks to goodwill with his capybara-select2 gem.


Need Your Help

Add HTML elements to the current page using PHP

php html dom

So I have the need to dynamically add html content using php which isnt the tricky part but I'm trying to put the HTML into a different location in the document than where the PHP is being run. So ...