Module: Watir::Adjacent

Included in:
Element
Defined in:
lib/watir/adjacent.rb

Instance Method Summary collapse

Instance Method Details

#child(opt = {}) ⇒ Object

Returns element of direct child of current element.

Examples:

browser.form(id: "new_user").child == browser.fieldset#=> true

92
93
94
# File 'lib/watir/adjacent.rb', line 92

def child(opt = {})
  xpath_adjacent(opt.merge(adjacent: :child, plural: false))
end

#children(opt = {}) ⇒ Object

Returns collection of elements of direct children of current element.

Examples:

children = browser.select_list(id: "new_user_languages").children
children == browser.select_list(id: "new_user_languages").options.to_a#=> true

Raises:

  • (ArgumentError)

105
106
107
108
109
# 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 = {}) ⇒ Object Also known as: next_sibling

Returns following sibling element of current element.

Examples:

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

52
53
54
# File 'lib/watir/adjacent.rb', line 52

def following_sibling(opt = {})
  xpath_adjacent(opt.merge(adjacent: :following, plural: false))
end

#following_siblings(opt = {}) ⇒ Object Also known as: next_siblings

Returns collection of following sibling elements of current element.

Examples:

browser.text_field(name: "new_user_first_name").following_siblings.size#=> 55

Raises:

  • (ArgumentError)

65
66
67
68
69
# 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

#parent(opt = {}) ⇒ Object

Returns parent element of current element.

Examples:

browser.text_field(name: "new_user_first_name").parent == browser.fieldset#=> true

11
12
13
# File 'lib/watir/adjacent.rb', line 11

def parent(opt = {})
  xpath_adjacent(opt.merge(adjacent: :ancestor, plural: false))
end

#preceding_sibling(opt = {}) ⇒ Object Also known as: previous_sibling

Returns preceding sibling element of current element.

Examples:

browser.text_field(name: "new_user_first_name").preceding_sibling(index: 1) == browser.legend#=> true

23
24
25
# File 'lib/watir/adjacent.rb', line 23

def preceding_sibling(opt = {})
  xpath_adjacent(opt.merge(adjacent: :preceding, plural: false))
end

#preceding_siblings(opt = {}) ⇒ Object Also known as: previous_siblings

Returns collection of preceding sibling elements of current element.

Examples:

browser.text_field(name: "new_user_first_name").preceding_siblings.size#=> 3

Raises:

  • (ArgumentError)

36
37
38
39
40
# 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

#siblings(opt = {}) ⇒ Object

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

Examples:

browser.text_field(name: "new_user_first_name").siblings.size#=> 59

80
81
82
# File 'lib/watir/adjacent.rb', line 80

def siblings(opt = {})
  parent.children(opt)
end