Class: TestCentricity::UIElement

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL, Test::Unit::Assertions
Defined in:
lib/testcentricity/web_elements/ui_elements_helper.rb,
lib/testcentricity/web_elements/siebel_open_ui_helper.rb

Constant Summary collapse

XPATH_SELECTORS =
['//', '[@', '[contains(@']
CSS_SELECTORS =
['#', ':nth-child(', ':nth-of-type(', '^=', '$=', '*=']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of UIElement.



43
44
45
46
47
48
49
50
51
# 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

Instance Attribute Details

#alt_locator ⇒ Object

Returns the value of attribute alt_locator.



38
39
40
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 38

def alt_locator
  @alt_locator
end

#context ⇒ Object (readonly)

Returns the value of attribute context.



37
38
39
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 37

def context
  @context
end

#locator ⇒ Object (readonly)

Returns the value of attribute locator.



37
38
39
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 37

def locator
  @locator
end

#locator_type ⇒ Object

Returns the value of attribute locator_type.



38
39
40
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 38

def locator_type
  @locator_type
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 37

def name
  @name
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



37
38
39
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 37

def parent
  @parent
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



37
38
39
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 37

def type
  @type
end

Instance Method Details

#clear_alt_locator ⇒ Object



94
95
96
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 94

def clear_alt_locator
  @alt_locator = nil
end

#click ⇒ Object

Click on an object

Examples:

basket_link.click


103
104
105
106
107
108
109
110
111
# 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(x, y) ⇒ Object

Click at a specific location within an object

Examples:

basket_item_image.click_at(10, 10)

Parameters:

  • x (Integer) —

    X offset

  • y (Integer) —

    Y offset



142
143
144
145
146
# 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

#disabled? ⇒ Boolean

Is UI object disabled (not enabled)?

Examples:

.disabled?

Returns:

  • (Boolean)


235
236
237
238
239
# 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

#displayed? ⇒ Boolean

Is UI object displayed in browser window?

Examples:

basket_link.displayed??

Returns:

  • (Boolean)


386
387
388
389
390
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 386

def displayed?
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.displayed?
end

#double_click ⇒ Object

Double-click on an object

Examples:

file_image.double_click


118
119
120
121
122
# 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

#drag_and_drop(target, right_offset = nil, down_offset = nil) ⇒ Object



433
434
435
436
437
438
439
440
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 433

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

#drag_by(right_offset, down_offset) ⇒ Object



425
426
427
428
429
430
431
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 425

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

#enabled? ⇒ Boolean

Is UI object enabled?

Examples:

.enabled?

Returns:

  • (Boolean)


225
226
227
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 225

def enabled?
  !disabled?
end

#exists?(visible = true) ⇒ Boolean

Does UI object exists?

Examples:

basket_link.exists?

Returns:

  • (Boolean)


172
173
174
175
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 172

def exists?(visible = true)
  obj, = find_object(visible)
  obj != nil
end

#get_attribute(attrib) ⇒ Object



442
443
444
445
446
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 442

def get_attribute(attrib)
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj[attrib]
end

#get_locator ⇒ Object



82
83
84
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 82

def get_locator
  @locator
end

#get_name ⇒ Object



86
87
88
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 86

def get_name
  @name
end

#get_native_attribute(attrib) ⇒ Object



448
449
450
451
452
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 448

def get_native_attribute(attrib)
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.native.attribute(attrib)
end

#get_object_type ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

#get_siebel_object_type ⇒ Object



9
10
11
12
13
# 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

#get_value(visible = true) ⇒ Object Also known as: get_caption



392
393
394
395
396
397
398
399
400
401
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 392

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

#height ⇒ Integer

Return height of object.

Examples:

button_height = my_button.height

Returns:

  • (Integer)


350
351
352
353
354
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 350

def height
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.get_height
end

#hidden? ⇒ Boolean

Is UI object hidden (not visible)?

Examples:

remember_me_checkbox.hidden?

Returns:

  • (Boolean)


215
216
217
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 215

def hidden?
  !visible?
end

#hover ⇒ Object

Hover the cursor over an object

Examples:

basket_link.hover


419
420
421
422
423
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 419

def hover
  obj, type = find_element
  object_not_found_exception(obj, type)
  obj.hover
end

#invoke_siebel_dialog(popup, seconds = nil) ⇒ Object



3
4
5
6
7
# 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 ⇒ Object

Right-click on an object

Examples:

basket_item_image.right_click


129
130
131
132
133
# 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_keys(*keys) ⇒ Object

Send keystrokes to this object.

Examples:

comment_field.send_keys(:enter)

Parameters:



160
161
162
163
164
# 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

#set(value) ⇒ Object



148
149
150
151
152
# 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

#set_alt_locator(temp_locator) ⇒ Object



90
91
92
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 90

def set_alt_locator(temp_locator)
  @alt_locator = temp_locator
end

#set_locator_type(locator = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#verify_value(expected, enqueue = false) ⇒ Object Also known as: verify_caption



405
406
407
408
409
410
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 405

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

#visible? ⇒ Boolean

Is UI object visible?

Examples:

remember_me_checkbox.visible?

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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_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 Capybara.default_max_wait_time.

Examples:

run_button.wait_until_exists(0.5)

Parameters:

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

    wait time in seconds



248
249
250
251
252
253
254
# 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_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 Capybara.default_max_wait_time.

Examples:

logout_button.wait_until_gone(5)

Parameters:

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

    wait time in seconds



263
264
265
266
267
268
269
# 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_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 Capybara.default_max_wait_time.

Examples:

run_button.wait_until_hidden(10)

Parameters:

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

    wait time in seconds



293
294
295
296
297
298
299
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 293

def wait_until_hidden(seconds = nil)
  timeout = seconds.nil? ? Capybara.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 Capybara.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



323
324
325
326
327
328
329
330
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 323

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_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 Capybara.default_max_wait_time.

Examples:

card_authorized_label.wait_until_value_is(5, 'Card authorized')

Parameters:

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

    wait time in seconds



308
309
310
311
312
313
314
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 308

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 { get_value == 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 Capybara.default_max_wait_time.

Examples:

run_button.wait_until_visible(0.5)

Parameters:

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

    wait time in seconds



278
279
280
281
282
283
284
# 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

#width ⇒ Integer

Return width of object.

Examples:

button_width = my_button.width

Returns:

  • (Integer)


338
339
340
341
342
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 338

def width
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.get_width
end

#x ⇒ Integer

Return x coordinate of object's location.

Examples:

button_x = my_button.x

Returns:

  • (Integer)


362
363
364
365
366
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 362

def x
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.get_x
end

#y ⇒ Integer

Return y coordinate of object's location.

Examples:

button_y = my_button.y

Returns:

  • (Integer)


374
375
376
377
378
# File 'lib/testcentricity/web_elements/ui_elements_helper.rb', line 374

def y
  obj, type = find_element(false)
  object_not_found_exception(obj, type)
  obj.get_y
end