Class: TestCentricity::AppUIElement

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/testcentricity/app_elements/app_element_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, locator, context) ⇒ AppUIElement

Returns a new instance of AppUIElement.



9
10
11
12
13
14
15
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 9

def initialize(name, parent, locator, context)
  @name       = name
  @parent     = parent
  @locator    = locator
  @context    = context
  @type       = nil
end

Instance Attribute Details

#context ⇒ Object (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 7

def context
  @context
end

#locator ⇒ Object (readonly)

Returns the value of attribute locator.



7
8
9
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 7

def locator
  @locator
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 7

def name
  @name
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 7

def parent
  @parent
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 7

def type
  @type
end

Instance Method Details

#clear ⇒ Object



74
75
76
77
78
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 74

def clear
  obj = element
  object_not_found_exception(obj)
  obj.clear
end

#click ⇒ Object



29
30
31
32
33
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 29

def click
  obj = element
  object_not_found_exception(obj)
  obj.click
end

#disabled? ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 143

def disabled?
  !enabled?
end

#double_tap ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 44

def double_tap
  obj = element
  object_not_found_exception(obj)
  x = obj.location.x
  y = obj.location.y
  tap_action = Appium::TouchAction.new.double_tap(element: obj, x: x, y: y)
  tap_action.perform
end

#enabled? ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 137

def enabled?
  obj = element
  object_not_found_exception(obj)
  obj.enabled?
end

#exists? ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 117

def exists?
  begin
    element.displayed?
  rescue
    false
  end
end

#get_attribute(attrib) ⇒ Object



159
160
161
162
163
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 159

def get_attribute(attrib)
  obj = element
  object_not_found_exception(obj)
  obj.attribute(attrib)
end

#get_caption ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 95

def get_caption
  obj = element
  object_not_found_exception(obj)
  if AppiumConnect.is_webview?
    case obj.tag_name.downcase
    when 'input', 'select', 'textarea'
      obj.value
    else
      obj.text
    end
  else
    case obj.tag_name
    when 'XCUIElementTypeNavigationBar'
      obj.attribute('name')
    when 'android.widget.TextView', 'android.widget.Button'
      obj.text
    else
      obj.attribute('label')
    end
  end
end

#get_locator ⇒ Object



21
22
23
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 21

def get_locator
  @locator
end

#get_name ⇒ Object



25
26
27
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 25

def get_name
  @name
end

#get_object_type ⇒ Object



17
18
19
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 17

def get_object_type
  @type
end

#get_value ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 80

def get_value
  obj = element
  object_not_found_exception(obj)
  if AppiumConnect.is_webview?
    case obj.tag_name.downcase
    when 'input', 'select', 'textarea'
      obj.value
    else
      obj.text
    end
  else
    obj.value
  end
end

#height ⇒ Integer

Return height of object.

Examples:

button_height = my_button.height

Returns:

  • (Integer)


271
272
273
274
275
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 271

def height
  obj = element
  object_not_found_exception(obj)
  obj.size.height
end

#hidden? ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 133

def hidden?
  !visible?
end

#scroll(direction) ⇒ Object



301
302
303
304
305
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 301

def scroll(direction)
  obj = element
  object_not_found_exception(obj)
  execute_script('mobile: scroll', direction: direction.to_s, element: obj)
end

#selected? ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 147

def selected?
  obj = element
  object_not_found_exception(obj)
  obj.selected?
end

#send_keys(value) ⇒ Object



68
69
70
71
72
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 68

def send_keys(value)
  obj = element
  object_not_found_exception(obj)
  obj.send_keys(value)
end

#set(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 53

def set(value)
  obj = element
  object_not_found_exception(obj)
  if value.is_a?(Array)
    obj.send_keys(value[0])
    if value[1].is_a?(Integer)
      press_keycode(value[1])
    else
      obj.send_keys(value[1])
    end
  elsif value.is_a?(String)
    obj.send_keys(value)
  end
end

#swipe(direction) ⇒ Object



