Class: TestCentricity::AppElements::AppUIElement
- Includes:
- Test::Unit::Assertions
- Defined in:
- lib/testcentricity_mobile/app_elements/app_element.rb
Direct Known Subclasses
AppAlert, AppButton, AppCheckBox, AppImage, AppLabel, AppList, AppRadio, AppSwitch, AppTextField
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#locator ⇒ Object
readonly
Returns the value of attribute locator.
-
#mru_app_session ⇒ Object
Returns the value of attribute mru_app_session.
-
#mru_locator ⇒ Object
Returns the value of attribute mru_locator.
-
#mru_object ⇒ Object
Returns the value of attribute mru_object.
-
#mru_parent ⇒ Object
Returns the value of attribute mru_parent.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #clear ⇒ Object
-
#click ⇒ Object
Click on a UI element.
-
#count ⇒ Integer
Return the number of occurrences of an object with an ambiguous locator that evaluates to multiple UI elements.
-
#disabled? ⇒ Boolean
Is UI object disabled (not enabled)?.
-
#double_tap ⇒ Object
Double-tap on a UI element.
-
#drag_and_drop(target, duration = 0.3) ⇒ Object
Drag the UI object to the specified target object.
-
#drag_by(right_offset, down_offset, duration = 0.3) ⇒ Object
Drag the UI object by the specified offset.
- #element ⇒ Object
-
#enabled? ⇒ Boolean
Is UI object enabled?.
-
#exists? ⇒ Boolean
Does UI object exists?.
- #get_attribute(attrib) ⇒ Object
- #get_caption ⇒ Object (also: #caption)
- #get_locator ⇒ Object
- #get_name ⇒ Object
- #get_object_type ⇒ Object
- #get_value ⇒ Object (also: #value)
-
#height ⇒ Integer
Return height of object.
-
#hidden? ⇒ Boolean
Is UI object hidden (not visible)?.
-
#initialize(name, parent, locator, context) ⇒ AppUIElement
constructor
A new instance of AppUIElement.
-
#long_press(duration = 1) ⇒ Object
Long press on a UI element.
- #reset_mru_cache ⇒ Object
-
#scroll_into_view(scroll_mode = :vertical) ⇒ Object
Scroll the UI object until it is visible.
- #selected? ⇒ Boolean
-
#send_keys(value) ⇒ Object
Send keystrokes to this UI element.
- #set(value) ⇒ Object
-
#swipe_gesture(direction, distance = 0.5) ⇒ Object
Perform a swipe gesture on the UI object in the specified direction.
-
#tap ⇒ Object
Tap on a UI element.
-
#visible? ⇒ Boolean
Is UI object visible?.
-
#wait_until_enabled(seconds = nil, post_exception = true) ⇒ Object
Wait until the object is enabled, or until the specified wait time has expired.
-
#wait_until_exists(seconds = nil, post_exception = true) ⇒ Object
Wait until the object exists, or until the specified wait time has expired.
-
#wait_until_gone(seconds = nil, post_exception = true) ⇒ Object
Wait until the object no longer exists, or until the specified wait time has expired.
-
#wait_until_hidden(seconds = nil, post_exception = true) ⇒ Object
Wait until the object is hidden, or until the specified wait time has expired.
-
#wait_until_value_changes(seconds = nil, post_exception = true) ⇒ Object
Wait until the object's value changes to a different value, or until the specified wait time has expired.
-
#wait_until_value_is(value, seconds = nil, post_exception = true) ⇒ Object
Wait until the object's value equals the specified value, or until the specified wait time has expired.
-
#wait_until_visible(seconds = nil, post_exception = true) ⇒ Object
Wait until the object is visible, or until the specified wait time has expired.
-
#width ⇒ Integer
Return width of object.
-
#x_loc ⇒ Integer
Return x coordinate of object's location.
-
#y_loc ⇒ Integer
Return y coordinate of object's location.
Constructor Details
#initialize(name, parent, locator, context) ⇒ AppUIElement
Returns a new instance of AppUIElement.
11 12 13 14 15 16 17 18 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 11 def initialize(name, parent, locator, context) @name = name @parent = parent @locator = locator @context = context @type = nil reset_mru_cache end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 8 def context @context end |
#locator ⇒ Object (readonly)
Returns the value of attribute locator.
8 9 10 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 8 def locator @locator end |
#mru_app_session ⇒ Object
Returns the value of attribute mru_app_session.
9 10 11 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 9 def mru_app_session @mru_app_session end |
#mru_locator ⇒ Object
Returns the value of attribute mru_locator.
9 10 11 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 9 def mru_locator @mru_locator end |
#mru_object ⇒ Object
Returns the value of attribute mru_object.
9 10 11 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 9 def mru_object @mru_object end |
#mru_parent ⇒ Object
Returns the value of attribute mru_parent.
9 10 11 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 9 def mru_parent @mru_parent end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 8 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 8 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 8 def type @type end |
Instance Method Details
#clear ⇒ Object
66 67 68 69 70 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 66 def clear obj = element object_not_found_exception(obj) obj.clear end |
#click ⇒ Object
Click on a UI element
425 426 427 428 429 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 425 def click obj = element object_not_found_exception(obj) obj.click end |
#count ⇒ Integer
Return the number of occurrences of an object with an ambiguous locator that evaluates to multiple UI elements.
415 416 417 418 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 415 def count objs = find_elements(@locator.keys[0], @locator.values[0]) objs.count end |
#disabled? ⇒ Boolean
Is UI object disabled (not enabled)?
187 188 189 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 187 def disabled? !enabled? end |
#double_tap ⇒ Object
Double-tap on a UI element
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 450 def double_tap obj = element object_not_found_exception(obj) driver.action .click_and_hold(obj) .release .pause(duration: 0.2) .click_and_hold(obj) .release .perform end |
#drag_and_drop(target, duration = 0.3) ⇒ Object
Drag the UI object to the specified target object. If the optional duration parameter is not specified, the duration defaults to 0.3 seconds (300 milliseconds).
514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 514 def drag_and_drop(target, duration = 0.3) drag = element object_not_found_exception(drag) drop = target.element drag_x = drag.location.x drag_y = drag.location.y drop_x = drop.location.x drop_y = drop.location.y driver.action .click_and_hold(drag) .move_by(drop_x - drag_x, drop_y - drag_y, duration: duration) .release .perform end |
#drag_by(right_offset, down_offset, duration = 0.3) ⇒ Object
Drag the UI object by the specified offset. If the optional duration parameter is not specified, the duration defaults to 0.3 seconds (300 milliseconds).
496 497 498 499 500 501 502 503 504 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 496 def drag_by(right_offset, down_offset, duration = 0.3) obj = element object_not_found_exception(obj) driver.action .click_and_hold(obj) .move_by(right_offset, down_offset, duration: duration) .release .perform end |
#element ⇒ Object
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 617 def element reset_mru_cache if @mru_app_session != Environ.app_session_id obj = if @context == :section parent_obj = nil parent_locator = @parent.get_locator if @mru_locator == @locator && @mru_parent == parent_locator && !@mru_object.nil? return @mru_object end parent_locator.each do |locators| if locators.keys[0] == :object parent_obj = locators.values[0] break end parent_obj = if parent_obj.nil? find_element(locators.keys[0], locators.values[0]) else parent_obj.find_element(locators.keys[0], locators.values[0]) end end puts "Found parent object '#{@parent.get_name}' - #{@parent.get_locator}" if ENV['DEBUG'] parent_obj.find_element(@locator.keys[0], @locator.values[0]) else return @mru_object if @mru_locator == @locator && !@mru_object.nil? find_element(@locator.keys[0], @locator.values[0]) end puts "Found object '#{@name}' - #{@locator}" if ENV['DEBUG'] @mru_object = obj @mru_locator = @locator @mru_parent = parent_locator @mru_app_session = Environ.app_session_id obj rescue puts "Did not find object '#{@name}' - #{@locator}" if ENV['DEBUG'] nil end |
#enabled? ⇒ Boolean
Is UI object enabled?
175 176 177 178 179 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 175 def enabled? obj = element object_not_found_exception(obj) obj.enabled? end |
#exists? ⇒ Boolean
Does UI object exists?
135 136 137 138 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 135 def exists? obj = element !obj.nil? end |
#get_attribute(attrib) ⇒ Object
197 198 199 200 201 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 197 def get_attribute(attrib) obj = element object_not_found_exception(obj) obj.attribute(attrib) end |
#get_caption ⇒ Object Also known as: caption
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 89 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 elsif Environ.is_ios? caption = case obj.tag_name when 'XCUIElementTypeNavigationBar' obj.attribute(:name) else obj.attribute(:label) end caption = '' if caption.nil? else caption = obj.text if caption.blank? case obj.attribute(:class) when 'android.view.ViewGroup' caption_obj = obj.find_element(:xpath, '//android.widget.TextView') caption = caption_obj.text when 'android.widget.Button' caption_obj = obj.find_element(:xpath, '//android.widget.TextView') caption = caption_obj.text if caption.blank? caption_obj = obj.find_element(:xpath, '//android.widget.ViewGroup/android.widget.TextView') caption = caption_obj.text end end end end caption end |
#get_locator ⇒ Object
31 32 33 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 31 def get_locator @locator end |
#get_name ⇒ Object
35 36 37 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 35 def get_name @name end |
#get_object_type ⇒ Object
27 28 29 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 27 def get_object_type @type end |
#get_value ⇒ Object Also known as: value
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 72 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.text end end |
#height ⇒ Integer
Return height of object.
379 380 381 382 383 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 379 def height obj = element object_not_found_exception(obj) obj.size.height end |
#hidden? ⇒ Boolean
Is UI object hidden (not visible)?
165 166 167 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 165 def hidden? !visible? end |
#long_press(duration = 1) ⇒ Object
Long press on a UI element
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 468 def long_press(duration = 1) obj = element object_not_found_exception(obj) if Environ.is_ios? begin Environ.appium_driver.execute_script('mobile: touchAndHold', { elementId: obj.id, duration: duration }) rescue => err puts "Retrying longpress due to error: #{err}" else return end end driver.action .click_and_hold(obj) .pause(duration: duration) .release .perform end |
#reset_mru_cache ⇒ Object
20 21 22 23 24 25 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 20 def reset_mru_cache @mru_object = nil @mru_locator = nil @mru_parent = nil @mru_app_session = nil end |
#scroll_into_view(scroll_mode = :vertical) ⇒ Object
Scroll the UI object until it is visible. If scroll_mode is not specified, then vertical scrolling will be used.
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 535 def scroll_into_view(scroll_mode = :vertical) return if visible? case scroll_mode when :vertical start_direction = :down end_direction = :up when :horizontal start_direction = :right end_direction = :left else raise "#{scroll_mode} is not a valid selector" end try_count = 8 direction = start_direction while hidden? ScreenManager.current_screen.swipe_gesture(direction, distance = 0.1) try_count -= 1 if try_count.zero? if direction == end_direction break else direction = end_direction try_count = 8 end end end end |
#selected? ⇒ Boolean
191 192 193 194 195 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 191 def selected? obj = element object_not_found_exception(obj) obj.selected? end |
#send_keys(value) ⇒ Object
Send keystrokes to this UI element.
60 61 62 63 64 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 60 def send_keys(value) obj = element object_not_found_exception(obj) obj.send_keys(value) end |
#set(value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 39 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_gesture(direction, distance = 0.5) ⇒ Object
Perform a swipe gesture on the UI object in the specified direction. The swipe start point is the center of the UI object, and the swipe end point is the distance specified.
A distance of 1 specifies a swipe gesture with a distance that is the full screen height (vertical swipe), or full screen width (horizontal swipe). A distance of 0.5 specifies a swipe gesture with a distance that is half the screen width or height.
If distance is a value less than zero, then the distance of the swipe gesture will be half the height (vertical) or width (horizontal) of the UI element being swiped. This is useful for preforming swipes/scrolls in vertical or horizontal list objects.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 579 def swipe_gesture(direction, distance = 0.5) raise 'Scroll distance must be less than 1' if distance > 1 obj = element object_not_found_exception(obj) start_pt = [(obj.location.x + (obj.size.width * 0.5)).to_i, (obj.location.y + (obj.size.height * 0.5)).to_i] if distance < 0 top = (start_pt[1] - obj.size.height).to_i bottom = (start_pt[1] + obj.size.height).to_i left = (start_pt[0] - obj.size.width).to_i right = (start_pt[0] + obj.size.width).to_i else screen_size = window_size top = (start_pt[1] - ((screen_size.height * distance) * 0.5)).to_i bottom = (start_pt[1] + ((screen_size.height * distance) * 0.5)).to_i left = (start_pt[0] - ((screen_size.width * distance) * 0.5)).to_i right = (start_pt[0] + ((screen_size.width * distance) * 0.5)).to_i end end_pt = case direction when :up [start_pt[0], bottom] when :down [start_pt[0], top] when :left [right, start_pt[1]] when :right [left, start_pt[1]] end puts "Swipe start_pt = #{start_pt} / end_pt = #{end_pt}" if ENV['DEBUG'] driver.action .click_and_hold(obj) .move_to_location(end_pt[0], end_pt[1], duration: 0.25) .pointer_up .perform end |
#tap ⇒ Object
Tap on a UI element
436 437 438 439 440 441 442 443 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 436 def tap obj = element object_not_found_exception(obj) driver.action .click_and_hold(obj) .release .perform end |
#visible? ⇒ Boolean
Is UI object visible?
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 146 def visible? obj = element return false if obj.nil? begin obj.displayed? rescue reset_mru_cache obj = element return false if obj.nil? obj.displayed? end end |
#wait_until_enabled(seconds = nil, post_exception = true) ⇒ Object
Wait until the object is enabled, 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.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 298 def wait_until_enabled(seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache enabled? end rescue if post_exception raise "UI #{} remained disabled after #{timeout} seconds" unless enabled? else enabled? end end |
#wait_until_exists(seconds = nil, post_exception = true) ⇒ 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.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 210 def wait_until_exists(seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache exists? end rescue if post_exception raise "Could not find UI #{} after #{timeout} seconds" unless exists? else exists? end end |
#wait_until_gone(seconds = nil, post_exception = true) ⇒ 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.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 232 def wait_until_gone(seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache !exists? end rescue if post_exception raise "UI #{} remained visible after #{timeout} seconds" if exists? else exists? end end |
#wait_until_hidden(seconds = nil, post_exception = true) ⇒ 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.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 276 def wait_until_hidden(seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache hidden? end rescue if post_exception raise "UI #{} remained visible after #{timeout} seconds" if visible? else hidden? end end |
#wait_until_value_changes(seconds = nil, post_exception = true) ⇒ 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.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 345 def wait_until_value_changes(seconds = nil, post_exception = true) value = get_value timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache get_value != value end rescue if post_exception raise "Value of UI #{} failed to change from '#{value}' after #{timeout} seconds" if get_value == value else get_value == value end end |
#wait_until_value_is(value, seconds = nil, post_exception = true) ⇒ 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.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 323 def wait_until_value_is(value, seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache compare(value, get_value) end rescue if post_exception raise "Value of UI #{} failed to equal '#{value}' after #{timeout} seconds" unless get_value == value else get_value == value end end |
#wait_until_visible(seconds = nil, post_exception = true) ⇒ 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.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 254 def wait_until_visible(seconds = nil, post_exception = true) timeout = seconds.nil? ? Environ.default_max_wait_time : seconds wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do reset_mru_cache visible? end rescue if post_exception raise "Could not find UI #{} after #{timeout} seconds" unless visible? else visible? end end |
#width ⇒ Integer
Return width of object.
367 368 369 370 371 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 367 def width obj = element object_not_found_exception(obj) obj.size.width end |
#x_loc ⇒ Integer
Return x coordinate of object's location.
391 392 393 394 395 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 391 def x_loc obj = element object_not_found_exception(obj) obj.location.x end |
#y_loc ⇒ Integer
Return y coordinate of object's location.
403 404 405 406 407 |
# File 'lib/testcentricity_mobile/app_elements/app_element.rb', line 403 def y_loc obj = element object_not_found_exception(obj) obj.location.y end |