Module: Meglish
- Defined in:
- lib/meglish.rb
Constant Summary collapse
- MELGISH_CONDITIONS =
{ clear_text: true, scroll_to_element: true, include_all: true, timeout: 30, confirm_alert: true }
Instance Method Summary collapse
- #clear_text_element(_query, _options = {}) ⇒ Object
- #element_enabled?(_query, _options = {}) ⇒ Boolean
- #element_visible?(_query, _options = {}) ⇒ Boolean
- #find_coordinate_element(_query, _options = {}) ⇒ Object
- #find_element_on_screen(_query, _options = {}) ⇒ Object
- #find_elements(_query, _options = {}) ⇒ Object
- #get_device_size ⇒ Object
- #get_element(_query, _options = {}) ⇒ Object
- #get_element_by_index(_query, _index, _options = {}) ⇒ Object
- #get_elements(_query, _options = {}) ⇒ Object
- #keyboard_enter_text_element(_text, _options = {}) ⇒ Object
- #long_press_and_touch_element(_query, _touch_element_query, _options = {}) ⇒ Object
- #long_press_element(_query, _options = {}) ⇒ Object
- #long_press_release_element(_query, _options = {}) ⇒ Object
- #scroll_to_element(_query, _to_top_first = false, _options = {}) ⇒ Object
- #scroll_to_top ⇒ Object
- #select_spinner_item_element(_spinner_query, _text, _options = {}) ⇒ Object
- #set_date_element(_query_input, _date, _options = {}) ⇒ Object
- #swipe_left(_query) ⇒ Object
- #swipe_right(_query) ⇒ Object
- #text_element(_query, _options = {}) ⇒ Object
- #text_spinner_element(_query, _options = {}) ⇒ Object
- #touch_and_keyboard_text_element(_query, _text, _options = {}) ⇒ Object
- #touch_element(_query, _options = {}) ⇒ Object
- #touch_element_by_text_position(_query, _text, _options = {}) ⇒ Object
- #wait_for_or_ignore(_query, _timeout = 3, _options = {}) ⇒ Object
- #wait_for_text_element(_query, _timeout = 10, _options = {}) ⇒ Object
- #wait_keyboard_visible?(_timeout = 3) ⇒ Boolean
Instance Method Details
#clear_text_element(_query, _options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/meglish.rb', line 10 def clear_text_element(_query, = {}) touch_element(_query.strip, ) clear_text end |
#element_enabled?(_query, _options = {}) ⇒ Boolean
15 16 17 |
# File 'lib/meglish.rb', line 15 def element_enabled?(_query, = {}) get_element['enabled'] end |
#element_visible?(_query, _options = {}) ⇒ Boolean
19 20 21 |
# File 'lib/meglish.rb', line 19 def element_visible?(_query, = {}) get_element['visible'] end |
#find_coordinate_element(_query, _options = {}) ⇒ Object
23 24 25 26 |
# File 'lib/meglish.rb', line 23 def find_coordinate_element(_query, = {}) find_element_on_screen(_query, ) find_coordinate(_query, ) end |
#find_element_on_screen(_query, _options = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/meglish.rb', line 28 def find_element_on_screen(_query, = {}) _query = include_all(_query, ) MeglishLog.new.log(_query) wait_for_element_exists(_query, get_option(:timeout, )) sleep 0.5 scroll_to_element(_query, ) end |
#find_elements(_query, _options = {}) ⇒ Object
36 37 38 |
# File 'lib/meglish.rb', line 36 def find_elements(_query, = {}) query(include_all(_query, )) end |
#get_device_size ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/meglish.rb', line 40 def get_device_size size = `adb shell dumpsys input | grep 'deviceSize' | awk '{ print $10 , $11 }'` size = size[0..size.length - 3] w, h = size.split(', ') w = w.to_i h = h.to_i [w, h] end |
#get_element(_query, _options = {}) ⇒ Object
49 50 51 52 |
# File 'lib/meglish.rb', line 49 def get_element(_query, = {}) find_element_on_screen(_query, ) query(include_all(_query, )).first end |
#get_element_by_index(_query, _index, _options = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/meglish.rb', line 54 def get_element_by_index(_query, _index, = {}) new_query = _query + ' index:' + _index find_element_on_screen(_query, ) query(new_query).first end |
#get_elements(_query, _options = {}) ⇒ Object
60 61 62 63 64 |
# File 'lib/meglish.rb', line 60 def get_elements(_query, = {}) query(_query) rescue return [] end |
#keyboard_enter_text_element(_text, _options = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/meglish.rb', line 66 def keyboard_enter_text_element(_text, = {}) wait_for_keyboard keyboard_enter_text _text hide_soft_keyboard end |
#long_press_and_touch_element(_query, _touch_element_query, _options = {}) ⇒ Object
88 89 90 91 |
# File 'lib/meglish.rb', line 88 def long_press_and_touch_element(_query, _touch_element_query, = {}) long_press_element(_query, ) touch_element(_touch_element_query, ) end |
#long_press_element(_query, _options = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/meglish.rb', line 72 def long_press_element(_query, = {}) start_monkey find_element_on_screen(_query, ) x, y = find_coordinate(_query, ) monkey_touch(:down, x, y) kill_existing_monkey_processes end |
#long_press_release_element(_query, _options = {}) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/meglish.rb', line 80 def long_press_release_element(_query, = {}) start_monkey find_element_on_screen(_query, ) x, y = find_coordinate(_query, ) monkey_touch(:up, x, y) kill_existing_monkey_processes end |
#scroll_to_element(_query, _to_top_first = false, _options = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/meglish.rb', line 104 def scroll_to_element(_query, _to_top_first = false, = {}) return unless get_option(:scroll_to_element, ) hide_soft_keyboard unless (query(_query).first['rect']['height']).zero? && (query(_query).first['rect']['width']).zero? w, h = get_device_size x, y = find_coordinate(include_all(_query, )) scroll_until_find_element(_query, h, y, w, x) swipe_until_find_element(_query, w, x, h, y) end rescue MeglishLog.new.log('Scroll to element was ignored') query(_query) end |
#scroll_to_top ⇒ Object
119 120 121 |
# File 'lib/meglish.rb', line 119 def scroll_to_top perform_action('drag', 50, 50, 50, 1500, 1) end |
#select_spinner_item_element(_spinner_query, _text, _options = {}) ⇒ Object
123 124 125 126 |
# File 'lib/meglish.rb', line 123 def select_spinner_item_element(_spinner_query, _text, = {}) touch_element(_spinner_query, ) touch_element("DropDownListView child CustomFontTextView text:'#{_text}'", ) end |
#set_date_element(_query_input, _date, _options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/meglish.rb', line 93 def set_date_element(_query_input, _date, = {}) new_date = _date.match(/(^[\d]{4})\/([\d]{1,2})\/([\d]{1,2})/) year = new_date[1].to_i month = (new_date[2].to_i) - 1 day = new_date[3].to_i touch_element(_query_input, ) query("DatePicker" ,{method_name: :updateDate, arguments: [year, month, day]}) touch("MDButton id:'md_buttonDefaultPositive'") unless get_option(:confirm_alert, ) end |
#swipe_left(_query) ⇒ Object
128 129 130 131 132 |
# File 'lib/meglish.rb', line 128 def swipe_left(_query) has_element = query(_query + ' parent HorizontalScrollView') _query = has_element.empty? ? _query : _query + ' parent HorizontalScrollView' pan_left(query_string: _query) end |
#swipe_right(_query) ⇒ Object
134 135 136 137 138 |
# File 'lib/meglish.rb', line 134 def swipe_right(_query) has_element = query(_query + ' parent HorizontalScrollView') _query = has_element.empty? ? _query : _query + ' parent HorizontalScrollView' pan_right(query_string: _query) end |
#text_element(_query, _options = {}) ⇒ Object
140 141 142 |
# File 'lib/meglish.rb', line 140 def text_element(_query, = {}) get_element(_query, )['text'] end |
#text_spinner_element(_query, _options = {}) ⇒ Object
144 145 146 147 |
# File 'lib/meglish.rb', line 144 def text_spinner_element(_query, = {}) _query += ' child CustomFontTextView' get_element(_query, )['text'] end |
#touch_and_keyboard_text_element(_query, _text, _options = {}) ⇒ Object
162 163 164 165 166 |
# File 'lib/meglish.rb', line 162 def touch_and_keyboard_text_element(_query, _text, = {}) touch_element(_query.strip, ) clear_text if get_element(:clear_text, ) keyboard_enter_text_element(_text, ) end |
#touch_element(_query, _options = {}) ⇒ Object
149 150 151 |
# File 'lib/meglish.rb', line 149 def touch_element(_query, = {}) touch_element(_query.strip, ) end |
#touch_element_by_text_position(_query, _text, _options = {}) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/meglish.rb', line 153 def touch_element_by_text_position(_query, _text, = {}) text_el = get_element(_query, )['text'] text_index = text_el.index(_text) + (_text.length / 2) line_for_offset = query(_query, :getLayout, getLineForOffset: text_index)[0] vertical = query(_query, :getLayout, getLineBaseline: line_for_offset)[0].to_i horizontal = query(_query, :getLayout, getPrimaryHorizontal: text_index)[0].to_i touch(_query, x: 0, y: 0, offset: { x: horizontal, y: vertical }) end |
#wait_for_or_ignore(_query, _timeout = 3, _options = {}) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/meglish.rb', line 168 def wait_for_or_ignore(_query, _timeout = 3, = {}) _timeout *= 2 found = false while _timeout > 0 && found == false if find_elements(_query, ).empty? _timeout -= 1 sleep 0.5 else found = true end end found end |
#wait_for_text_element(_query, _timeout = 10, _options = {}) ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/meglish.rb', line 182 def wait_for_text_element(_query, _timeout = 10, = {}) count = 0 filled = '' while (filled.nil? || filled.empty?) return if count >= _timeout filled = text_element(_query, ) sleep 1 count += 1 end end |
#wait_keyboard_visible?(_timeout = 3) ⇒ Boolean
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/meglish.rb', line 193 def wait_keyboard_visible?(_timeout = 3) visible = 'false' _timeout *= 2 while _timeout > 0 && visible == 'false' field, visible = `adb shell dumpsys input_method | grep mInputShown | awk '{ print $4 }'`.split('=') if visible == 'false' _timeout -= 1 sleep 0.5 end end visible == 'true' ? true : false end |