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
}.freeze

Instance Method Summary collapse

Instance Method Details

#build_index(_index) ⇒ Object



12
13
14
# File 'lib/meglish.rb', line 12

def build_index(_index)
    _index.to_s.empty? ? ' ' : " index:#{_index} "
end

#clear_text_element(_query, _options = {}) ⇒ Object



16
17
18
19
# File 'lib/meglish.rb', line 16

def clear_text_element(_query, _options = {})
    touch_element(_query.strip, _options)
    clear_text
end

#element_checked?(_query, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/meglish.rb', line 21

def element_checked?(_query, _options = {})
    get_element(_query, _options)['checked']
end

#element_enabled?(_query, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/meglish.rb', line 25

def element_enabled?(_query, _options = {})
    get_element(_query, _options)['enabled']
end

#element_visible?(_query, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/meglish.rb', line 29

def element_visible?(_query, _options = {})
    get_element(_query, _options)['visible']
end

#find_coordinate_element(_query, _options = {}) ⇒ Object



33
34
35
36
# File 'lib/meglish.rb', line 33

def find_coordinate_element(_query, _options = {})
    find_element_on_screen(_query, _options)
    find_coordinate(_query, _options)
end

#find_element_on_screen(_query, _options = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/meglish.rb', line 38

def find_element_on_screen(_query, _options = {})
    hide_soft_keyboard
    _query = include_all(_query, _options)
    MeglishLog.new.log(_query)
    wait_for_element_exists(_query, timeout: get_option(:timeout, _options))
    scroll_to_element(_query, _options)
end

#find_elements(_query, _options = {}) ⇒ Object



46
47
48
# File 'lib/meglish.rb', line 46

def find_elements(_query, _options = {})
    query(include_all(_query, _options))
end

#get_device_sizeObject



50
51
52
53
54
55
56
57
# File 'lib/meglish.rb', line 50

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



59
60
61
62
# File 'lib/meglish.rb', line 59

def get_element(_query, _options = {})
    find_element_on_screen(_query, _options)
    query(include_all(_query, _options)).first
end

#get_element_by_index(_query, _index, _options = {}) ⇒ Object



64
65
66
67
68
# File 'lib/meglish.rb', line 64

def get_element_by_index(_query, _index, _options = {})
    new_query = _query + ' index:' + _index
    find_element_on_screen(_query, _options)
    query(new_query).first
end

#get_elements(_query, _options = {}) ⇒ Object



70
71
72
73
74
# File 'lib/meglish.rb', line 70

def get_elements(_query, _options = {})
    query(_query)
rescue
    return []
end

#keyboard_enter_text_element(_text, _options = {}) ⇒ Object



76
77
78
79
80
# File 'lib/meglish.rb', line 76

def keyboard_enter_text_element(_text, _options = {})
    wait_for_keyboard
    keyboard_enter_text _text
    hide_soft_keyboard
end

#long_press_and_touch_element(_query, _touch_element_query, _options = {}) ⇒ Object



98
99
100
101
# File 'lib/meglish.rb', line 98

def long_press_and_touch_element(_query, _touch_element_query, _options = {})
    long_press_element(_query, _options)
    touch_element(_touch_element_query, _options)
end

#long_press_element(_query, _options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/meglish.rb', line 82

def long_press_element(_query, _options = {})
    start_monkey
    find_element_on_screen(_query, _options)
    x, y = find_coordinate(_query, _options)
    monkey_touch(:down, x, y)
    kill_existing_monkey_processes
end

#long_press_release_element(_query, _options = {}) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/meglish.rb', line 90

def long_press_release_element(_query, _options = {})
    start_monkey
    find_element_on_screen(_query, _options)
    x, y = find_coordinate(_query, _options)
    monkey_touch(:up, x, y)
    kill_existing_monkey_processes
end

#scroll_to_element(_query, _options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/meglish.rb', line 115

def scroll_to_element(_query, _options = {})
    return unless get_option(:scroll_to_element, _options)
    sleep 0.5
    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, _options))

        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_topObject



131
132
133
# File 'lib/meglish.rb', line 131

def scroll_to_top
    perform_action('drag', 50, 50, 50, 1500, 1)
end

#select_spinner_item_element(_spinner_query, _text, _options = {}) ⇒ Object



