├── gem ├── Rakefile ├── lib │ ├── capybara │ │ ├── select2.rb │ │ └── selectors │ │ │ └── tag_selector.rb │ ├── capybara-select2 │ │ └── version.rb │ └── capybara-select2.rb ├── Gemfile └── capybara-select2.gemspec ├── .gitignore ├── LICENSE.txt └── README.md /gem/Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /gem/lib/capybara/select2.rb: -------------------------------------------------------------------------------- 1 | require '../capybara-select2' -------------------------------------------------------------------------------- /gem/lib/capybara-select2/version.rb: -------------------------------------------------------------------------------- 1 | module Capybara 2 | module Select2 3 | VERSION = "1.0.1" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /gem/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in capybara-select2.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | .idea 7 | Gemfile.lock 8 | InstalledFiles 9 | _yardoc 10 | coverage 11 | doc/ 12 | lib/bundler/man 13 | pkg 14 | rdoc 15 | spec/reports 16 | test/tmp 17 | test/version_tmp 18 | tmp 19 | -------------------------------------------------------------------------------- /gem/lib/capybara/selectors/tag_selector.rb: -------------------------------------------------------------------------------- 1 | module Capybara 2 | module Selectors 3 | module TagSelector 4 | def select2_tag(value, options = {}) 5 | if options[:from] 6 | find(:fillable_field, options[:from]).set(value) 7 | else 8 | find('input.select2-input').set(value) 9 | end 10 | 11 | find('.select2-drop li', text: value).click 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /gem/capybara-select2.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'capybara-select2/version' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "capybara-select2" 8 | gem.version = Capybara::Select2::VERSION 9 | gem.authors = ["William Yeung"] 10 | gem.email = ["william@tofugear.com"] 11 | gem.description = %q{Helper for triggering select for select2 javascript library} 12 | gem.summary = "" 13 | gem.homepage = "" 14 | 15 | gem.files = `git ls-files`.split($/) 16 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 | gem.require_paths = ["lib"] 19 | 20 | gem.add_dependency 'rspec' 21 | gem.add_dependency 'capybara' 22 | end 23 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 William Yeung 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Capybara::Select2 2 | 3 | [![Code Climate](https://codeclimate.com/github/goodwill/capybara-select2.png)](https://codeclimate.com/github/goodwill/capybara-select2) 4 | 5 | All this gem does is something very simple- allow you to trigger select2 dropdown to select the value you want. The original select doesn't with the javascript overrides, so this new helper method does only this thing. 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | ``` ruby 12 | gem 'capybara-select2', group: :test 13 | ``` 14 | 15 | Or, add it into your test group 16 | 17 | ``` ruby 18 | group :test do 19 | gem 'capybara-select2' 20 | ... 21 | end 22 | ``` 23 | 24 | And then execute: 25 | 26 | ``` bash 27 | $ bundle 28 | ``` 29 | 30 | Or install it yourself as: 31 | 32 | ``` bash 33 | $ gem install capybara-select2 34 | ``` 35 | 36 | The gem automatically hook itself into rspec helper using Rspec.configure. 37 | 38 | If you're using capybara outside of Rspec, you may have to include the following (eg: in `features/support/env.rb` for Cucumber users): 39 | 40 | ``` ruby 41 | include Capybara::Select2 42 | ``` 43 | 44 | ## Usage 45 | 46 | Just use this method inside your capybara test: 47 | 48 | ``` ruby 49 | select2("Dropdown Text", from: "Label of the dropdown") 50 | ``` 51 | 52 | or 53 | 54 | ``` ruby 55 | select2("Dropdown Text", xpath: "") 56 | ``` 57 | 58 | If the select2 field has a `min_length` option (acts as a search field) specify it with: 59 | 60 | ``` ruby 61 | select2("foo", from: "Label of the dropdown", search: true) 62 | ``` 63 | 64 | If select2 field has [tags](http://ivaynberg.github.io/select2/#tags) option you can use: 65 | 66 | ```ruby 67 | select2_tag('value', from: 'Label of input') 68 | ``` 69 | 70 | ## Contributing 71 | 72 | 1. Fork it 73 | 2. Create your feature branch (`git checkout -b my-new-feature`) 74 | 3. Commit your changes (`git commit -am 'Add some feature'`) 75 | 4. Push to the branch (`git push origin my-new-feature`) 76 | 5. Create new Pull Request 77 | -------------------------------------------------------------------------------- /gem/lib/capybara-select2.rb: -------------------------------------------------------------------------------- 1 | require "capybara-select2/version" 2 | require 'capybara/selectors/tag_selector' 3 | require 'rspec/core' 4 | 5 | module Capybara 6 | module Select2 7 | def select2(value, options = {}) 8 | raise "Must pass a hash containing 'from' or 'xpath' or 'css'" unless options.is_a?(Hash) and [:from, :xpath, :css].any? { |k| options.has_key? k } 9 | 10 | if options.has_key? :xpath 11 | select2_container = find(:xpath, options[:xpath]) 12 | elsif options.has_key? :css 13 | select2_container = find(:css, options[:css]) 14 | else 15 | select_name = options[:from] 16 | select2_container = find("label", text: select_name).find(:xpath, '..').find(".select2-container") 17 | end 18 | 19 | # Open select2 field 20 | if select2_container.has_selector?(".select2-selection") 21 | # select2 version 4.0 22 | select2_container.find(".select2-selection").click 23 | elsif select2_container.has_selector?(".select2-choice") 24 | select2_container.find(".select2-choice").click 25 | else 26 | select2_container.find(".select2-choices").click 27 | end 28 | 29 | if options.has_key? :search 30 | find(:xpath, "//body").find(".select2-search input.select2-search__field").set(value) 31 | page.execute_script(%|$("input.select2-search__field:visible").keyup();|) 32 | drop_container = ".select2-results" 33 | elsif find(:xpath, "//body").has_selector?(".select2-dropdown") 34 | # select2 version 4.0 35 | drop_container = ".select2-dropdown" 36 | else 37 | drop_container = ".select2-drop" 38 | end 39 | 40 | [value].flatten.each do |value| 41 | if find(:xpath, "//body").has_selector?("#{drop_container} li.select2-results__option") 42 | # select2 version 4.0 43 | find(:xpath, "//body").find("#{drop_container} li.select2-results__option", text: value).click 44 | else 45 | find(:xpath, "//body").find("#{drop_container} li.select2-result-selectable", text: value).click 46 | end 47 | end 48 | end 49 | end 50 | end 51 | 52 | RSpec.configure do |config| 53 | config.include Capybara::Select2 54 | config.include Capybara::Selectors::TagSelector 55 | end 56 | --------------------------------------------------------------------------------