Module: Appom::ElementFinder

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

Instance Method Summary collapse

Instance Method Details

#_all(*find_args) ⇒ Object

Find elements



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/appom/element_finder.rb', line 33

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?
      if element.displayed? && element.text == text
        els.push(element)
      end
    elsif !visible.nil?
      if element.displayed?
        els.push(element)
      end
    elsif !text.nil?
      if element.text == text
        els.push(element)
      end
    else
      els.push(element)
    end
  end
  return 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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/appom/element_finder.rb', line 60

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

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

#_find(*find_args) ⇒ Object

Find an element



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/appom/element_finder.rb', line 4

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

  wait.until do
    elements = page.find_elements(*args)
    elements.each do |element|
      if !visible.nil? && !text.nil?
        if element.displayed? && element.text == text
          return element
        end
      elsif !visible.nil?
        if element.displayed?
          return element
        end
      elsif !text.nil?
        if element.text == text
          return element
        end
      # Just return first element
      else
        return element
      end
    end
    raise StandardError, "Can not found element with args = #{find_args}"
  end
end

#wait_until(type, *find_args) ⇒ Object

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/appom/element_finder.rb', line 106

def wait_until(type, *find_args)
  wait = 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)
      if result.enabled?
        raise StandardError, "Still found an element enable with args = #{find_args}"
      end
      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)
      if result.empty?
        raise StandardError, "Could not find any elements with args = #{find_args}"
      end
      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)
      if !result.empty?
        if result.size > 1
          raise StandardError, "Still found #{result.size} elements with args = #{find_args}"
        else
          raise StandardError, "Still found #{result.size} element with args = #{find_args}"
        end
      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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/appom/element_finder.rb', line 91

def wait_until_get_not_empty(*find_args)
  wait = 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
    if result.empty?
      raise StandardError, "Can not found any elements with args = #{find_args}"
    end
    # Return result
    return result
  end
end