appom 1.4.0 → 2.0.0

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.
@@ -1,174 +1,178 @@
1
- module Appom
2
- module ElementFinder
3
- # Find an element
4
- def _find(*find_args)
5
- args, text, visible = deduce_element_args(find_args)
6
- wait = Wait.new(timeout: Appom.max_wait_time)
7
-
8
- wait.until do
9
- elements = page.find_elements(*args)
10
- elements.each do |element|
11
- if !visible.nil? && !text.nil?
12
- if element.displayed? && element.text == text
13
- return element
14
- end
15
- elsif !visible.nil?
16
- if element.displayed?
17
- return element
18
- end
19
- elsif !text.nil?
20
- if element.text == text
21
- return element
22
- end
23
- # Just return first element
24
- else
25
- return element
26
- end
27
- end
28
- raise StandardError, "Can not found element with args = #{find_args}"
29
- end
30
- end
1
+ # frozen_string_literal: true
31
2
 
32
- # Find elements
33
- def _all(*find_args)
34
- args, text, visible = deduce_element_args(find_args)
35
- elements = page.find_elements(*args)
36
- els = []
3
+ # Element finding functionality for Appom automation framework
4
+ # Handles element location with various strategies and options
5
+ module Appom::ElementFinder
6
+ include Appom::Logging
7
+
8
+ def self.included(klass)
9
+ # Include cache-aware finder if caching is enabled
10
+
11
+ klass.include(ElementCache::CacheAwareFinder) if Appom.cache_config[:enabled]
12
+ rescue StandardError
13
+ # Continue without caching if it fails to load
14
+ end
15
+
16
+ # Find an element
17
+ def _find(*find_args)
18
+ args, text, visible = deduce_element_args(find_args)
19
+ wait = Appom::Wait.new(timeout: Appom.max_wait_time)
20
+
21
+ log_debug('Finding element', { args: args, text: text, visible: visible })
22
+ start_time = Time.now
37
23
 
24
+ wait.until do
25
+ elements = page.find_elements(*args)
38
26
  elements.each do |element|
39
27
  if !visible.nil? && !text.nil?
40
28
  if element.displayed? && element.text == text
41
- els.push(element)
29
+ duration = ((Time.now - start_time) * 1000).round(2)
30
+ log_element_action('FOUND', "element with #{args.join(', ')}", duration)
31
+ return element
42
32
  end
43
33
  elsif !visible.nil?
44
34
  if element.displayed?
45
- els.push(element)
35
+ duration = ((Time.now - start_time) * 1000).round(2)
36
+ log_element_action('FOUND', "element with #{args.join(', ')}", duration)
37
+ return element
46
38
  end
47
39
  elsif !text.nil?
48
40
  if element.text == text
49
- els.push(element)
41
+ duration = ((Time.now - start_time) * 1000).round(2)
42
+ log_element_action('FOUND', "element with #{args.join(', ')}", duration)
43
+ return element
50
44
  end
45
+ # Just return first element
51
46
  else
52
- els.push(element)
47
+ duration = ((Time.now - start_time) * 1000).round(2)
48
+ log_element_action('FOUND', "element with #{args.join(', ')}", duration)
49
+ return element
53
50
  end
54
51
  end
55
- return els
52
+ raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time)
56
53
  end
54
+ rescue Appom::WaitError
55
+ log_error('Element not found', { args: find_args, timeout: Appom.max_wait_time })
56
+ raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time)
57
+ end
57
58
 
58
- # Check page has or has not element with find_args
59
- # If page has element return TRUE else return FALSE
60
- def _check_has_element(*find_args)
61
- args, text, visible = deduce_element_args(find_args)
62
- elements = page.find_elements(*args)
59
+ # Find elements
60
+ def _all(*find_args)
61
+ args, text, visible = deduce_element_args(find_args)
62
+ elements = page.find_elements(*args)
63
+ els = []
63
64
 
64
- if visible.nil? && text.nil?
65
- return elements.empty? ? false : true
65
+ elements.each do |element|
66
+ if !visible.nil? && !text.nil?
67
+ els.push(element) if element.displayed? && element.text == text
68
+ elsif !visible.nil?
69
+ els.push(element) if element.displayed?
70
+ elsif !text.nil?
71
+ els.push(element) if element.text == text
66
72
  else
67
- is_found = false
68
- elements.each do |element|
69
- if !visible.nil? && !text.nil?
70
- if element.displayed? && element.text == text
71
- is_found = true
72
- end
73
- elsif !visible.nil?
74
- if element.displayed?
75
- is_found = true
76
- end
77
- elsif !text.nil?
78
- if element.text == text
79
- is_found = true
80
- end
81
- end
82
- end
83
- return is_found
73
+ els.push(element)
84
74
  end
85
75
  end
76
+ els
77
+ end
86
78
 
87
- ##
88
- # Use wait to get elements
89
- # Before timeout we will try to find elements until response return array is not empty
90
- #
91
- def wait_until_get_not_empty(*find_args)
92
- wait = Wait.new(timeout: Appom.max_wait_time)
93
- wait.until do
94
- result = page.find_elements(*find_args)
95
- # If response is empty we will return false to make it not pass Wait condition
96
- if result.empty?
97
- raise StandardError, "Can not found any elements with args = #{find_args}"
98
- end
99
- # Return result
100
- return result
79
+ # Check page has or has not element with find_args
80
+ # If page has element return TRUE else return FALSE
81
+ def _check_has_element(*find_args)
82
+ args, text, visible = deduce_element_args(find_args)
83
+ elements = page.find_elements(*args)
84
+
85
+ return !elements.empty? if visible.nil? && text.nil?
86
+
87
+ is_found = false
88
+ elements.each do |element|
89
+ if !visible.nil? && !text.nil?
90
+ is_found = true if element.displayed? && element.text == text
91
+ elsif !visible.nil?
92
+ is_found = true if element.displayed?
93
+ elsif !text.nil?
94
+ is_found = true if element.text == text
101
95
  end
