Class: TestCentricity::Elements::List

Inherits:
UIElement show all
Defined in:
lib/testcentricity_web/web_elements/list.rb

Constant Summary

Constants inherited from UIElement

UIElement::CSS_SELECTORS, UIElement::XPATH_SELECTORS

Instance Attribute Summary collapse

Attributes inherited from UIElement

#alt_locator, #base_object, #context, #locator, #locator_type, #mru_driver, #mru_locator, #mru_object, #mru_parent, #name, #original_style, #parent, #type

Instance Method Summary collapse

Methods inherited from UIElement

#aria_autocomplete, #aria_busy?, #aria_checked?, #aria_colcount, #aria_controls, #aria_describedby, #aria_disabled?, #aria_expanded?, #aria_haspopup?, #aria_hidden?, #aria_invalid?, #aria_keyshortcuts, #aria_label, #aria_labelledby, #aria_live, #aria_modal?, #aria_multiline?, #aria_multiselectable?, #aria_orientation, #aria_pressed?, #aria_readonly?, #aria_required?, #aria_roledescription, #aria_rowcount, #aria_selected?, #aria_sort, #aria_valuemax, #aria_valuemin, #aria_valuenow, #aria_valuetext, #clear_alt_locator, #click, #click_at, #content_editable?, #count, #crossorigin, #disabled?, #displayed?, #double_click, #drag_and_drop, #drag_by, #enabled?, #exists?, #find_element, #focused?, #get_attribute, #get_locator, #get_locator_type, #get_name, #get_native_attribute, #get_object_type, #get_value, #height, #hidden?, #highlight, #hover, #hover_at, #obscured?, #required?, #reset_mru_cache, #right_click, #role, #scroll_to, #send_keys, #set, #set_alt_locator, #set_locator_type, #style, #tabindex, #title, #unhighlight, #verify_value, #visible?, #wait_until_enabled, #wait_until_exists, #wait_until_gone, #wait_until_hidden, #wait_until_value_changes, #wait_until_value_is, #wait_until_visible, #wait_while_busy, #width, #x, #y

Constructor Details

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

Returns a new instance of List.



7
8
9
10
11
12
13
14
15
# File 'lib/testcentricity_web/web_elements/list.rb', line 7

def initialize(name, parent, locator, context)
  super
  @type = :list
  list_spec = {
    selected_item: 'li.selected',
    list_item:     'li'
  }
  define_list_elements(list_spec)
end

Instance Attribute Details

#list_itemObject

Returns the value of attribute list_item.



4
5
6
# File 'lib/testcentricity_web/web_elements/list.rb', line 4

def list_item
  @list_item
end

#selected_itemObject

Returns the value of attribute selected_item.



5
6
7
# File 'lib/testcentricity_web/web_elements/list.rb', line 5

def selected_item
  @selected_item
end

Instance Method Details

#choose_item(item) ⇒ Object

Select the specified item in a list object. Accepts a String or Integer.

Examples:

province_list.choose_item(2)
province_list.choose_item('Manitoba')

Parameters:

  • item (String, Integer)

    text or index of item to select



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/testcentricity_web/web_elements/list.rb', line 38

def choose_item(item)
  obj, = find_element
  object_not_found_exception(obj, nil)
  if item.is_a?(Integer)
    obj.find(:css, "#{@list_item}:nth-of-type(#{item})", visible: true, wait: 2).click
  elsif item.is_a?(String)
    items = obj.all(@list_item).collect(&:text)
    sleep(2) unless items.include?(item)
    obj.first(:css, @list_item, text: item).click
  end
end

#define_list_elements(element_spec) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/testcentricity_web/web_elements/list.rb', line 17

def define_list_elements(element_spec)
  element_spec.each do |element, value|
    case element
    when :list_item
      @list_item = value
    when :selected_item
      @selected_item = value
    else
      raise "#{element} is not a recognized list element"
    end
  end
end

#get_all_items_countObject



117
118
119
120
121
# File 'lib/testcentricity_web/web_elements/list.rb', line 117

def get_all_items_count
  obj, = find_element
  object_not_found_exception(obj, nil)
  obj.all(@list_item, visible: :all).count
end

#get_all_list_items(element_spec = nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/testcentricity_web/web_elements/list.rb', line 110

def get_all_list_items(element_spec = nil)
  define_list_elements(element_spec) unless element_spec.nil?
  obj, = find_element
  object_not_found_exception(obj, nil)
  obj.all(@list_item, visible: :all).collect(&:text)
end

#get_item_countInteger Also known as: item_count

