Class: TestCentricity::UIElement

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of UIElement.



45
46
47
48
49
50
51
52
53
54
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 45

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

Instance Attribute Details

#alt_locatorObject

Returns the value of attribute alt_locator.



40
41
42
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 40

def alt_locator
  @alt_locator
end

#contextObject (readonly)

Returns the value of attribute context.



39
40
41
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 39

def context
  @context
end

#locatorObject (readonly)

Returns the value of attribute locator.



39
40
41
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 39

def locator
  @locator
end

#locator_typeObject

Returns the value of attribute locator_type.



40
41
42
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 40

def locator_type
  @locator_type
end

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 39

def name
  @name
end

#original_styleObject

Returns the value of attribute original_style.



40
41
42
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 40

def original_style
  @original_style
end

#parentObject (readonly)

Returns the value of attribute parent.



39
40
41
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 39

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



39
40
41
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 39

def type
  @type
end

Instance Method Details

#clear_alt_locatorObject



101
102
103
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 101

def clear_alt_locator
  @alt_locator = nil
end

#clickObject

Click on an object

Examples:

basket_link.click


110
111
112
113
114
115
116
117
118
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 110

def click
  obj, type = find_element
  object_not_found_exception(obj, type)
  begin
    obj.click
  rescue StandardError
    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



149
150
151
152
153
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 149

def click_at(x, y)
  obj, = find_element
  raise "UI #{object_ref_message} not found" unless obj
  obj.click_at(x, y)
end

#count(visible = true) ⇒ Object

Return the number of occurrences of an object with an ambiguous locator that evaluates to multiple UI elements.

Examples:

num_uploads = upload_progress_bars.count(:all)

Parameters:

  • visible (Boolean, Symbol) (defaults to: true)

    Only find elements with the specified visibility:

    • true - only finds visible elements.
    • false - finds invisible and visible elements.
    • :all - same as false; finds visible and invisible elements.
    • :hidden - only finds invisible elements.
    • :visible - same as true; only finds visible elements.


377
378
379
380
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 377

def count(visible = true)
  obj_locator = @alt_locator.nil? ? @locator : @alt_locator
  page.all(@locator_type, obj_locator, wait: 0.01, visible: visible, minimum: 0).count
end

#disabled?Boolean

Is UI object disabled (not enabled)?

Examples:

.disabled?

Returns:

  • (Boolean)


242
243
244
245
246
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 242

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)


436
437
438
439
440
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 436

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

#double_clickObject

Double-click on an object

Examples:

file_image.double_click


125
126
127
128
129
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 125

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



483
484
485
486
487
488
489
490
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 483

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



475
476
477
478
479
480
481
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 475

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)


232
233
234
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 232

def enabled?
  !disabled?
end

#exists?(visible = true) ⇒ Boolean

Does UI object exists?

Examples:

basket_link.exists?

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 179

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

#get_attribute(attrib) ⇒ Object



540
541
542
543
544
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 540

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

#get_locatorObject



89
90
91
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 89

def get_locator
  @locator
end

#get_locator_typeObject



71
72
73
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 71

def get_locator_type
  @locator_type
end

#get_nameObject



93
94
95
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 93

def get_name
  @name
end

#get_native_attribute(attrib) ⇒ Object



546
547
548
549
550
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 546

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

#get_object_typeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 75

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_typeObject



9
10
11
12
13
# File 'lib/testcentricity_web/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



442
443
444
445
446
447
448
449
450
451
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 442

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

#heightInteger

Return height of object.

Examples:

button_height = my_button.height

Returns:

  • (Integer)


400
401
402
403
404
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 400

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)


222
223
224
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 222

def hidden?
  !visible?
end

#highlight(duration = 1) ⇒ Object

Highlight an object with a 3 pixel wide, red dashed border for the specified wait time. If wait time is zero, then the highlight will remain until the page is refreshed

Examples:

error_message.highlight(3)

Parameters:

  • duration (Integer or Float) (defaults to: 1)

    wait time in seconds



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 499