102
96
  end
97
+ is_found
98
+ end
103
99
 
104
- # Function is used to check
105
- # Note: Function WILL NOT RETURN ELEMENT
106
- def wait_until(type, *find_args)
107
- wait = Wait.new(timeout: Appom.max_wait_time)
108
- wait.until do
109
- case type
110
- # Function only return true if element enabled or raise an error if time out
111
- when 'element enable'
112
- _find(*find_args).enabled?
113
- # Function only return true if element disabled or raise an error if time out
114
- when 'element disable'
115
- result = _find(*find_args)
116
- if result.enabled?
117
- raise StandardError, "Still found an element enable with args = #{find_args}"
118
- end
119
- return true
120
- # Function only return true if we can find at least one element (array is not empty) or raise error
121
- when 'at least one element exists'
122
- result = _all(*find_args)
123
- if result.empty?
124
- raise StandardError, "Could not find any elements with args = #{find_args}"
125
- end
126
- return true
127
-
128
- # Function only return true if we can't find at least one element (array is empty) or raise error
129
- when 'no element exists'
130
- result = _all(*find_args)
131
- if !result.empty?
132
- if result.size > 1
133
- raise StandardError, "Still found #{result.size} elements with args = #{find_args}"
134
- else
135
- raise StandardError, "Still found #{result.size} element with args = #{find_args}"
136
- end
137
- end
138
- return true
100
+ ##
101
+ # Use wait to get elements
102
+ # Before timeout we will try to find elements until response return array is not empty
103
+ #
104
+ def wait_until_get_not_empty(*find_args)
105
+ wait = Appom::Wait.new(timeout: Appom.max_wait_time)
106
+ wait.until do
107
+ result = page.find_elements(*find_args)
108
+ # If response is empty we will return false to make it not pass Wait condition
109
+ raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time) if result.empty?
110
+
111
+ # Return result
112
+ return result
113
+ end
114
+ end
115
+
116
+ # Function is used to check
117
+ # Note: Function WILL NOT RETURN ELEMENT
118
+ def wait_until(type, *find_args)
119
+ wait = Appom::Wait.new(timeout: Appom.max_wait_time)
120
+ wait.until do
121
+ case type
122
+ # Function only return true if element enabled or raise an error if time out
123
+ when 'element enable'
124
+ _find(*find_args).enabled?
125
+ # Function only return true if element disabled or raise an error if time out
126
+ when 'element disable'
127
+ result = _find(*find_args)
128
+ raise StandardError, "Still found an element enable with args = #{find_args}" if result.enabled?
129
+
130
+ return true
131
+ # Function only return true if we can find at least one element (array is not empty) or raise error
132
+ when 'at least one element exists'
133
+ result = _all(*find_args)
134
+ raise Appom::ElementNotFoundError.new(find_args.join(', '), Appom.max_wait_time) if result.empty?
135
+
136
+ return true
137
+
138
+ # Function only return true if we can't find at least one element (array is empty) or raise error
139
+ when 'no element exists'
140
+ result = _all(*find_args)
141
+ unless result.empty?
142
+ message = "Still found #{result.size} element#{'s' if result.size > 1}"
143
+ raise Appom::ElementError.new(message, { elements_found: result.size, selector: find_args.join(', ') })
139
144
  end
145
+ return true
140
146
  end
141
147
  end
148
+ end
142
149
 
143
- private
150
+ private
144
151
 
145
- def deduce_element_args(args)
146
- # Flatten argument array first if we are in case array inside array
147
- args = args.flatten
152
+ def deduce_element_args(args)
153
+ # Flatten argument array first if we are in case array inside array
154
+ args = args.flatten
148
155
 
149
- if args.empty?
150
- raise(ArgumentError, 'You should provide search arguments in element creation')
151
- end
156
+ raise Appom::InvalidElementError if args.empty?
152
157
 
153
- # Get last key and check if it contain 'text' key
154
- text = nil
155
- visible = nil
158
+ # Get last key and check if it contain 'text' key
159
+ text = nil
160
+ visible = nil
156
161
 
157
- args.each do |arg|
158
- if arg.is_a?(Hash)
159
- # Extract text value
160
- if arg.key?(:text)
161
- text = arg[:text]
162
- args.delete(arg)
163
- end
164
- # Extract visible value
165
- if arg.key?(:visible)
166
- visible = arg[:visible]
167
- args.delete(arg)
168
- end
169
- end
162
+ args.each do |arg|
163
+ next unless arg.is_a?(Hash)
164
+
165
+ # Extract text value
166
+ if arg.key?(:text)
167
+ text = arg[:text]
168
+ args.delete(arg)
169
+ end
170
+ # Extract visible value
171
+ if arg.key?(:visible)
172
+ visible = arg[:visible]
173
+ args.delete(arg)
170
174
  end
171
- [args, text, visible]
172
175
  end
176
+ [args, text, visible]
173
177
  end
174
- end
178
+ end