capybara-select-2 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 01f1574cc4400755884d066349b52d23174b5b61634c9baff7e5332e83687654
4
- data.tar.gz: 5614b845dc929345d9463b7e9de2fdebe483071b5423ff5b0a376add3ac08e5d
2
+ SHA1:
3
+ metadata.gz: 35a699d3e0d5eec84d768a123c94ba5480793e43
4
+ data.tar.gz: da4ddebbf20ea31461c6fb5be32b3ef3d7a3e6b1
5
5
  SHA512:
6
- metadata.gz: 7ebe35a60da59411376f3e473d479fde65b05bd1186caef104b8745564fc5afbe21815bd1f0504a83cb1ba3bd15ae4ead119d4182af5978be9341f5b1591cc61
7
- data.tar.gz: 0cd9d2747b433bbdefd1ad926dbddb35f0790ae10a48206666dc43441a3ab3fd3d7afb03ca2816062972c1c3a6e37dcf1a4ed6858dab95ddefa56f045153f235
6
+ metadata.gz: cc85290a29be0951fa40e6a763ce2f526c59cb6d4c7e438f1e82c7a1a1ed25826464d783774b9b8142f108d8615b6f56e7ca5c5584f42ba594bc9d6683c80040
7
+ data.tar.gz: 7c8324a51abda2745c5d1ce3f9ba0c88407801805f959b6890b41898fe8fa0eedd941ec44ce1685ccc08c971c3042ca156e67d8dcb512224c015181649ff6c20
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
2
  --color
3
+ --require spec_helper
@@ -1,8 +1,9 @@
1
1
  sudo: required
2
- dist: trusty
2
+ dist: xenial
3
3
  language: ruby
4
4
  rvm:
5
5
  - 2.4.1
6
+ - 2.7.1
6
7
  before_install: gem install bundler -v 2.0.1
7
8
  env:
8
9
  global:
data/README.md CHANGED
@@ -19,73 +19,157 @@ And then execute:
19
19
 
20
20
  $ bundle
21
21
 
22
- Or install it yourself as:
22
+ Or install it with `gem install` command:
23
23
 
24
24
  $ gem install capybara-select-2
25
25
 
26
- [Note] In the projects which use RSpec or Cucumber `select2` helper is available out of the box
26
+ ## Configuration
27
27
 
28
- ### Manual installation with Rspec
28
+ ### Minitest
29
29
 
30
- In your spec_helper.rb
30
+ ```ruby
31
+ # application_system_test_case.rb
32
+ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
33
+ include CapybaraSelect2
34
+ include CapybaraSelect2::Helpers # if need specific helpers
35
+ end
36
+ ```
37
+
38
+ ### Rspec
31
39
 
32
40
  ```ruby
41
+ # spec_helper.rb
33
42
  RSpec.configure do |config|
34
43
  config.include CapybaraSelect2
44
+ config.include CapybaraSelect2::Helpers # if need specific helpers
35
45
  end
36
46
  ```
47
+ [Note] In RSpec tests `select2` helper is available out of the box
37
48
 
38
- ### Manual installation with Cucumber
39
-
40
- In your env.rb
49
+ ### Cucumber
41
50
 
42
51
  ```ruby
52
+ # env.rb
43
53
  World CapybaraSelect2
54
+ World CapybaraSelect2::Helpers # if need specific helpers
44
55
  ```
45
56
 
46
57
  ## Usage
47
58
 
48
- ### Select from a node containing select2 control
59
+ ### Examples
49
60
 
50
- * Identified by CSS selector
51
61
  ```ruby
62
+ select2 'Buy Milk', from: 'Todo'
52
63
  select2 'Buy Milk', css: '#todo'
64
+ select2 'Buy Milk', xpath: '//div[@id="todo"]'
53
65
  ```
54
66
 