135
136
137
138
# File 'lib/meglish.rb', line 135

def select_spinner_item_element(_spinner_query, _text, _options = {})
    touch_element(_spinner_query, _options)
    touch_element("DropDownListView child * text:'#{_text}'", _options)
end

#set_date_element(_query_input, _date, _dialog_confim_button_query = "", _options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/meglish.rb', line 103

def set_date_element(_query_input, _date, _dialog_confim_button_query = "", _options = {})
    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, _options)
    query('DatePicker', method_name: :updateDate, arguments: [year, month, day])
    _dialog_confim_button_query = "MDButton id:'md_buttonDefaultPositive'" if _dialog_confim_button_query.empty?
    touch(_dialog_confim_button_query) if get_option(:confirm_alert, _options)
end

#swipe_down(_scroll_amount) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/meglish.rb', line 140

def swipe_down(_scroll_amount)
    if _scroll_amount < 4
      _scroll_amount = 5
    elsif _scroll_amount >= 50
      _scroll_amount = 49
    end
    down = 50 + _scroll_amount
    perform_action('drag', 50, 50, 50, down, 5)
end

#swipe_left(_query) ⇒ Object



150
151
152
153
154
# File 'lib/meglish.rb', line 150

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



156
157
158
159
160
# File 'lib/meglish.rb', line 156

def swipe_right(_query)
    has_element = query(_query + ' parent HorizontalScrollView')
    _query = has_element.empty? ? _query : _query + ' parent HorizontalScrollView'
    pan_right(query_string: _query)
end

#swipe_up(_scroll_amount) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/meglish.rb', line 162

def swipe_up(_scroll_amount)
    if _scroll_amount < 4
      _scroll_amount = 5
    elsif _scroll_amount >= 50
      _scroll_amount = 49
    end
    up = 50 + _scroll_amount
    perform_action('drag', 50, 50, up, 50, 5)
end

#text_element(_query, _options = {}) ⇒ Object



172
173
174
# File 'lib/meglish.rb', line 172

def text_element(_query, _options = {})
    get_element(_query, _options)['text']
end

#text_spinner_element(_query, _options = {}) ⇒ Object



176
177
178
179
# File 'lib/meglish.rb', line 176

def text_spinner_element(_query, _options = {})
    _query += ' child CustomFontTextView'
    get_element(_query, _options)['text']
end

#touch_and_keyboard_text_element(_query, _text, _options = {}) ⇒ Object



200
201
202
203
204
# File 'lib/meglish.rb', line 200

def touch_and_keyboard_text_element(_query, _text, _options = {})
    touch_element(_query.strip, _options)
    clear_text if get_option(:clear_text, _options)
    keyboard_enter_text_element(_text, _options)
end

#touch_element(_query, _options = {}) ⇒ Object



181
182
183
184
# File 'lib/meglish.rb', line 181

def touch_element(_query, _options = {})
    find_element_on_screen(_query, _options)
    touch(_query.strip)
end

#touch_element_by_text_position(_query, _text, _options = {}) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/meglish.rb', line 191

def touch_element_by_text_position(_query, _text, _options = {})
    text_el = get_element(_query, _options)['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

#touch_element_with_all(_query, _options = {}) ⇒ Object



186
187
188
189
# File 'lib/meglish.rb', line 186

def touch_element_with_all(_query, _options = {})
    find_element_on_screen(_query, _options)
    touch(include_all(_query, options).strip)
end

#wait_for_or_ignore(_query, _timeout = 3, _options = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/meglish.rb', line 206

def wait_for_or_ignore(_query, _timeout = 3, _options = {})
    _timeout *= 2
    found = false
    while _timeout > 0 && found == false
        if find_elements(_query, _options).empty?
            _timeout -= 1
            sleep 0.5
        else
            found = true
        end
    end
    found
end

#wait_for_text_element(_query, _timeout = 10, _options = {}) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/meglish.rb', line 220

def wait_for_text_element(_query, _timeout = 10, _options = {})
    count = 0
    filled = ''
    while filled.nil? || filled.empty?
        return if count >= _timeout
        filled = text_element(_query, _options)
        sleep 1
        count += 1
    end
end

#wait_keyboard_visible?(_timeout = 3) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/meglish.rb', line 231

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