capybara-select-2 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -3
- data/lib/capybara_select2.rb +24 -1
- data/lib/capybara_select2/helpers.rb +49 -18
- data/lib/capybara_select2/selectors.rb +10 -0
- data/lib/capybara_select2/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaba1602e5f227889734248ce001fe00831ead8a
|
4
|
+
data.tar.gz: ddbe5b57d2cb74225f29c034c12a1466c7e55804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8c3a064c252b1d3fe56c3642e8e6920061ff866d34b5752f237f75c71172299af60201f2afd56518db482bfc08878cac08706e804dc24467e017244088618fa
|
7
|
+
data.tar.gz: 48253431a5c902523dfc031a615b160db8f5ed849b860a8ce8d7a8d2a1a83a33333f9936ffdf3f434995dd96befb66e25a4ce0cc00e624c269fb57e148739ad9
|
data/README.md
CHANGED
@@ -30,7 +30,8 @@ Or install it with `gem install` command:
|
|
30
30
|
```ruby
|
31
31
|
# application_system_test_case.rb
|
32
32
|
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
|
33
|
-
include CapybaraSelect2
|
33
|
+
include CapybaraSelect2
|
34
|
+
include CapybaraSelect2::Helpers # if need specific helpers
|
34
35
|
end
|
35
36
|
```
|
36
37
|
|
@@ -40,6 +41,7 @@ end
|
|
40
41
|
# spec_helper.rb
|
41
42
|
RSpec.configure do |config|
|
42
43
|
config.include CapybaraSelect2
|
44
|
+
config.include CapybaraSelect2::Helpers # if need specific helpers
|
43
45
|
end
|
44
46
|
```
|
45
47
|
[Note] In RSpec tests `select2` helper is available out of the box
|
@@ -49,6 +51,7 @@ end
|
|
49
51
|
```ruby
|
50
52
|
# env.rb
|
51
53
|
World CapybaraSelect2
|
54
|
+
World CapybaraSelect2::Helpers # if need specific helpers
|
52
55
|
```
|
53
56
|
|
54
57
|
## Usage
|
@@ -62,6 +65,7 @@ select2 'Buy Milk', from: 'Things to do'
|
|
62
65
|
select2 'Buy Milk', 'Go to gym', css: '#todo'
|
63
66
|
|
64
67
|
select2 'Buy Milk', from: 'Things to do', search: true
|
68
|
+
select2 'Buy Milk', from: 'Things to do', search: 'Buy'
|
65
69
|
select2 'Millennials', from: 'Generation', tag: true
|
66
70
|
```
|
67
71
|
|
@@ -74,11 +78,33 @@ Option | Purpose
|
|
74
78
|
css | Identify select2 element by a CSS selector
|
75
79
|
xpath | Identify select2 element by an XPath selector
|
76
80
|
from | Identify select2 element by a label
|
77
|
-
search | Search for an option
|
81
|
+
search | Search for an option by the passed string. Search by an option text if `true` passed
|
78
82
|
tag | Create an option
|
79
83
|
match | Specifies Capybara's [matching strategy](https://github.com/teamcapybara/capybara#strategy) when selecting an option
|
80
84
|
exact_text | Whether an option text must match exactly
|
81
85
|
|
86
|
+
### Helpers
|
87
|
+
|
88
|
+
Specific select2 helpers that allow more refined access to select2 control
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
select2_open label: 'Todo'
|
92
|
+
select2_close
|
93
|
+
select2_search 'Milk', css: '#todo'
|
94
|
+
select2_select 'Buy Milk', from: 'Todo'
|
95
|
+
select2_clear xpath: "//div[@id='todo']"
|
96
|
+
```
|
97
|
+
|
98
|
+
Helper | Purpose
|
99
|
+
:------|:-------
|
100
|
+
select2_open | Open select2 control
|
101
|
+
select2_close | Close select2 control
|
102
|
+
select2_search | Type into a select2 search field
|
103
|
+
select2_select | Select an option from an opened select2 control
|
104
|
+
select2_clear | Remove selected options (for multi select only)
|
105
|
+
|
106
|
+
[!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))
|
107
|
+
|
82
108
|
### RSpec matchers
|
83
109
|
|
84
110
|
```ruby
|
@@ -105,7 +131,7 @@ Visit http://localhost:9292/select2/v4.0.5/examples in your browser to see examp
|
|
105
131
|
$ bundle exec rspec
|
106
132
|
|
107
133
|
# run spec cases for a specific select2 version
|
108
|
-
$
|
134
|
+
$ SELECT2_VERSION=4.0.5 bundle exec rspec spec/shared
|
109
135
|
```
|
110
136
|
|
111
137
|
## Contributing
|
data/lib/capybara_select2.rb
CHANGED
@@ -1,8 +1,31 @@
|
|
1
1
|
require "capybara_select2/version"
|
2
|
+
require 'capybara_select2/utils'
|
2
3
|
require 'capybara_select2/helpers'
|
3
4
|
|
4
5
|
module CapybaraSelect2
|
5
|
-
|
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
|
+
extended_options = options.merge({ container: container, version: version, page: page })
|
16
|
+
|
17
|
+
values.each do |value|
|
18
|
+
Helpers.select2_open(extended_options)
|
19
|
+
|
20
|
+
if options[:search] || options[:tag]
|
21
|
+
term = options[:search].is_a?(String) ? options[:search] : value
|
22
|
+
Helpers.select2_search(term, extended_options)
|
23
|
+
end
|
24
|
+
|
25
|
+
Helpers.select2_select(value, extended_options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
6
29
|
end
|
7
30
|
|
8
31
|
if defined?(RSpec)
|
@@ -3,32 +3,63 @@ require 'capybara_select2/selectors'
|
|
3
3
|
|
4
4
|
module CapybaraSelect2
|
5
5
|
module Helpers
|
6
|
+
module_function
|
6
7
|
|
7
|
-
def
|
8
|
-
options =
|
9
|
-
|
8
|
+
def select2_open(options)
|
9
|
+
options = options.dup
|
10
|
+
options[:from] ||= options[:label]
|
11
|
+
Utils.validate_options!(options)
|
12
|
+
|
13
|
+
_page = options[:page] || page
|
14
|
+
container = options[:container] || Utils.find_select2_container(options, _page)
|
15
|
+
version = options[:version] || Utils.detect_select2_version(container)
|
16
|
+
opener_selector = Selectors.opener_selector(version)
|
17
|
+
|
18
|
+
container.find(:css, opener_selector).click
|
19
|
+
end
|
20
|
+
|
21
|
+
def select2_close(options = {})
|
22
|
+
page.find(:css, 'body').click
|
23
|
+
end
|
24
|
+
|
25
|
+
def select2_search(text, options)
|
26
|
+
options = options.dup
|
27
|
+
options[:from] ||= options[:label]
|
28
|
+
Utils.validate_options!(options)
|
29
|
+
|
30
|
+
_page = options[:page] || page
|
31
|
+
container = options[:container] || Utils.find_select2_container(options, _page)
|
32
|
+
version = options[:version] || Utils.detect_select2_version(container)
|
33
|
+
search_input_selector = Selectors.search_input_selector(version)
|
34
|
+
|
35
|
+
_page.find(:xpath, '//body').find(:css, search_input_selector).set text
|
36
|
+
end
|
10
37
|
|
38
|
+
def select2_select(value, options)
|
11
39
|
Utils.validate_options!(options)
|
12
40
|
|
13
|
-
|
14
|
-
|
41
|
+
_page = options[:page] || page
|
42
|
+
container = options[:container] || Utils.find_select2_container(options, _page)
|
43
|
+
version = options[:version] || Utils.detect_select2_version(container)
|
44
|
+
option_selector = Selectors.option_selector(version)
|
15
45
|
|
16
|
-
|
17
|
-
|
46
|
+
find_options = { text: value }
|
47
|
+
find_options[:match] = options[:match] if options[:match]
|
48
|
+
find_options[:exact_text] = options[:exact_text] if options[:exact_text]
|
18
49
|
|
19
|
-
|
20
|
-
|
50
|
+
_page.find(:xpath, '//body').find(:css, option_selector, find_options).click
|
51
|
+
end
|
52
|
+
|
53
|
+
def select2_clear(options)
|
54
|
+
options = options.dup
|
55
|
+
options[:from] ||= options[:label]
|
56
|
+
Utils.validate_options!(options)
|
21
57
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
58
|
+
container = Utils.find_select2_container(options, page)
|
59
|
+
version = Utils.detect_select2_version(container)
|
60
|
+
remove_option_selector = Selectors.remove_option_selector(version)
|
26
61
|
|
27
|
-
|
28
|
-
find_options[:match] = options[:match] if options[:match]
|
29
|
-
find_options[:exact_text] = options[:exact_text] if options[:exact_text]
|
30
|
-
find(:xpath, '//body').find(:css, option_selector, find_options).click
|
31
|
-
end
|
62
|
+
container.all(remove_option_selector).map(&:click)
|
32
63
|
end
|
33
64
|
|
34
65
|
end
|
@@ -34,5 +34,15 @@ module CapybaraSelect2
|
|
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
|
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
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dzmitry Kavalionak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|