module XPath::HTML

Public Instance Methods

button(locator) click to toggle source
# File lib/xpath/html.rb, line 16
def button(locator)
  button = descendant(:input)[attr(:type).one_of('submit', 'image', 'button')][attr(:id).equals(locator) | attr(:value).is(locator) | attr(:title).is(locator)]
  button += descendant(:button)[attr(:id).equals(locator) | attr(:value).is(locator) | string.n.is(locator) | attr(:title).is(locator)]
  button += descendant(:input)[attr(:type).equals('image')][attr(:alt).is(locator)]
end
checkbox(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 60
def checkbox(locator, options={})
  xpath = locate_field(descendant(:input)[attr(:type).equals('checkbox')], locator)
end
content(locator) click to toggle source
# File lib/xpath/html.rb, line 12
def content(locator)
  child(:"descendant-or-self::*")[current.n.contains(locator)]
end
field(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 30
def field(locator, options={})
  xpath = descendant(:input, :textarea, :select)[~attr(:type).one_of('submit', 'image', 'hidden')]
  xpath = locate_field(xpath, locator)
  xpath = xpath[attr(:checked)] if options[:checked]
  xpath = xpath[~attr(:checked)] if options[:unchecked]
  xpath = xpath[field_value(options[:with])] if options.has_key?(:with)
  xpath
end
fieldset(locator) click to toggle source
# File lib/xpath/html.rb, line 26
def fieldset(locator)
  descendant(:fieldset)[attr(:id).equals(locator) | descendant(:legend)[string.n.is(locator)]]
end
file_field(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 68
def file_field(locator, options={})
  locate_field(descendant(:input)[attr(:type).equals('file')], locator)
end
fillable_field(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 39
def fillable_field(locator, options={})
  xpath = descendant(:input, :textarea)[~attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
  xpath = locate_field(xpath, locator)
  xpath = xpath[field_value(options[:with])] if options.has_key?(:with)
  xpath
end
optgroup(name) click to toggle source
# File lib/xpath/html.rb, line 72
def optgroup(name)
  descendant(:optgroup)[attr(:label).is(name)]
end
option(name) click to toggle source
# File lib/xpath/html.rb, line 76
def option(name)
  descendant(:option)[string.n.is(name)]
end
radio_button(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 64
def radio_button(locator, options={})
  locate_field(descendant(:input)[attr(:type).equals('radio')], locator)
end
select(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 46
def select(locator, options={})
  xpath = locate_field(descendant(:select), locator)

  options[:options].each do |option|
    xpath = xpath[descendant(:option).equals(option)]
  end if options[:options]

  [options[:selected]].flatten.each do |option|
    xpath = xpath[descendant(:option)[attr(:selected)].equals(option)]
  end if options[:selected]

  xpath
end
table(locator, options={}) click to toggle source
# File lib/xpath/html.rb, line 80
def table(locator, options={})
  xpath = descendant(:table)[attr(:id).equals(locator) | descendant(:caption).contains(locator)]
  xpath = xpath[table_rows(options[:rows])] if options[:rows]
  xpath
end
table_row(cells) click to toggle source
# File lib/xpath/html.rb, line 94
def table_row(cells)
  cell_conditions = child(:td, :th)[string.n.equals(cells.first)]
  cells.drop(1).each do |cell|
    cell_conditions = cell_conditions.next_sibling(:td, :th)[string.n.equals(cell)]
  end
  cell_conditions
end
table_rows(rows) click to toggle source
# File lib/xpath/html.rb, line 86
def table_rows(rows)
  row_conditions = descendant(:tr)[table_row(rows.first)]
  rows.drop(1).each do |row|
    row_conditions = row_conditions.next_sibling(:tr)[table_row(row)]
  end
  row_conditions
end

Protected Instance Methods

field_value(value) click to toggle source
# File lib/xpath/html.rb, line 109
def field_value(value)
  (string.n.is(value) & tag(:textarea)) | (attr(:value).equals(value) & ~tag(:textarea))
end
locate_field(xpath, locator) click to toggle source
# File lib/xpath/html.rb, line 104
def locate_field(xpath, locator)
  locate_field = xpath[attr(:id).equals(locator) | attr(:name).equals(locator) | attr(:id).equals(anywhere(:label)[string.n.is(locator)].attr(:for))]
  locate_field += descendant(:label)[string.n.is(locator)].descendant(xpath)
end