55
- * Identified by XPath selector
67
+ #### Selecting two or more options
68
+
56
69
  ```ruby
57
- select2 'Buy Milk', xpath: '//div[@id="todo"]'
70
+ select2 'Buy Milk', 'Go to gym', from: 'Todo'
58
71
  ```
59
72
 
60
- ### Select from select2 control identified by label
73
+ #### Searching
74
+
61
75
  ```ruby
62
- select2 'Buy Milk', from: 'Things to do'
76
+ select2 'Buy Milk', from: 'Todo', search: true
63
77
  ```
64
78
 
65
- If you want to search for an option (via Ajax for example)
79
+ #### Searching by text different than an option text
80
+
81
+ ```
82
+ select2 'Buy Milk', from: 'Todo', search: 'Buy'
83
+ ```
84
+
85
+ #### Tagging
86
+
66
87
  ```ruby
67
- select2 'Buy Milk', from: 'Things to do', search: true
88
+ select2 'Go to gym', from: 'Todo', tag: true
68
89
  ```
69
90
 
70
- ### Dynamically create an option from search input
91
+ #### Resolving ambiguous match
92
+
71
93
  ```ruby
72
- select2 'Millennials', from: 'Generations', tag: true
94
+ # Select the first matching option if there are two or more options with text 'Buy'
95
+ select2 'Buy', from: 'Todo', match: :first
73
96
  ```
74
97
 
75
- ### Select several options at once
98
+ #### Selecting an option by exact text
99
+
76
100
  ```ruby
77
- select2 'Buy Milk', 'Go to gym', css: '#todo'
101
+ # Select 'Eat' if there are two or more options with text 'Eat' ('Eat', 'Eat salad')
102
+ select2 'Eat', from: 'Todo', exact_text: true
78
103
  ```
79
104
 
80
- ### Add match strategy in case of ambiguous results
105
+ [Note] CSS and XPath selectors must identify an HTML node that wraps select2 element or a select2 element itself (an HTML element with the `.select2-container` class)
106
+
107
+ ### Options
108
+
109
+ Option | Purpose
110
+ :------|:-------
111
+ `css` | Identify select2 element by a CSS selector
112
+ `xpath` | Identify select2 element by an XPath selector
113
+ `from` | Identify select2 element by a label
114
+ `label` | Identify select2 element by a label
115
+ `search` | Search for an option by the passed string. Search by an option text if `true` is passed
116
+ `tag` | Create an option
117
+ `match` | Specifies Capybara's [matching strategy](https://github.com/teamcapybara/capybara#strategy) when selecting an option
118
+ `exact_text` | Whether an option text must match exactly
119
+
120
+ ### Helpers
121
+
122
+ Specific select2 helpers that allow more refined access to a select2 control
123
+
81
124
  ```ruby
82
- select2 'Buy Milk', from: 'Things to do', search: true, match: :first
125
+ select2_open label: 'Todo'
126
+ select2_close
127
+ select2_search 'Milk', css: '#todo'
128
+ select2_select 'Buy Milk', from: 'Todo'
129
+ select2_clear xpath: "//div[@id='todo']"
83
130
  ```
84
131
 
