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
# File 'lib/appom/element_finder.rb', line 60

def _check_has_element(*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 Appom::ElementsEmptyError, "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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/appom/element_finder.rb', line 105

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'
      !_find(*find_args).enabled?
    # Function only return true if we can find at leat one element (array is not empty) or raise error
    when 'at least one element exists'
      !_all(*find_args).empty?
    # Function only return true if we can't find at leat one element (array is empty) or raise error
    when 'no element exists'
      _all(*find_args).empty?
    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



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

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 reponse is empty we will return false to make it not pass Wait condition
    if result.empty?
      raise Appom::ElementsEmptyError, "Can not found any elements with args = #{find_args}"
    end
    # Return result
    return result
  end
end