unobtainium-multifind 0.1.0 → 0.2.0

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
2
  SHA1:
3
- metadata.gz: b372724a026239958b189cca8eef2da19be56b32
4
- data.tar.gz: 09e80f23d67eaa9038944a0d9b85eae6a04609c6
3
+ metadata.gz: 392fa2fb38d4cec4325a05a5b69243a5f9f4be07
4
+ data.tar.gz: c56559898399e3d17d8642d6e819c8983aa5edbb
5
5
  SHA512:
6
- metadata.gz: c593de1984c2fd92e32379caf34104037fd249c7349c590c60df58042b349d5e67577977146ad1e85d4bd7e036e9a7d7b74897f6d49f3b92f93ae0729e10ad3d
7
- data.tar.gz: 008eb7a5e629f1cf95113287c18f60d861af673e68bc17c4a00665d8eae0595cccc0542d30987e4563b33f7204f830d69c581795d1d850d0291e6eb77e5a5a43
6
+ metadata.gz: 1774529bf9115b1ab19348ba2e34acf6110d841f90a2aea769f40ade372cceddc7224a120d06048b25255f7ce9ec731a058735ec29fc79b8c1d09b025120fa63
7
+ data.tar.gz: ec8e6dff393173d2a35eb9547b79de88b37cda555a74c5df10ea8557b598f69ae7ac8de12b544b3c37f9b10ecda6f21091cacb27c783d8f8c2f28a28ccc0ea6d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unobtainium-multifind (0.1.0)
4
+ unobtainium-multifind (0.2.0)
5
5
  unobtainium (~> 0.5)
6
6
 
7
7
  GEM
@@ -32,6 +32,10 @@ module Unobtainium
32
32
  # If :last is specified, the last non-error result is
33
33
  # returned.
34
34
  find: :all,
35
+ # Defaults to only finding :displayed? elements. You can use any method
36
+ # that Selenium::WebDriver::Element responds to, or :exists? if you only
37
+ # care whether the element exists.
38
+ check_element: :displayed?,
35
39
  }.freeze
36
40
 
37
41
  class << self
@@ -48,6 +52,13 @@ module Unobtainium
48
52
  attr_accessor :multifind_options
49
53
  @multifind_options = DEFAULT_OPTIONS
50
54
 
55
+ ##
56
+ # Find multiple elements. Each argument is a Hash of selector options
57
+ # that are passed to #find_element. If one argument contains keys from
58
+ # the DEFAULT_OPTIONS Hash, it is instead treated as an options Hash for
59
+ # the #multifind method.
60
+ # @return Array of found elements or nil entries if no matching element
61
+ # was found.
51
62
  def multifind(*args)
52
63
  # Parse options
53
64
  options, selectors = multifind_parse_options(*args)
@@ -118,6 +129,15 @@ module Unobtainium
118
129
  ":last, but is: #{options[:find]}"
119
130
  end
120
131
 
132
+ # Ensure that 'check_element' contains only valid options.
133
+ elem_klass = ::Selenium::WebDriver::Element
134
+ if options[:check_element] != :exists? and
135
+ not elem_klass.instance_methods.include?(options[:check_element])
136
+ raise ArgumentError, ":check_element must either be :exists? or "\
137
+ "a boolean method that ::Selenium::WebDriver::Element responds to, "\
138
+ "but got: #{options[:check_element]}"
139
+ end
140
+
121
141
  return options, selectors
122
142
  end
123
143
 
@@ -125,6 +145,40 @@ module Unobtainium
125
145
  # Filters results from multifind; this largely means honouring the
126
146
  # :find option.
127
147
  def multifind_filter_results(options, results)
148
+ results = multifind_collapse_results(options, results)
149
+
150
+ # If we're only checking for existence, we're done here.
151
+ if options[:check_element] == :exists?
152
+ return results
153
+ end
154
+
155
+ # Filter all results according to the :check_element option
156
+ filtered = []
157
+ results.each do |result|
158
+ if result.nil?
159
+ filtered << result
160
+ next
161
+ end
162
+ if result.is_a?(::Selenium::WebDriver::Error::NoSuchElementError)
163
+ filtered << result
164
+ next
165
+ end
166
+
167
+ # Run check_element
168
+ if result.send(options[:check_element])
169
+ filtered << result
170
+ next
171
+ end
172
+
173
+ filtered << nil
174
+ end
175
+
176
+ return filtered
177
+ end
178
+
179
+ ##
180
+ # Collapses results to only return what's specified by :find.
181
+ def multifind_collapse_results(options, results)
128
182
  # That was easy!
129
183
  if options[:find] == :all
130
184
  return results
@@ -9,6 +9,6 @@
9
9
  module Unobtainium
10
10
  module MultiFind
11
11
  # The current release version
12
- VERSION = "0.1.0".freeze
12
+ VERSION = "0.2.0".freeze
13
13
  end # module MultiFind
14
14
  end # module Unobtainium
data/spec/data/foo.html CHANGED
@@ -1,4 +1,5 @@
1
1
  <foo>
2
2
  <bar>
3
- <something>else</something>
3
+ <something>else</something></bar>
4
+ <hidden style="display: none"></hidden>
4
5
  <foo>
data/spec/module_spec.rb CHANGED
@@ -148,6 +148,29 @@ describe 'Unobtainium::MultiFind::DriverModule' do
148
148
  expect(elem).to be_empty
149
149
  end
150
150
 
151
+ it "does not find hidden elements by default" do
152
+ drv = @tester.driver(DRIVER)
153
+ drv.navigate.to(TEST_URL)
154
+
155
+ elem = drv.find(xpath: '//foo/hidden')
156
+ expect(elem).not_to be_nil
157
+ expect(elem).not_to be_empty
158
+ expect(elem[0]).to be_nil
159
+ end
160
+
161
+ it "finds hidden element with :exists?" do
162
+ drv = @tester.driver(DRIVER)
163
+ drv.navigate.to(TEST_URL)
164
+
165
+ # rubocop:disable Style/BracesAroundHashParameters
166
+ elem = drv.find({ xpath: '//hidden' },
167
+ { check_element: :exists? })
168
+ # rubocop:enable Style/BracesAroundHashParameters
169
+ expect(elem).not_to be_nil
170
+ expect(elem).not_to be_empty
171
+ expect(elem[0]).not_to be_nil
172
+ end
173
+
151
174
  it "passes non-hash arguments without touching them" do
152
175
  drv = @tester.driver(DRIVER)
153
176
  drv.navigate.to(TEST_URL)
@@ -212,5 +235,22 @@ describe 'Unobtainium::MultiFind::DriverModule' do
212
235
  drv.find(xpath: '//foo')
213
236
  end.to raise_error
214
237
  end
238
+
239
+ it "validates :check_element" do
240
+ drv = @tester.driver(DRIVER)
241
+ drv.navigate.to(TEST_URL)
242
+
243
+ # Good option
244
+ drv.multifind_options = { check_element: :exists? }
245
+ expect do
246
+ drv.find(xpath: '//foo')
247
+ end.not_to raise_error
248
+
249
+ # Bad option
250
+ drv.multifind_options = { check_element: :foobar }
251
+ expect do
252
+ drv.find(xpath: '//foo')
253
+ end.to raise_error
254
+ end
215
255
  end
216
256
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unobtainium-multifind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Finkhaeuser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler