haya_select_helpers 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3a49ed55099b6d8c40a7cc79dd858ec70a5b62c52256f8651194eafff7ef777
4
- data.tar.gz: a32bfb12dc808215d3bbdcd09ee998ce9b84bdc13a1b351a15b2d73e9d4f837c
3
+ metadata.gz: 96f76d49276820fa73aa7b62dc77f39ce6f1b92ced049fc7ba96bb99be275a34
4
+ data.tar.gz: fa5bdbe4b24df0e7bf0ec7c17b358af38991822881826c96a218f3a1bf688f80
5
5
  SHA512:
6
- metadata.gz: 3265b3febd190d12e916542f458cc536405b17a4375ddae0df083126caaf1077b4777db974be50a66181868d86541ed89b2409ab884c6677610972d47ca28c46
7
- data.tar.gz: 5e05c8c17dbb2840368de065731e6a164c2693c55d2daf1d92a0acd82147473ba974c3cd255f252f8f721087ac8fe0ef232d6e0748ff1e5b31bf55f6b90f6b59
6
+ metadata.gz: eaa631f02a3f44ed87a3070e341f5fd08d0cd85e518f9e6e2e217d4c7167f6491b75f6e282e681b0da5d672e78eee79ffb81a26eff637041d5b06c8a0ba7117f
7
+ data.tar.gz: 9b09bc8269f204a345e18331a2dd49f1080cb0be89a52677f9aa306fde5c2a8ec19d9d389ea7cea8383580b1c27c4c6cf73ce0762f941325d13a8871f4ad5fd0
data/lib/haya_select.rb CHANGED
@@ -93,17 +93,14 @@ class HayaSelect
93
93
  attempts = 0
94
94
 
95
95
  begin
96
+ log_select_start(label, value, allow_if_selected, attempts)
96
97
  guard_already_selected(label, value, allow_if_selected) if attempts.zero?
97
98
 
98
- previous_value = value
99
- open
100
- selected_value = select_option_value(label:, value:)
101
- selected_value = "" if selected_value.nil? && value.nil?
102
- allow_blank = previous_value == selected_value
103
- close_if_open
104
- wait_for_selected_value_or_label(label, value || selected_value, allow_blank:)
99
+ selected_value, allow_blank = select_value_and_close(label:, value:)
100
+ wait_for_selected_after_select(label, value, selected_value, allow_blank)
105
101
  self
106
102
  rescue WaitUtil::TimeoutError, Selenium::WebDriver::Error::StaleElementReferenceError
103
+ log_select_retry(attempts)
107
104
  attempts += 1
108
105
  retry if attempts < 3
109
106
  raise
@@ -111,18 +108,27 @@ class HayaSelect
111
108
  end
112
109
 
113
110
  def guard_already_selected(label, value, allow_if_selected)
114
- current_value = value_no_wait
111
+ return if allow_if_selected
115
112
 
116
- if !value.nil? && current_value == value
117
- return if allow_if_selected
113
+ raise_if_value_already_selected(label, value)
114
+ raise_if_label_already_selected(label, value)
115
+ end
118
116
 
119
- raise "The '#{label || value}'-option is already selected"
120
- end
117
+ def selected_label_for_value(value)
118
+ return nil if value.nil? || value == ""
121
119
 
122
- if value.nil? && !label.nil? && label_no_wait == label
123
- return if allow_if_selected
120
+ was_open = scope.page.has_selector?(options_selector, visible: :all, wait: 0)
121
+ self.open(allow_if_open: true)
124
122
 
125
- raise "The '#{label}'-option is already selected"
123
+ begin
124
+ option = scope.page.first(
125
+ "#{options_selector} [data-class='select-option'][data-value='#{value}']",
126
+ minimum: 0,
127
+ wait: 0
128
+ )
129
+ option&.[]("data-text") || option&.text
130
+ ensure
131
+ close_if_open unless was_open
126
132
  end
127
133
  end
128
134
 
@@ -180,8 +186,17 @@ class HayaSelect
180
186
  raise "No 'label' or 'value' given" if label.nil? && value.nil?
181
187
 
182
188
  selector = select_option_selector(label: label, value: value)
189
+ Rails.logger.debug do
190
+ "[haya_select] select_option_value selector=#{base_selector} option_selector=#{selector} label=#{label.inspect} value=#{value.inspect}"
191
+ end
183
192
  wait_for_option(selector)
184
193
  option = find_option_element(selector, label)