Return the number of items in a list object.

Examples:

num_nav_items = nav_list.get_item_count

Returns:

  • (Integer)


102
103
104
105
106
# File 'lib/testcentricity_web/web_elements/list.rb', line 102

def get_item_count
  obj, = find_element
  object_not_found_exception(obj, nil)
  obj.all(@list_item, visible: true, minimum: 0, wait: 2).count
end

#get_list_item(index, visible = true) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/testcentricity_web/web_elements/list.rb', line 87

def get_list_item(index, visible = true)
  items = visible ? get_list_items : get_all_list_items
  item = items[index - 1]
  item.delete!("\n")
  item.delete!("\r")
  item.delete!("\t")
  item
end

#get_list_items(element_spec = nil) ⇒ Array

Return array of strings of all items in a list object.

Examples:

nav_items = nav_list.get_options

Returns:

  • (Array)


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

def get_list_items(element_spec = nil)
  define_list_elements(element_spec) unless element_spec.nil?
  obj, = find_element
  object_not_found_exception(obj, nil)
  items = obj.all(@list_item, visible: true, minimum: 0, wait: 2).collect(&:text)

  items.map!{ |item| item.delete("\n") }
  items.map!{ |item| item.delete("\r") }
  items.map!{ |item| item.delete("\t") }
  items.map!{ |item| item.strip }
end

#get_list_row_locator(row) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/testcentricity_web/web_elements/list.rb', line 146

def get_list_row_locator(row)
  case @locator_type
  when :xpath
    "#{@locator}/#{@list_item}[#{row}]"
  when :css
    "#{@locator} > #{@list_item}:nth-of-type(#{row})"
  end
end

#get_selected_itemString Also known as: selected?

Return text of first selected item in a list object.

Examples:

current_selection = nav_list.get_selected_item

Returns:



129
130
131
132
133
# File 'lib/testcentricity_web/web_elements/list.rb', line 129

def get_selected_item
  obj, = find_element
  object_not_found_exception(obj, nil)
  obj.first(:css, @list_item, minimum: 0) ? obj.first(:css, @selected_item).text : nil
end

#hover_item(item) ⇒ Object

Hover over the specified item in a list object. Accepts a String or Integer.

Examples:

province_list.hover_item(2)
province_list.hover_item('Manitoba')

Parameters:

  • item (String, Integer)

    text or index of item to hover over



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

def hover_item(item)
  obj, = find_element
  object_not_found_exception(obj, nil)
  if item.is_a?(Integer)
    obj.find(:css, "#{@list_item}:nth-of-type(#{item})", visible: true, wait: 2).hover
  elsif item.is_a?(String)
    items = obj.all(@list_item).collect(&:text)
    sleep(2) unless items.include?(item)
    obj.first(:css, @list_item, text: item, wait: 2).hover
  end
end

#verify_list_items(expected, enqueue = false) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/testcentricity_web/web_elements/list.rb', line 137

def verify_list_items(expected, enqueue = false)
  actual = get_list_items
  if enqueue
    ExceptionQueue.enqueue_assert_equal(expected, actual, "Expected list #{object_ref_message}")
  else
    assert_equal(expected, actual, "Expected list #{object_ref_message} to be #{expected} but found #{actual}")
  end
end

#wait_until_item_count_changes(seconds = nil, post_exception = true) ⇒ Object

Wait until the list's item count 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:

search_results_list.wait_until_value_changes(5)

Parameters:

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

    wait time in seconds



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/testcentricity_web/web_elements/list.rb', line 184

def wait_until_item_count_changes(seconds = nil, post_exception = true)
  value = get_item_count
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { get_item_count != value }
rescue
  if post_exception
    raise "Value of List #{object_ref_message} failed to change from '#{value}' after #{timeout} seconds" if get_item_count == value
  else
    get_item_count == value
  end
end

#wait_until_item_count_is(value, seconds = nil, post_exception = true) ⇒ Object

Wait until the list's item count 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:

search_results_list.wait_until_item_count_is(10, 15)
  or
search_results_list.wait_until_item_count_is({ greater_than_or_equal: 1 }, 5)

Parameters:

  • value (Integer or Hash)

    value expected or comparison hash

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

    wait time in seconds



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/testcentricity_web/web_elements/list.rb', line 165

def wait_until_item_count_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_item_count) }
rescue
  if post_exception
    raise "Value of List #{object_ref_message} failed to equal '#{value}' after #{timeout} seconds" unless get_item_count == value
  else
    get_item_count == value
  end
end