Module: Appom::ElementFinder

Includes:
Logging
Included in:
Page, Section
Defined in:
lib/appom/element_finder.rb

Overview

Element finding functionality for Appom automation framework Handles element location with various strategies and options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

level, level=, #log_debug, #log_element_action, #log_error, #log_info, #log_wait_end, #log_wait_start, #log_warn, #logger

Class Method Details

.included(klass) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/appom/element_finder.rb', line 8

def self.included(klass)
  # Include cache-aware finder if caching is enabled

  klass.include(ElementCache::CacheAwareFinder) if Appom.cache_config[:enabled]
rescue StandardError
  # Continue without caching if it fails to load
end

Instance Method Details

#_all(*find_args) ⇒ Object

Find elements



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/appom/element_finder.rb', line 60

def _all(*find_args)
  args, text, visible = deduce_element_args(find_args)
  elements = page.find_elements(*args)
  els = []

  elements.each do |element|
    if !visible.nil? && !text.nil?
      els.push(element) if element.displayed? && element.text == text
    elsif !visible.nil?
      els.push(element) if element.displayed?
    elsif !text.nil?
      els.push(element) if element.text == text
    else
      els.push(element)
    end
  end
  els
end

#_check_has_element(*find_args) ⇒ Object

Check page has or has not element with find_args If page has element return TRUE else return FALSE



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/appom/element_finder.rb', line 81

def _check_has_element(*find_args)
  args, text, visible = deduce_element_args(find_args)
  elements = page.find_elements(*args)

  return !elements.empty? if visible.nil? && text.nil?

  is_found = false
  elements.each do |element|
    if !visible.nil? && !text.nil?
      is_found = true if element.displayed? && element.text == text
    elsif !visible.nil?
      is_found = true if element.displayed?
    elsif !text.nil?
      is_found = true if element.text == text
    end
  end
  is_found
end

#_find(*find_args) ⇒ Object

Find an element



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/appom/element_finder.rb', line 17

def _find(*find_args)
  args, text, visible = deduce_element_args(find_args)
  wait = Appom::Wait.new(timeout: Appom.max_wait_time)

  log_debug('Finding element', { args: args, text: text, visible: visible })
  start_time = Time.now

  wait.until do
    elements = page.find_elements(*args)
    elements.each do |element|
      if !visible.nil? && !text.nil?
        if element.displayed? && element.text == text
          duration = ((Time.now - start_time) * 1000).round(2)
          log_element_action('FOUND', "element with #{args.join(', ')}", duration)
          return element
        end
      elsif !visible.nil?
        if element.displayed?
          duration = ((Time.now - start_time) * 1000).round(2)
          log_element_action('FOUND', "element with #{args.join(', ')}", duration)
          return element
        end
      elsif !text.nil?
        if element.text == text
          duration = ((Time.now - start_time) * 1000).round(2)
          log_element_action('FOUND', "element with #{args.join(', ')}", duration)
          return element
        end
      # Just return first element
      else
        duration = ((Time.now - start_time) * 1000).round(2)
        log_element_action('FOUND', "element with #{args.join(', ')}", duration)
        return element
      end
    end
    raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time)
  end
rescue Appom::WaitError
  log_error('Element not found', { args: find_args, timeout: Appom.max_wait_time })
  raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time)
end

#wait_until(type, *find_args) ⇒ Object

Function is used to check Note: Function WILL NOT RETURN ELEMENT



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/appom/element_finder.rb', line 118

def wait_until(type, *find_args)
  wait = Appom::Wait.new(timeout: Appom.max_wait_time)
  wait.until do
    case type
    # Function only return true if element enabled or raise an error if time out
    when 'element enable'
      _find(*find_args).enabled?
    # Function only return true if element disabled or raise an error if time out
    when 'element disable'
      result = _find(*find_args)
      raise StandardError, "Still found an element enable with args = #{find_args}" if result.enabled?

      return true
    # Function only return true if we can find at least one element (array is not empty) or raise error
    when 'at least one element exists'
      result = _all(*find_args)
      raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time) if result.empty?

      return true

    # Function only return true if we can't find at least one element (array is empty) or raise error
    when 'no element exists'
      result = _all(*find_args)
      unless result.empty?
        message = "Still found #{result.size} element#{'s' if result.size > 1}"
        raise Appom::ElementError.new(message, { elements_found: result.size, selector: find_args.join(', ') })
      end
      return true
    end
  end
end

#wait_until_get_not_empty(*find_args) ⇒ Object

Use wait to get elements Before timeout we will try to find elements until response return array is not empty



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/appom/element_finder.rb', line 104

def wait_until_get_not_empty(*find_args)
  wait = Appom::Wait.new(timeout: Appom.max_wait_time)
  wait.until do
    result = page.find_elements(*find_args)
    # If response is empty we will return false to make it not pass Wait condition
    raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time) if result.empty?

    # Return result
    return result
  end
end