194
+ Rails.logger.debug do
195
+ "[haya_select] option_element selector=#{base_selector} " \
196
+ "data-value=#{option['data-value'].inspect} " \
197
+ "data-disabled=#{option['data-disabled'].inspect} " \
198
+ "data-selected=#{option['data-selected'].inspect}"
199
+ end
185
200
 
186
201
  raise "The '#{label}'-option is disabled" if option['data-disabled'] == 'true'
187
202
 
@@ -237,6 +252,17 @@ class HayaSelect
237
252
  self
238
253
  end
239
254
 
255
+ def selected?(label, value)
256
+ return false unless label || value
257
+
258
+ return true if label_matches?(label)
259
+ return true if value_matches?(value)
260
+
261
+ label_matches_selected_value?(label)
262
+ rescue Selenium::WebDriver::Error::StaleElementReferenceError
263
+ retry
264
+ end
265
+
240
266
  def wait_for_value(expected_value)
241
267
  wait_for_selector(
242
268
  "#{base_selector} [data-class='current-selected'] input[type='hidden'][value='#{expected_value}']",
@@ -247,6 +273,43 @@ class HayaSelect
247
273
 
248
274
  private
249
275
 
276
+ def raise_if_value_already_selected(label, value)
277
+ return if value.nil?
278
+
279
+ current_value = value_no_wait
280
+ return unless current_value == value
281
+
282
+ raise "The '#{label || value}'-option is already selected"
283
+ end
284
+
285
+ def raise_if_label_already_selected(label, value)
286
+ return if label.nil? || !value.nil?
287
+ return if label_no_wait == label
288
+
289
+ current_value = value_no_wait
290
+ return if current_value.nil? || current_value == ""
291
+
292
+ selected_label = selected_label_for_value(current_value)
293
+ return unless selected_label == label
294
+
295
+ raise "The '#{label}'-option is already selected"
296
+ end
297
+
298
+ def value_matches?(value)
299
+ return false unless value
300
+
301
+ scope.page.has_selector?(current_value_selector(value), visible: false)
302
+ end
303
+
304
+ def label_matches_selected_value?(label)
305
+ return false unless label
306
+
307
+ current_value = value_no_wait
308
+ return false if current_value.nil? || current_value == ""
309
+
310
+ selected_label_for_value(current_value) == label
311
+ end
312
+
250
313
  def select_option_selector(label:, value:)
251
314
  if value
252
315
  "#{select_option_container_selector}[data-value='#{value}']"
@@ -286,35 +349,21 @@ private
286
349
  end
287
350
 
288
351
  def wait_for_selected_value_or_label(label, value, allow_blank: false)
352
+ log_wait_for_selected_start(label, value, allow_blank)
353
+ value_input_selector = "#{base_selector} [data-class='current-selected'] input[type='hidden']"
354
+ log_wait_for_selected_initial_state(value_input_selector)
289
355
  wait_for_expect do
290
- value_input_selector = "#{base_selector} [data-class='current-selected'] input[type='hidden']"
291
- has_value_input = scope.page.has_selector?(value_input_selector, visible: false)
292
- label_matches = label && label_matches?(label)
293
- value_matches = value && scope.page.has_selector?(current_value_selector(value), visible: false)
294
- blank_matches = allow_blank && scope.page.has_selector?(current_value_selector(""), visible: false)
295
-
296
- matches =
297
- if has_value_input
298
- value_matches || blank_matches
299
- else
300
- label_matches || value_matches || blank_matches
301
- end
302
-
303
- expect(matches).to eq true
356
+ expect(
357
+ selected_value_or_label_matches?(
358
+ label:,
359
+ value:,
360
+ allow_blank:,
361
+ value_input_selector:
362
+ )
363
+ ).to eq true
304
364
  end
305
365
  end
306
366
 
307
- def selected?(label, value)
308
- return false unless label || value
309
-
310
- label_matches = label && label_matches?(label)
311
- value_matches = value && scope.page.has_selector?(current_value_selector(value), visible: false)
312
-
313
- label_matches || value_matches
314
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
315
- retry
316
- end
317
-
318
367
  def search_for_option(label)
319
368
  return unless scope.page.has_selector?(search_input_selector)
320
369
 
@@ -467,6 +516,8 @@ private
467
516
  end
468
517
 
469
518
  def label_matches?(label)
519
+ return false unless label
520
+
470
521
  current_option_label_selectors.any? do |selector|
471
522
  scope.page.has_selector?(selector, exact_text: label)
472
523
  end
@@ -539,6 +590,11 @@ private
539
590
  end
540
591
 
541
592
  def perform_option_selection(option, label, option_value)
593
+ Rails.logger.debug do
594
+ "[haya_select] perform_option_selection selector=#{base_selector} " \
595
+ "label=#{label.inspect} option_value=#{option_value.inspect} " \
596
+ "data-selected=#{option['data-selected'].inspect}"
597
+ end
542
598
  click_option_element(option)
543
599
  wait_for_selected_value_or_label(label, option_value)
544
600
  end
@@ -547,5 +603,82 @@ private
547
603
  "#{options_selector} [data-class='select-option']"
548
604
  end
549
605
 
606
+ def log_select_start(label, value, allow_if_selected, attempts)
607
+ Rails.logger.debug do
608
+ "[haya_select] select start selector=#{base_selector} " \
609
+ "label=#{label.inspect} value=#{value.inspect} " \
610
+ "allow_if_selected=#{allow_if_selected} attempts=#{attempts}"
611
+ end
612
+ end
613
+
614
+ def select_value_and_close(label:, value:)
615
+ previous_value = value
616
+ Rails.logger.debug { "[haya_select] open selector=#{base_selector}" }
617
+ open
618
+ Rails.logger.debug { "[haya_select] select_option_value selector=#{base_selector}" }
619
+ selected_value = select_option_value(label:, value:)
620
+ Rails.logger.debug do
621
+ "[haya_select] select_option_value selector=#{base_selector} selected_value=#{selected_value.inspect}"
622
+ end
623
+ selected_value = "" if selected_value.nil? && value.nil?
624
+ allow_blank = previous_value == selected_value
625
+ Rails.logger.debug { "[haya_select] close_if_open selector=#{base_selector}" }
626
+ close_if_open
627
+ [selected_value, allow_blank]
628
+ end
629
+
630
+ def wait_for_selected_after_select(label, value, selected_value, allow_blank)
631
+ expected_value = value || selected_value
632
+ Rails.logger.debug do
633
+ "[haya_select] wait_for_selected_value_or_label " \
634
+ "selector=#{base_selector} label=#{label.inspect} " \
635
+ "value=#{expected_value.inspect} allow_blank=#{allow_blank}"
636
+ end
637
+ wait_for_selected_value_or_label(label, expected_value, allow_blank:)
638
+ end
639
+
640
+ def log_select_retry(attempts)
641
+ Rails.logger.debug { "[haya_select] select retry selector=#{base_selector} attempts=#{attempts}" }
642
+ end
643
+
644
+ def log_wait_for_selected_start(label, value, allow_blank)
645
+ Rails.logger.debug do
646
+ "[haya_select] wait_for_selected_value_or_label start selector=#{base_selector} " \
647
+ "label=#{label.inspect} value=#{value.inspect} allow_blank=#{allow_blank}"
648
+ end
649
+ end
650
+
651
+ def log_wait_for_selected_initial_state(value_input_selector)
652
+ has_value_input_initial = scope.page.has_selector?(value_input_selector, visible: false, wait: 0)
653
+ current_value_initial = scope.page.first(value_input_selector, visible: false, wait: 0)&.[](:value)
654
+ current_option = scope.page.first(
655
+ "#{base_selector} [data-class='current-selected'] [data-class='current-option']",
656
+ minimum: 0,
657
+ wait: 0
658
+ )
659
+ current_label_initial =
660
+ if current_option
661
+ option_text = current_option.first("[data-testid='option-presentation-text']", minimum: 0)
662
+ option_text ? option_text.text : current_option.text
663
+ end
664
+
665
+ Rails.logger.debug do
666
+ "[haya_select] wait_for_selected_value_or_label initial " \
667
+ "selector=#{base_selector} has_value_input=#{has_value_input_initial} " \
668
+ "current_value=#{current_value_initial.inspect} " \
669
+ "current_label=#{current_label_initial.inspect}"
670
+ end
671
+ end
672
+
673
+ def selected_value_or_label_matches?(label:, value:, allow_blank:, value_input_selector:)
674
+ has_value_input = scope.page.has_selector?(value_input_selector, visible: false)
675
+ value_matches = value && scope.page.has_selector?(current_value_selector(value), visible: false)
676
+ blank_matches = allow_blank && scope.page.has_selector?(current_value_selector(""), visible: false)
677
+ return value_matches || blank_matches if has_value_input
678
+
679
+ label_matches = label && label_matches?(label)
680
+ label_matches || value_matches || blank_matches
681
+ end
682
+
550
683
  # rubocop:enable Metrics/ClassLength, Style/Documentation
551
684
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HayaSelectHelpers
4
- VERSION = "0.0.21"
4
+ VERSION = "0.0.23"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haya_select_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-02-13 00:00:00.000000000 Z
11
+ date: 2026-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails