module Watir::Adjacent

Public Instance Methods

child(opt = {}) click to toggle source

Returns element of direct child of current element.

@example

browser.form(id: "new_user").child == browser.fieldset
#=> true
# File lib/watir/adjacent.rb, line 92
def child(opt = {})
  xpath_adjacent(opt.merge(adjacent: :child, plural: false))
end
children(opt = {}) click to toggle source

Returns collection of elements of direct children of current element.

@example

children = browser.select_list(id: "new_user_languages").children
children == browser.select_list(id: "new_user_languages").options.to_a
#=> true
# File lib/watir/adjacent.rb, line 105
def children(opt = {})
  raise ArgumentError, '#children can not take an index value' if opt[:index]

  xpath_adjacent(opt.merge(adjacent: :child, plural: true))
end
following_sibling(opt = {}) click to toggle source

Returns following sibling element of current element.

@example

following_sibling = browser.text_field(name: "new_user_first_name").following_sibling(index: 2)
following_sibling == browser.text_field(id: "new_user_last_name")
#=> true
# File lib/watir/adjacent.rb, line 52
def following_sibling(opt = {})
  xpath_adjacent(opt.merge(adjacent: :following, plural: false))
end
Also aliased as: next_sibling
following_siblings(opt = {}) click to toggle source

Returns collection of following sibling elements of current element.

@example

browser.text_field(name: "new_user_first_name").following_siblings.size
#=> 55
# File lib/watir/adjacent.rb, line 65
def following_siblings(opt = {})
  raise ArgumentError, '#next_siblings can not take an index value' if opt[:index]

  xpath_adjacent(opt.merge(adjacent: :following, plural: true))
end
Also aliased as: next_siblings
next_sibling(opt = {})
Alias for: following_sibling
next_siblings(opt = {})
Alias for: following_siblings
parent(opt = {}) click to toggle source

Returns parent element of current element.

@example

browser.text_field(name: "new_user_first_name").parent == browser.fieldset
#=> true
# File lib/watir/adjacent.rb, line 11
def parent(opt = {})
  xpath_adjacent(opt.merge(adjacent: :ancestor, plural: false))
end
preceding_sibling(opt = {}) click to toggle source

Returns preceding sibling element of current element.

@example

browser.text_field(name: "new_user_first_name").preceding_sibling(index: 1) == browser.legend
#=> true
# File lib/watir/adjacent.rb, line 23
def preceding_sibling(opt = {})
  xpath_adjacent(opt.merge(adjacent: :preceding, plural: false))
end
Also aliased as: previous_sibling
preceding_siblings(opt = {}) click to toggle source

Returns collection of preceding sibling elements of current element.

@example

browser.text_field(name: "new_user_first_name").preceding_siblings.size
#=> 3
# File lib/watir/adjacent.rb, line 36
def preceding_siblings(opt = {})
  raise ArgumentError, '#previous_siblings can not take an index value' if opt[:index]

  xpath_adjacent(opt.merge(adjacent: :preceding, plural: true))
end
Also aliased as: previous_siblings
previous_sibling(opt = {})
Alias for: preceding_sibling
previous_siblings(opt = {})
Alias for: preceding_siblings
siblings(opt = {}) click to toggle source

Returns collection of siblings of current element, including current element.

@example

browser.text_field(name: "new_user_first_name").siblings.size
#=> 59
# File lib/watir/adjacent.rb, line 80
def siblings(opt = {})
  parent.children(opt)
end

Private Instance Methods

xpath_adjacent(opt = {}) click to toggle source
# File lib/watir/adjacent.rb, line 113
def xpath_adjacent(opt = {})
  plural = opt.delete(:plural)
  opt[:index] ||= 0 unless plural || opt.values.any? { |e| e.is_a? Regexp }
  if !plural
    el = Watir.element_class_for(opt[:tag_name] || '').new(self, opt)
    el.is_a?(Input) ? el.to_subtype : el
  elsif opt[:tag_name]
    Watir.const_get("#{Watir.element_class_for(opt[:tag_name])}Collection").new(self, opt)
  else
    HTMLElementCollection.new(self, opt)
  end
end