class AePageObjects::Collection

Attributes

item_class[RW]
item_locator[R]

Private Class Methods

inherited(subclass) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 13
def inherited(subclass)
  subclass.item_class = self.item_class
end

Public Instance Methods

[](index) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 33
def [](index)
  at(index)
end
at(index) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 25
def at(index)
  if index >= size || index < 0
    nil
  else
    item_at(index)
  end
end
each() { |item_at(index)| ... } click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 37
def each
  size.times do |index|
    yield item_at(index)
  end
end
item_class() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 20
def item_class
  self.class.item_class
end
last() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 59
def last
  self.at(size - 1)
end
size() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 43
def size
  #
  # In some cases when #size is called while the DOM is updating, Capybara
  # will catch (and swallow) underlying exceptions such as
  # `Selenium::WebDriver::Error::StaleElementReferenceError`.
  # When this happens it will wait up to the max wait time, which can cause
  # issues for `AePageObjects.wait_until` blocks.
  #
  # To prevent this issue the #all and #size calls are made with the Capybara
  # wait time set to 0.
  #
  Capybara.using_wait_time(0) do
    node.all(:xpath, item_xpath, **options).size
  end
end

Private Instance Methods

configure(options) click to toggle source
Calls superclass method AePageObjects::Element#configure
# File lib/ae_page_objects/elements/collection.rb, line 65
def configure(options)
  super

  @item_locator = options.delete(:item_locator) || default_item_locator
end
default_item_locator() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 125
def default_item_locator
  @default_item_locator ||= [:xpath, ".//*"]
end
item_at(index) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 76
def item_at(index)
  element(is: item_class_at(index), name: index, locator: item_locator_at(index))
end
item_class_at(index) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 80
def item_class_at(index)
  item_class
end
item_locator_at(index) click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 121
def item_locator_at(index)
  [:xpath, "(#{item_xpath})[#{index + 1}]", options]
end
item_xpath() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 84
def item_xpath
  @item_xpath ||= begin
    query_args = eval_locator(@item_locator).dup

    default_options = {
      session_options: Capybara.session_options
    }

    if query_args[1].is_a?(XPath::Expression)
      # Use the { exact: true } setting for XPath selectors that use "XPath.is".  For example, given the XPath
      #   XPath.descendant(:div)[XPath.text.is('Example Text')]
      # the resulting path will be
      #   .//div[./text() = 'Example Text']
      # instead of
      #   .//div[contains(./text(), 'Example Text')]
      # See https://github.com/jnicklas/capybara#exactness for more information.
      default_options[:exact] = true
    end

    if query_args.last.is_a?(::Hash)
      default_options.merge!(query_args.pop)
    end

    query = Capybara::Queries::SelectorQuery.new(*query_args, **default_options)

    result = query.xpath

    # if it's CSS, we need to run it through XPath as Capybara::Queries::SelectorQuery#xpath only
    # works when the selector is xpath. Lame.
    if query.selector.format == :css
      result = XPath.css(query.xpath).to_xpath
    end

    result
  end
end
options() click to toggle source
# File lib/ae_page_objects/elements/collection.rb, line 71
def options
  evaled_locator = eval_locator(@item_locator)
  evaled_locator.last.is_a?(::Hash) ? evaled_locator.last.dup : {}
end