class TestCentricity::UIElement
Constants
- CSS_SELECTORS
- XPATH_SELECTORS
Attributes
Public Class Methods
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 43 def initialize(name, parent, locator, context) @name = name @parent = parent @locator = locator @context = context @type = nil @alt_locator = nil set_locator_type end
Public Instance Methods
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 94 def clear_alt_locator @alt_locator = nil end
Click on an object
@example
basket_link.click
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 103 def click obj, type = find_element object_not_found_exception(obj, type) begin obj.click rescue obj.click_at(10, 10) unless Capybara.current_driver == :poltergeist end end
Click at a specific location within an object
@param x [Integer] X offset @param y [Integer] Y offset @example
basket_item_image.click_at(10, 10)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 142 def click_at(x, y) obj, = find_element raise "UI #{object_ref_message} not found" unless obj obj.click_at(x, y) end
Is UI object disabled (not enabled)?
@return [Boolean] @example
login_button.disabled?
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 235 def disabled? obj, type = find_element object_not_found_exception(obj, type) obj.disabled? end
Is UI object displayed in browser window?
@return [Boolean] @example
basket_link.displayed??
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 389 def displayed? obj, type = find_element(false) object_not_found_exception(obj, type) obj.displayed? end
Double-click on an object
@example
file_image.double_click
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 118 def double_click obj, type = find_element object_not_found_exception(obj, type) page.driver.browser.action.double_click(obj.native).perform end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 436 def drag_and_drop(target, right_offset = nil, down_offset = nil) source, type = find_element object_not_found_exception(source, type) page.driver.browser.action.click_and_hold(source.native).perform sleep(1) target_drop, = target.find_element page.driver.browser.action.move_to(target_drop.native, right_offset.to_i, down_offset.to_i).release.perform end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 428 def drag_by(right_offset, down_offset) obj, type = find_element object_not_found_exception(obj, type) page.driver.browser.action.click_and_hold(obj.native).perform sleep(1) obj.drag_by(right_offset, down_offset) end
Is UI object enabled?
@return [Boolean] @example
login_button.enabled?
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 225 def enabled? !disabled? end
Does UI object exists?
@return [Boolean] @example
basket_link.exists?
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 172 def exists?(visible = true) obj, = find_object(visible) obj != nil end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 445 def get_attribute(attrib) obj, type = find_element(false) object_not_found_exception(obj, type) obj[attrib] end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 82 def get_locator @locator end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 86 def get_name @name end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 451 def get_native_attribute(attrib) obj, type = find_element(false) object_not_found_exception(obj, type) obj.native.attribute(attrib) end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 68 def get_object_type if @type @type else obj, type = find_element object_not_found_exception(obj, type) if obj.tag_name obj.tag_name elsif obj.native.attribute('type') obj.native.attribute('type') end end end
# File lib/testcentricity/web_elements/siebel_open_ui_helper.rb, line 9 def get_siebel_object_type obj, = find_element object_not_found_exception(obj, 'Siebel object') obj.native.attribute('ot') end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 395 def get_value(visible = true) obj, type = find_element(visible) object_not_found_exception(obj, type) case obj.tag_name.downcase when 'input', 'select', 'textarea' obj.value else obj.text end end
Return height of object.
@return [Integer] @example
button_height = my_button.height
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 353 def height obj, type = find_element(false) object_not_found_exception(obj, type) obj.get_height end
Hover the cursor over an object
@example
basket_link.hover
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 422 def hover obj, type = find_element object_not_found_exception(obj, type) obj.hover end
# File lib/testcentricity/web_elements/siebel_open_ui_helper.rb, line 3 def invoke_siebel_dialog(popup, seconds = nil) invoke_siebel_popup timeout = seconds.nil? ? 15 : seconds popup.wait_until_exists(timeout) end
Right-click on an object
@example
basket_item_image.right_click
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 129 def right_click obj, type = find_element object_not_found_exception(obj, type) page.driver.browser.action.context_click(obj.native).perform end
Send keystrokes to this object.
@param keys [String] keys @example
comment_field.send_keys(:enter)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 160 def send_keys(*keys) obj, type = find_element object_not_found_exception(obj, type) obj.send_keys(*keys) end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 148 def set(value) obj, type = find_element object_not_found_exception(obj, type) obj.set(value) end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 90 def set_alt_locator(temp_locator) @alt_locator = temp_locator end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 53 def set_locator_type(locator = nil) locator = @locator if locator.nil? is_xpath = XPATH_SELECTORS.any? { |selector| locator.include?(selector) } is_css = CSS_SELECTORS.any? { |selector| locator.include?(selector) } if is_xpath && !is_css @locator_type = :xpath elsif is_css && !is_xpath @locator_type = :css elsif !is_css && !is_xpath @locator_type = :css else raise "Cannot determine type of locator for UIElement '#{@name}' - locator = #{locator}" end end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 408 def verify_value(expected, enqueue = false) actual = get_value enqueue ? ExceptionQueue.enqueue_assert_equal(expected.strip, actual.strip, "Expected UI #{object_ref_message}") : assert_equal(expected.strip, actual.strip, "Expected UI #{object_ref_message} to display '#{expected}' but found '#{actual}'") end
Is UI object visible?
@return [Boolean] @example
remember_me_checkbox.visible?
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 183 def visible? obj, type = find_object exists = obj invisible = false if type == :css Capybara.using_wait_time 0.1 do # is object itself hidden with .ui-helper-hidden class? self_hidden = page.has_css?("#{@locator}.ui-helper-hidden") # is parent of object hidden, thus hiding the object? parent_hidden = page.has_css?(".ui-helper-hidden > #{@locator}") # is grandparent of object, or any other ancestor, hidden? other_ancestor_hidden = page.has_css?(".ui-helper-hidden * #{@locator}") # if any of the above conditions are true, then object is invisible invisible = self_hidden || parent_hidden || other_ancestor_hidden end else invisible = !obj.visible? if exists end # the object is visible if it exists and it is not invisible if exists && !invisible true else false end end
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 Capybara.default_max_wait_time.
@param seconds [Integer or Float] wait time in seconds @example
run_button.wait_until_exists(0.5)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 248 def wait_until_exists(seconds = nil) timeout = seconds.nil? ? Capybara.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 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 Capybara.default_max_wait_time.
@param seconds [Integer or Float] wait time in seconds @example
logout_button.wait_until_gone(5)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 263 def wait_until_gone(seconds = nil) timeout = seconds.nil? ? Capybara.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 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 Capybara.default_max_wait_time.
@param seconds [Integer or Float] wait time in seconds @example
basket_grand_total_label.wait_until_value_changes(5)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 326 def wait_until_value_changes(seconds = nil) value = get_value timeout = seconds.nil? ? Capybara.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 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 Capybara.default_max_wait_time.
@param value [String or Hash] value expected or comparison hash @param seconds [Integer or Float] wait time in seconds @example
card_authorized_label.wait_until_value_is('Card authorized', 5) or total_weight_field.wait_until_value_is({ :greater_than => '250' }, 5)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 311 def wait_until_value_is(value, seconds = nil) timeout = seconds.nil? ? Capybara.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 the object is visible, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Capybara.default_max_wait_time.
@param seconds [Integer or Float] wait time in seconds @example
run_button.wait_until_visible(0.5)
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 278 def wait_until_visible(seconds = nil) timeout = seconds.nil? ? Capybara.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
Return width of object.
@return [Integer] @example
button_width = my_button.width
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 341 def width obj, type = find_element(false) object_not_found_exception(obj, type) obj.get_width end
Return x coordinate of object's location.
@return [Integer] @example
button_x = my_button.x
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 365 def x obj, type = find_element(false) object_not_found_exception(obj, type) obj.get_x end
Return y coordinate of object's location.
@return [Integer] @example
button_y = my_button.y
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 377 def y obj, type = find_element(false) object_not_found_exception(obj, type) obj.get_y end
Private Instance Methods
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 509 def compare(expected, actual) if expected.is_a?(Hash) result = false expected.each do |key, value| case key when :lt, :less_than result = actual < value when :lt_eq, :less_than_or_equal result = actual <= value when :gt, :greater_than result = actual > value when :gt_eq, :greater_than_or_equal result = actual >= value when :not_equal result = actual != value end end else result = expected == actual end result end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 459 def find_element(visible = true) wait = Selenium::WebDriver::Wait.new(timeout: Capybara.default_max_wait_time) wait.until { find_object(visible) } end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 464 def find_object(visible = true) @alt_locator.nil? ? obj_locator = @locator : obj_locator = @alt_locator parent_section = @context == :section && !@parent.get_locator.nil? parent_section ? tries ||= 2 : tries ||= 1 if parent_section && tries > 1 parent_locator = @parent.get_locator parent_locator = parent_locator.gsub('|', ' ') parent_locator_type = @parent.get_locator_type obj = page.find(parent_locator_type, parent_locator, :wait => 0.01).find(@locator_type, obj_locator, :wait => 0.01, :visible => visible) else obj = page.find(@locator_type, obj_locator, :wait => 0.01, :visible => visible) end [obj, @locator_type] rescue retry if (tries -= 1) > 0 [nil, nil] end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 489 def invalid_object_type_exception(obj, obj_type) unless obj.tag_name == obj_type || obj.native.attribute('type') == obj_type @alt_locator.nil? ? locator = @locator : locator = @alt_locator raise "#{locator} is not a #{obj_type} element" end end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 496 def invoke_siebel_popup obj, = find_element object_not_found_exception(obj, 'Siebel object') trigger_name = obj.native.attribute('aria-describedby').strip trigger = "span##{trigger_name}" trigger = "#{@parent.get_locator} #{trigger}" if @context == :section && !@parent.get_locator.nil? first(trigger).click end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 483 def object_not_found_exception(obj, obj_type) @alt_locator.nil? ? locator = @locator : locator = @alt_locator obj_type.nil? ? object_type = 'Object' : object_type = obj_type raise ObjectNotFoundError.new("#{object_type} named '#{@name}' (#{locator}) not found") unless obj end
# File lib/testcentricity/web_elements/ui_elements_helper.rb, line 505 def object_ref_message "object '#{get_name}' (#{get_locator})" end