85
- ### Check for select2 option on the page
132
+ Helper | Purpose | Options
133
+ :------|:--------|:-------
134
+ `select2_open(options)` | Open select2 control | `label`, `css`, `xpath`
135
+ `select2_close` | Close select2 control | -
136
+ `select2_search(term, options)` | Type into a select2 search field | `label`, `from`, `css`, `xpath`
137
+ `select2_select(value, options)` | Select an option from an opened select2 control | `label`, `from`, `css`, `xpath`, `match`, `exact_text`
138
+ `select2_clear(options)` | Remove selected options (for multi select only) | `label`, `from`, `css`, `xpath`
139
+
140
+ See description for each option in the [Options](https://github.com/Hirurg103/capybara_select2#options) section
141
+
142
+ [Note] Helpers above are not available in tests by default. To use them include `CapybaraSelect2::Helpers` in your test invironment (see [Configuration section](https://github.com/Hirurg103/capybara_select2#configuration))
143
+
144
+ ### RSpec matchers
145
+
86
146
  ```ruby
87
- expect(page).to have_select2_option('Buy Milk')
147
+ # Check that a select2 option with specified text is present on the page
148
+ expect(page).to have_select2_option 'Buy Milk'
149
+ ```
150
+
151
+ ## Testing
152
+
153
+ ### See test examples
154
+
155
+ To see test examples for a specific select2 version, start Sinatra app first:
156
+
157
+ ```bash
158
+ $ rackup spec/support/select2_examples/config.ru
88
159
  ```
160
+
161
+ Visit http://localhost:9292/select2/v4.0.5/examples in your browser to see examples for select2 version `4.0.5`
162
+
163
+ ### Running tests
164
+
165
+ ```bash
166
+ # run spec cases for all select2 versions
167
+ $ bundle exec rspec
168
+
169
+ # run spec cases for a specific select2 version
170
+ $ SELECT2_VERSION=4.0.5 bundle exec rspec spec/shared
171
+ ```
172
+
89
173
  ## Contributing
90
174
 
91
175
  1. Add a test case which covers the bug
@@ -27,13 +27,14 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_development_dependency "bundler", "~> 2.0"
30
- spec.add_development_dependency "rake", "~> 10.0"
31
- spec.add_development_dependency "rspec", "~> 3.0"
32
- spec.add_development_dependency "capybara", "~> 3.12"
33
- spec.add_development_dependency "chromedriver-helper", "~> 2.1"
34
- spec.add_development_dependency "selenium-webdriver", "~> 3.141.0"
30
+ spec.add_development_dependency "rake", "~> 13.0"
31
+ spec.add_development_dependency "rspec", "~> 3.9"
32
+ spec.add_development_dependency "capybara", "~> 3.31"
33
+ spec.add_development_dependency "selenium-webdriver", "~> 3.142"
34
+ spec.add_development_dependency "webdrivers", "~> 4.2"
35
35
  spec.add_development_dependency "capybara-screenshot" , "~> 1.0"
36
- spec.add_development_dependency "puma", "~> 3.12"
37
- spec.add_development_dependency "pry", "~> 0.11.0"
38
- spec.add_development_dependency "simplecov", "~> 0.16"
36
+ spec.add_development_dependency "puma", "~> 4.3"
37
+ spec.add_development_dependency "pry", "~> 0.12"
38
+ spec.add_development_dependency "simplecov", "~> 0.18"
39
+ spec.add_development_dependency "sinatra", "~> 2.0"
39
40
  end
@@ -1,8 +1,32 @@
1
1
  require "capybara_select2/version"
2
+ require 'capybara_select2/utils'
2
3
  require 'capybara_select2/helpers'
3
4
 
4
5
  module CapybaraSelect2
5
- include Helpers
6
+
7
+ def select2(*args)
8
+ options = args.pop
9
+ values = args
10
+
11
+ Utils.validate_options!(options)
12
+
13
+ container = Utils.find_select2_container(options, page)
14
+ version = Utils.detect_select2_version(container)
15
+ options_with_select2_details =
16
+ options.merge({ container: container, version: version, page: page })
17
+
18
+ values.each do |value|
19
+ Helpers.select2_open(options_with_select2_details)
20
+
21
+ if options[:search] || options[:tag]
22
+ term = options[:search].is_a?(String) ? options[:search] : value
23
+ Helpers.select2_search(term, options_with_select2_details)
24
+ end
25
+
26
+ Helpers.select2_select(value, options_with_select2_details)
27
+ end
28
+ end
29
+
6
30
  end
7
31
 
8
32
  if defined?(RSpec)
@@ -1,32 +1,54 @@
1
1
  require 'capybara_select2/utils'
2
+ require 'capybara_select2/selectors'
2
3
 
3
4
  module CapybaraSelect2
4
5
  module Helpers
6
+ module_function
5
7
 
6
- def select2(*args)
7
- options = args.pop
8
- values = args
8
+ def select2_open(options)
9
+ options = Utils.set_option_aliases(options)
10
+ Utils.validate_options!(options)
11
+
12
+ _, container, version = Utils.get_page_container_and_version(options, self)
13
+ opener_selector = Selectors.opener_selector(version)
14
+
15
+ container.find(:css, opener_selector).click
16
+ end
17
+
18
+ def select2_close(options = {})
19
+ page.find(:css, 'body').click
20
+ end
21
+
22
+ def select2_search(text, options)
23
+ options = Utils.set_option_aliases(options)
24
+ Utils.validate_options!(options)
25
+
26
+ page, _, version = Utils.get_page_container_and_version(options, self)
27
+ search_input_selector = Selectors.search_input_selector(version)
28
+
29
+ page.find(:xpath, '//body').find(:css, search_input_selector).set text
30
+ end
9
31
 
32
+ def select2_select(value, options)
10
33
  Utils.validate_options!(options)
11
34
 
12
- select2_container = Utils.find_select2_container(options, page)
13
- select2_version = Utils.detect_select2_version(select2_container)
35
+ page, _, version = Utils.get_page_container_and_version(options, self)
36
+ option_selector = Selectors.option_selector(version)
14
37
 
15
- opener_selector = Selectors.opener_selector(select2_version)
16
- option_selector = Selectors.option_selector(select2_version)
38
+ find_options = options.select { |k, _| [:match, :exact_text].include?(k) }
39
+ find_options = find_options.merge(text: value)
17
40
 
18
- values.each do |value|
19
- select2_container.find(:css, opener_selector).click
41
+ page.find(:xpath, '//body').find(:css, option_selector, **find_options).click
42
+ end
43
+
44
+ def select2_clear(options)
45
+ options = Utils.set_option_aliases(options)
46
+ Utils.validate_options!(options)
20
47
 
21
- if options[:search] || options[:tag]
22
- search_input_selector = Selectors.search_input_selector(select2_version)
23
- find(:xpath, '//body').find(:css, search_input_selector).set value
24
- end
48
+ _, container, version = Utils.get_page_container_and_version(options, self)
49
+ remove_option_selector = Selectors.remove_option_selector(version)
25
50
 
26
- find_options = { text: value }
27
- find_options[:match] = options[:match] if options[:match]
28
- find(:xpath, '//body').find(:css, option_selector, find_options).click
29
- end
51
+ container.all(remove_option_selector).map(&:click)
30
52
  end
31
53
 
32
54
  end
@@ -26,13 +26,23 @@ module CapybaraSelect2
26
26
 
27
27
  OptionSelectors = {
28
28
  '2' => ".select2-container-active .select2-result",
29
- '3' => ".select2-drop-active .select2-result",
30
- '4' => ".select2-results .select2-results__option[role='treeitem']"
29
+ '3' => ".select2-drop-active .select2-result-label",
30
+ '4' => ".select2-results .select2-results__option:not([role='group'])"
31
31
  }.freeze
32
32
 
33
33
  def option_selector(select2_version)
34
34
  OptionSelectors.fetch(select2_version)
35
35
  end
36
36
 
37
+ RemoveOptionSelectors = {
38
+ '2' => '.select2-search-choice-close',
39
+ '3' => '.select2-search-choice-close',
40
+ '4' => '.select2-selection__choice__remove'
41
+ }.freeze
42
+
43
+ def remove_option_selector(select2_version)
44
+ RemoveOptionSelectors.fetch(select2_version)
45
+ end
46
+
37
47
  end
38
48
  end
@@ -1,6 +1,10 @@
1
1
  module CapybaraSelect2
2
2
  module Utils
3
3
 
4
+ def self.set_option_aliases(options)
5
+ options.dup.tap { |o| o[:from] ||= o[:label] }
6
+ end
7
+
4
8
  def self.detect_select2_version(container)
5
9
  if container['class'] =~ /^select2\s/
6
10
  '4'
@@ -35,5 +39,13 @@ module CapybaraSelect2
35
39
  end
36
40
  end
37
41
 
42
+ def self.get_page_container_and_version(options, context)
43
+ page = options[:page] || context.page
44
+ container = options[:container] || find_select2_container(options, page)
45
+ version = options[:version] || detect_select2_version(container)
46
+
47
+ [page, container, version]
48
+ end
49
+
38
50
  end
39
51
  end
@@ -1,3 +1,3 @@
1
1
  module CapybaraSelect2
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-select-2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Kavalionak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2020-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,70 +30,70 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.9'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '3.9'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: capybara
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.12'
61
+ version: '3.31'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.12'
68
+ version: '3.31'
69
69
  - !ruby/object:Gem::Dependency
70
- name: chromedriver-helper
70
+ name: selenium-webdriver
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '2.1'
75
+ version: '3.142'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '2.1'
82
+ version: '3.142'
83
83
  - !ruby/object:Gem::Dependency
84
- name: selenium-webdriver
84
+ name: webdrivers
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.141.0
89
+ version: '4.2'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 3.141.0
96
+ version: '4.2'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: capybara-screenshot
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -114,42 +114,56 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '3.12'
117
+ version: '4.3'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '3.12'
124
+ version: '4.3'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: pry
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 0.11.0
131
+ version: '0.12'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 0.11.0
138
+ version: '0.12'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: simplecov
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '0.16'
145
+ version: '0.18'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '0.16'
152
+ version: '0.18'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sinatra
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.0'
153
167
  description:
154
168
  email:
155
169
  - dzm.kov@gmail.com
@@ -170,8 +184,6 @@ files:
170
184
  - bin/console
171
185
  - bin/setup
172
186
  - capybara_select2.gemspec
173
- - ci/install-chrome
174
- - ci/install-chromedriver
175
187
  - lib/capybara-select-2.rb
176
188
  - lib/capybara_select2.rb
177
189
  - lib/capybara_select2/helpers.rb
@@ -199,7 +211,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
211
  - !ruby/object:Gem::Version
200
212
  version: '0'
201
213
  requirements: []
202
- rubygems_version: 3.0.2
214
+ rubyforge_project:
215
+ rubygems_version: 2.6.11
203
216
  signing_key:
204
217
  specification_version: 4
205
218
  summary: Capybara helpers for https://select2.org select box (supports Select2 versions
@@ -1,11 +0,0 @@
1
- #!/bin/bash
2
-
3
- set -xe
4
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
5
- echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
6
-
7
- # Update packages
8
- apt-get update -yqqq
9
-
10
- # Install Chrome browser
11
- apt-get install -y google-chrome-stable
@@ -1,23 +0,0 @@
1
- #!/bin/bash
2
-
3
- set -xe
4
-
5
- # Update packages
6
- apt-get update -yqqq
7
-
8
- # Install Chrome browser
9
- apt-get install -y unzip
10
-
11
- # Install/use specific version of chrome if an installed version is lower
12
- install_chromedriver() {
13
- wget -O $TMPDIR/chromedriver.zip https://chromedriver.storage.googleapis.com/${MIN_CHROMEDRIVER_VER}/chromedriver_linux64.zip
14
- mkdir -p $HOME/.chromedriver
15
- (cd $HOME/.chromedriver && unzip $TMPDIR/chromedriver.zip && chmod +x chromedriver)
16
- rm -f $TMPDIR/chromedriver.zip
17
- ln -s $HOME/.chromedriver/chromedriver /usr/local/bin/chromedriver
18
- }
19
- if ! which chromedriver; then
20
- install_chromedriver
21
- elif dpkg --compare-versions `chromedriver --version | cut -d " " -f2` lt $MIN_CHROMEDRIVER_VER; then
22
- install_chromedriver
23
- fi