Module: Appom::Helpers::ElementHelpers
- Includes:
- Retry::RetryMethods
- Defined in:
- lib/appom/helpers.rb
Overview
Common element interaction patterns
Instance Method Summary collapse
-
#element_contains_text?(element_name, text) ⇒ Boolean
Check if element contains text.
-
#get_attribute_with_fallback(element_name, attribute, fallback_value = nil) ⇒ Object
Get element attribute with fallback.
-
#get_text_with_retry(element_name, retries: 3) ⇒ Object
Get element text with retry.
-
#scroll(direction = :down) ⇒ Object
Basic scroll method for scrolling in specified direction.
-
#scroll_to_and_tap(element_name, direction: :down) ⇒ Object
Scroll to element if needed and tap.
-
#tap_and_wait(element_name, timeout: nil) ⇒ Object
Tap an element and wait for it to be enabled.
-
#wait_and_tap(element_name, timeout: nil) ⇒ Object
Wait for element to be visible and tap.
Methods included from Retry::RetryMethods
#find_with_retry, #interact_with_retry, #wait_for_state_with_retry
Instance Method Details
#element_contains_text?(element_name, text) ⇒ Boolean
Check if element contains text
62 63 64 65 |
# File 'lib/appom/helpers.rb', line 62 def element_contains_text?(element_name, text) element_text = get_text_with_retry(element_name) element_text&.include?(text) || false end |
#get_attribute_with_fallback(element_name, attribute, fallback_value = nil) ⇒ Object
Get element attribute with fallback
54 55 56 57 58 59 |
# File 'lib/appom/helpers.rb', line 54 def get_attribute_with_fallback(element_name, attribute, fallback_value = nil) send(element_name).attribute(attribute) || fallback_value rescue StandardError => e log_warn("Failed to get attribute #{attribute} for #{element_name}: #{e.message}") fallback_value end |
#get_text_with_retry(element_name, retries: 3) ⇒ Object
Get element text with retry
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/appom/helpers.rb', line 31 def get_text_with_retry(element_name, retries: 3) attempt = 0 begin send(element_name).text rescue StandardError => e attempt += 1 raise e unless attempt <= retries sleep(0.5) retry end end |
#scroll(direction = :down) ⇒ Object
Basic scroll method for scrolling in specified direction
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/appom/helpers.rb', line 68 def scroll(direction = :down) # Basic implementation - actual implementation would depend on platform case direction when :down Appom.driver.execute_script('mobile: scrollGesture', { left: 100, top: 100, width: 200, height: 200, direction: 'down', percent: 3.0, }) when :up Appom.driver.execute_script('mobile: scrollGesture', { left: 100, top: 100, width: 200, height: 200, direction: 'up', percent: 3.0, }) end rescue StandardError => e # Fallback scroll for different platforms log_warn("Scroll gesture failed: #{e.message}") end |
#scroll_to_and_tap(element_name, direction: :down) ⇒ Object
Scroll to element if needed and tap
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/appom/helpers.rb', line 88 def scroll_to_and_tap(element_name, direction: :down) max_scrolls = 5 scrolls = 0 while scrolls < max_scrolls return wait_and_tap(element_name) if respond_to?("has_#{element_name}") && send("has_#{element_name}") scroll(direction) scrolls += 1 end raise Appom::ElementNotFoundError.new(element_name, "after #{max_scrolls} scrolls") end |
#tap_and_wait(element_name, timeout: nil) ⇒ Object
Tap an element and wait for it to be enabled
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/appom/helpers.rb', line 18 def tap_and_wait(element_name, timeout: nil) Appom::Helpers.performance_module.time_operation("tap_and_wait_#{element_name}") do timeout ||= Appom.max_wait_time element = send(element_name) element.tap # Wait for element to be enabled if it has an enable checker send("#{element_name}_enable") if respond_to?(:"#{element_name}_enable") element end end |
#wait_and_tap(element_name, timeout: nil) ⇒ Object
Wait for element to be visible and tap
45 46 47 48 49 50 51 |
# File 'lib/appom/helpers.rb', line 45 def wait_and_tap(element_name, timeout: nil) # rubocop:disable Lint/UnusedMethodArgument Appom::Helpers.performance_module.time_operation("wait_and_tap_#{element_name}") do method_name = "has_#{element_name}" send(method_name) if respond_to?(method_name) send(element_name).tap end end |