def highlight(duration = 1)
  obj, type = find_element
  object_not_found_exception(obj, type)
  # store original style so it can be reset later
  @original_style = obj.native.attribute('style')
  # style element with red border
  page.execute_script(
    'arguments[0].setAttribute(arguments[1], arguments[2])',
    obj,
    'style',
    'border: 3px solid red; border-style: dashed;'
  )
  # keep element highlighted for duration and then revert to original style
  if duration.positive?
    sleep duration
    page.execute_script(
      'arguments[0].setAttribute(arguments[1], arguments[2])',
      obj,
      'style',
      @original_style
    )
  end
end

#hoverObject

Hover the cursor over an object

Examples:

basket_link.hover


469
470
471
472
473
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 469

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/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_clickObject

Right-click on an object

Examples:

basket_item_image.right_click


136
137
138
139
140
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 136

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

comment_field.send_keys(:enter)



167
168
169
170
171
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 167

def send_keys(*keys)
  obj, type = find_element
  object_not_found_exception(obj, type)
  obj.send_keys(*keys)
end

#set(value) ⇒ Object



155
156
157
158
159
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 155

def set(value)
  obj, type = find_element
  object_not_found_exception(obj, type)
  obj.set(value)
end

#set_alt_locator(temp_locator) ⇒ Object



97
98
99
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 97

def set_alt_locator(temp_locator)
  @alt_locator = temp_locator
end

#set_locator_type(locator = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 56

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) }
  @locator_type = if is_xpath && !is_css
                    :xpath
                  elsif is_css && !is_xpath
                    :css
                  elsif !is_css && !is_xpath
                    :css
                  else
                    :css
                  end
end

#unhighlightObject

Restore a highlighted object's original style

Examples:

store_link.unhighlight


528
529
530
531
532
533
534
535
536
537
538
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 528

def unhighlight
  obj, type = find_element
  object_not_found_exception(obj, type)
  return if @original_style.nil?
  page.execute_script(
    'arguments[0].setAttribute(arguments[1], arguments[2])',
    obj,
    'style',
    @original_style
  )
end

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



455
456
457
458
459
460
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 455

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)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 190

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, 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 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



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 255

def wait_until_exists(seconds = nil, post_exception = true)
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { exists? }
rescue StandardError
  if post_exception
    raise "Could not find UI #{object_ref_message} 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 Capybara.default_max_wait_time.

Examples:

logout_button.wait_until_gone(5)

Parameters:

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

    wait time in seconds



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 274

def wait_until_gone(seconds = nil, post_exception = true)
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { !exists? }
rescue StandardError
  if post_exception
    raise "UI #{object_ref_message} 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 Capybara.default_max_wait_time.

Examples:

run_button.wait_until_hidden(10)

Parameters:

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

    wait time in seconds



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 312

def wait_until_hidden(seconds = nil, post_exception = true)
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { hidden? }
rescue StandardError
  if post_exception
    raise "UI #{object_ref_message} remained visible after #{timeout} seconds" if visible?
  else
    visible?
  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 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



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 353

def wait_until_value_changes(seconds = nil, post_exception = true)
  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 StandardError
  if post_exception
    raise "Value of UI #{object_ref_message} 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 Capybara.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



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 334

def wait_until_value_is(value, seconds = nil, post_exception = true)
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { compare(value, get_value) }
rescue StandardError
  if post_exception
    raise "Value of UI #{object_ref_message} 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 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



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 293

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

#widthInteger

Return width of object.

Examples:

button_width = my_button.width

Returns:

  • (Integer)


388
389
390
391
392
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 388

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

#xInteger

Return x coordinate of object's location.

Examples:

button_x = my_button.x

Returns:

  • (Integer)


412
413
414
415
416
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 412

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

#yInteger

Return y coordinate of object's location.

Examples:

button_y = my_button.y

Returns:

  • (Integer)


424
425
426
427
428
# File 'lib/testcentricity_web/web_elements/ui_elements_helper.rb', line 424

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