307
308
309
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 307

def swipe(direction)
  execute_script('mobile: swipe', direction: direction.to_s)
end

#tag_name ⇒ Object



153
154
155
156
157
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 153

def tag_name
  obj = element
  object_not_found_exception(obj)
  obj.tag_name
end

#tap ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 35

def tap
  obj = element
  object_not_found_exception(obj)
  x = obj.location.x
  y = obj.location.y
  tap_action = Appium::TouchAction.new.tap(element: obj, x: x, y: y)
  tap_action.perform
end

#visible? ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 125

def visible?
  if exists?
    element.displayed?
  else
    false
  end
end

#wait_until_exists(seconds = nil) ⇒ Object

Wait until the object exists, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

run_button.wait_until_exists(0.5)

Parameters:

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



172
173
174
175
176
177
178
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 172

def wait_until_exists(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { exists? }
rescue
  raise "Could not find UI #{object_ref_message} after #{timeout} seconds" unless exists?
end

#wait_until_gone(seconds = nil) ⇒ Object

Wait until the object no longer exists, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

logout_button.wait_until_gone(5)

Parameters:

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



187
188
189
190
191
192
193
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 187

def wait_until_gone(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { !exists? }
rescue
  raise "UI #{object_ref_message} remained visible after #{timeout} seconds" if exists?
end

#wait_until_hidden(seconds = nil) ⇒ Object

Wait until the object is hidden, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

run_button.wait_until_hidden(10)

Parameters:

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



217
218
219
220
221
222
223
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 217

def wait_until_hidden(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { hidden? }
rescue
  raise "UI #{object_ref_message} remained visible after #{timeout} seconds" if visible?
end

#wait_until_value_changes(seconds = nil) ⇒ Object

Wait until the object's value changes to a different value, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

basket_grand_total_label.wait_until_value_changes(5)

Parameters:

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



250
251
252
253
254
255
256
257
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 250

def wait_until_value_changes(seconds = nil)
  value = get_value
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { get_value != value }
rescue
  raise "Value of UI #{object_ref_message} failed to change from '#{value}' after #{timeout} seconds" if get_value == value
end

#wait_until_value_is(value, seconds = nil) ⇒ Object

Wait until the object's value equals the specified value, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

card_authorized_label.wait_until_value_is('Card authorized', 5)
  or
total_weight_field.wait_until_value_is({ :greater_than => '250' }, 5)

Parameters:

  • value (String or Hash) —

    value expected or comparison hash

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



235
236
237
238
239
240
241
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 235

def wait_until_value_is(value, seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { compare(value, get_value) }
rescue
  raise "Value of UI #{object_ref_message} failed to equal '#{value}' after #{timeout} seconds" unless get_value == value
end

#wait_until_visible(seconds = nil) ⇒ Object

Wait until the object is visible, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Environ.default_max_wait_time.

Examples:

run_button.wait_until_visible(0.5)

Parameters:

  • seconds (Integer or Float) (defaults to: nil) —

    wait time in seconds



202
203
204
205
206
207
208
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 202

def wait_until_visible(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { visible? }
rescue
  raise "Could not find UI #{object_ref_message} after #{timeout} seconds" unless visible?
end

#width ⇒ Object



259
260
261
262
263
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 259

def width
  obj = element
  object_not_found_exception(obj)
  obj.size.width
end

#x_loc ⇒ Integer

Return x coordinate of object's location.

Examples:

button_x = my_button.x

Returns:

  • (Integer)


283
284
285
286
287
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 283

def x_loc
  obj = element
  object_not_found_exception(obj)
  obj.location.x
end

#y_loc ⇒ Integer

Return y coordinate of object's location.

Examples:

button_y = my_button.y

Returns:

  • (Integer)


295
296
297
298
299
# File 'lib/testcentricity/app_elements/app_element_helper.rb', line 295

def y_loc
  obj = element
  object_not_found_exception(obj)
  obj.location.y
end