capybara_select2 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93c2e91cdd4b23d94a576633844142c2aa98c25b
4
+ data.tar.gz: 691a41714911311e684214ca8aa510d7a69adecc
5
+ SHA512:
6
+ metadata.gz: a0299c79716cca6f7b7cf11e6e523feed64bbc7c4bbc255fdeebcce09185ad281f9647996158c4b7b9ffa9fd168ae663a1162c7082694995d9ef7e7899a412a0
7
+ data.tar.gz: 7ccb61bca97c304e49cb597be10488f3a43dd24c3b944bed7b39cab86fc040d80b95748d99a3de1d08bf34d6a2cd40a986168bb640c6744e40c5188586a0766b
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Inpsired by [goodwill's capybara-select2](https://github.com/goodwill/capybara-select2), this gem assists in testing forms that uses the [select2](http://ivaynberg.github.io/select2/) select box replacement.
4
4
 
5
- To be frank, the other gem was out of date, and no fork had everything we needed. They were also very ... _procedural_.
5
+ (We needed the gem to work with MiniTest.)
6
6
 
7
7
  ## Installation
8
8
 
@@ -3,59 +3,79 @@ require "capybara_select2/version"
3
3
  module Capybara
4
4
  module Select2
5
5
  class Element
6
- attr_accessor :page, :search_enabled, :multiselect, :container
6
+ attr_accessor :page, :options, :search_enabled, :multiselect, :container, :drop_container
7
7
 
8
8
  def initialize(page_context, options = {})
9
9
  self.page = page_context
10
+ self.options= options
10
11
  self.search_enabled = !!options[:search]
11
12
  self.multiselect = !!options[:multiple]
12
13
  find_container(options.slice(:from, :xpath))
13
14
  end
14
15
 
15
- def drop_container_class
16
- if search_enabled then '.select2-results' else '.select2-drop' end
16
+ def drop_container_class(value)
17
+ if options.has_key? :search
18
+ page.find(:xpath, "//body").find(".select2-search input.select2-search__field").set(value)
19
+ page.execute_script(%|$("input.select2-search__field:visible").keyup();|)
20
+ ".select2-results"
21
+ elsif page.find(:xpath, "//body").has_selector?(".select2-dropdown")
22
+ # select2 version 4.0
23
+ ".select2-dropdown"
24
+ else
25
+ ".select2-drop"
26
+ end
17
27
  end
18
28
 
19
- def select_match!
20
- page.find(:css, drop_container_class).find(:css, ".select2-match").click
29
+ def select_match!(value)
30
+ [value].flatten.each do |value|
31
+ if page.find(:xpath, "//body").has_selector?("#{drop_container_class(value)} li.select2-results__option")
32
+ # select2 version 4.0
33
+ page.find(:xpath, "//body").find("#{drop_container_class(value)} li.select2-results__option", text: value).click
34
+ else
35
+ page.find(:xpath, "//body").find("#{drop_container_class(value)} li.select2-result-selectable", text: value).click
36
+ end
37
+ end
21
38
  end
22
39
 
23
- def initiate!(value)
40
+ def initiate!
24
41
  focus!
25
- search!(value) if search_enabled
26
42
  end
27
43
 
28
44
  private
29
45
  def find_container(options)
30
- if options[:xpath].present?
31
- self.container = page.first(:xpath, options[:xpath])
46
+ if options.has_key? :xpath
47
+ self.container = page.find(:xpath, options[:xpath])
48
+ elsif options.has_key? :css
49
+ self.container = page.find(:css, options[:css])
32
50
  else
33
- label = page.first("label", text: options[:from]) || page.first(options[:from])
34
- self.container = label.find(:xpath, '..').find(".select2-container")
51
+ select_name = options[:from]
52
+ self.container = page.find("label", text: select_name).find(:xpath, '..').find(".select2-container")
35
53
  end
36
54
  end
37
55
 
38
56
  def focus!
39
- css_class = if multiselect then '.select2-choices' else '.select2-choice' end
40
- container.find(css_class).click
41
- end
42
-
43
- def search!(value)
44
- page.find(:xpath, "//body").find("input.select2-input").set(value)
45
- page.execute_script(%|$("input.select2-input:visible").keyup();|)
57
+ if self.container.has_selector?(".select2-selection")
58
+ # select2 version 4.0
59
+ self.container.find(".select2-selection").click
60
+ elsif select2_container.has_selector?(".select2-choice")
61
+ self.container.find(".select2-choice").click
62
+ else
63
+ self.container.find(".select2-choices").click
64
+ end
46
65
  end
47
66
  end
48
67
  end
49
68
  end
50
69
 
51
- # This module gets included in the Cucumber or RSpec World
70
+ # This module gets included in the Cucumber, RSpec, or MiniTest World
52
71
  module Capybara
53
72
  module Select2
54
73
  def select2(value, options = {})
55
- raise "Must pass a hash containing 'from' or 'xpath'" unless options.is_a?(Hash) and [:from, :xpath].any? { |k| options.has_key? k }
74
+ 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 }
75
+
56
76
  element = Capybara::Select2::Element.new(self, options)
57
- element.initiate!(value)
58
- element.select_match!
77
+ element.initiate!
78
+ element.select_match!(value)
59
79
  end
60
80
  end
61
81
  end
@@ -1,3 +1,3 @@
1
1
  module CapybaraSelect2
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara_select2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Miles Z. Sterrett
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-12-02 00:00:00.000000000 Z
11
+ date: 2015-10-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Interact with select2 select boxes with capyabara
@@ -50,7 +45,7 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
53
- - .gitignore
48
+ - ".gitignore"
54
49
  - Gemfile
55
50
  - LICENSE.txt
56
51
  - README.md
@@ -61,32 +56,25 @@ files:
61
56
  homepage: ''
62
57
  licenses:
63
58
  - MIT
59
+ metadata: {}
64
60
  post_install_message:
65
61
  rdoc_options: []
66
62
  require_paths:
67
63
  - lib
68
64
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
65
  requirements:
71
- - - ! '>='
66
+ - - ">="
72
67
  - !ruby/object:Gem::Version
73
68
  version: '0'
74
- segments:
75
- - 0
76
- hash: 1121620845391379523
77
69
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
70
  requirements:
80
- - - ! '>='
71
+ - - ">="
81
72
  - !ruby/object:Gem::Version
82
73
  version: '0'
83
- segments:
84
- - 0
85
- hash: 1121620845391379523
86
74
  requirements: []
87
75
  rubyforge_project:
88
- rubygems_version: 1.8.23
76
+ rubygems_version: 2.4.5
89
77
  signing_key:
90
- specification_version: 3
78
+ specification_version: 4
91
79
  summary: Interact with select2 select boxes with capyabara
92
80
  test_files: []