Module: XPath::HTML

Extended by:
HTML
Includes:
XPath
Included in:
HTML
Defined in:
lib/xpath/html.rb

Constant Summary

Constants included from XPath

VERSION

Instance Method Summary collapse

Methods included from XPath

#anywhere, #attr, #child, #contains, #css, #current, #descendant, generate, #name, #string, #text, #var, #varstring

Instance Method Details

#button(locator) ⇒ Object



15
16
17
18
19
# File 'lib/xpath/html.rb', line 15

def button(locator)
  button = descendant(:input)[attr(:type).one_of('submit', 'image', 'button')][attr(:id).equals(locator) | attr(:value).is(locator)]
  button += descendant(:button)[attr(:id).equals(locator) | attr(:value).is(locator) | string.n.is(locator)]
  button += descendant(:input)[attr(:type).equals('image')][attr(:alt).is(locator)]
end

#checkbox(locator, options = {}) ⇒ Object



62
63
64
# File 'lib/xpath/html.rb', line 62

def checkbox(locator, options={})
  xpath = locate_field(descendant(:input)[attr(:type).equals('checkbox')], locator)
end

#content(locator) ⇒ Object



11
12
13
# File 'lib/xpath/html.rb', line 11

def content(locator)
  child(:"descendant-or-self::*")[current.n.contains(locator)]
end

#field(locator, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xpath/html.rb', line 29

def field(locator, options={})
  if options[:with]
    fillable_field(locator, options)
  else
    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
  end
end

#fieldset(locator) ⇒ Object



25
26
27
# File 'lib/xpath/html.rb', line 25

def fieldset(locator)
  descendant(:fieldset)[attr(:id).equals(locator) | descendant(:legend)[text.is(locator)]]
end

#file_field(locator, options = {}) ⇒ Object



70
71
72
# File 'lib/xpath/html.rb', line 70

def file_field(locator, options={})
  locate_field(descendant(:input)[attr(:type).equals('file')], locator)
end

#fillable_field(locator, options = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/xpath/html.rb', line 41

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


6
7
8
9
# File 'lib/xpath/html.rb', line 6

def link(locator)
  link = descendant(:a)[attr(:href)]
  link[attr(:id).equals(locator) | string.n.is(locator) | attr(:title).is(locator) | descendant(:img)[attr(:alt).is(locator)]]
end


21
22
23
# File 'lib/xpath/html.rb', line 21

def link_or_button(locator)
  link(locator) + button(locator)
end

#option(name) ⇒ Object



74
75
76
# File 'lib/xpath/html.rb', line 74

def option(name)
  descendant(:option)[text.n.is(name)]
end

#radio_button(locator, options = {}) ⇒ Object



66
67
68
# File 'lib/xpath/html.rb', line 66

def radio_button(locator, options={})
  locate_field(descendant(:input)[attr(:type).equals('radio')], locator)
end

#select(locator, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xpath/html.rb', line 48

def select(locator, options={})
  xpath = locate_field(descendant(:select), locator)

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

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

  xpath
end

#table(locator, options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/xpath/html.rb', line 78

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) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/xpath/html.rb', line 92

def table_row(cells)
  cell_conditions = child(:td, :th)[text.equals(cells.first)]
  cells.drop(1).each do |cell|
    cell_conditions = cell_conditions.next_sibling(:td, :th)[text.equals(cell)]
  end
  cell_conditions
end

#table_rows(rows) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/xpath/html.rb', line 84

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