Class: Capybara::XPath

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/xpath.rb

Overview

this is a class for generating XPath queries, use it like this:

Xpath.text_field('foo').link('blah').to_s

this will generate an XPath that matches either a text field or a link

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ XPath

Returns a new instance of XPath.



32
33
34
# File 'lib/capybara/xpath.rb', line 32

def initialize(*paths)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



30
31
32
# File 'lib/capybara/xpath.rb', line 30

def paths
  @paths
end

Class Method Details

.from_css(css) ⇒ Object Also known as: for_css



8
9
10
# File 'lib/capybara/xpath.rb', line 8

def from_css(css)
  Nokogiri::CSS.xpath_for(css).first
end

.method_missing(*args) ⇒ Object



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

def method_missing(*args)
  new.send(*args)
end

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method)
  new.respond_to?(method)
end

.wrap(path) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/capybara/xpath.rb', line 13

def wrap(path)
  if path.is_a?(self)
    path
  else
    new(path.to_s)
  end
end

Instance Method Details

#append(path) ⇒ Object



109
110
111
# File 'lib/capybara/xpath.rb', line 109

def append(path)
  XPath.new(*[@paths, XPath.wrap(path).paths].flatten)
end

#button(locator) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/capybara/xpath.rb', line 79

def button(locator)
  xpath = append("//input[@type='submit' or @type='image' or @type='button'][@id=#{s(locator)} or contains(@value,#{s(locator)})]")
  xpath = xpath.append("//button[@id=#{s(locator)} or contains(@value,#{s(locator)}) or contains(.,#{s(locator)})]")
  xpath = xpath.prepend("//input[@type='submit' or @type='image' or @type='button'][@value=#{s(locator)}]")
  xpath = xpath.prepend("//input[@type='image'][@alt=#{s(locator)} or contains(@alt,#{s(locator)})]")
  xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end

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



117
118
119
# File 'lib/capybara/xpath.rb', line 117

def checkbox(locator, options={})
  input_field(:checkbox, locator, options)
end

#content(locator) ⇒ Object



54
55
56
# File 'lib/capybara/xpath.rb', line 54

def content(locator)
  append("/descendant-or-self::*[contains(.,#{s(locator)})]")
end

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/capybara/xpath.rb', line 36

def field(locator, options={})
  if options[:with]
    fillable_field(locator, options)
  else
    xpath = fillable_field(locator)
    xpath = xpath.input_field(:file, locator, options)
    xpath = xpath.checkbox(locator, options)
    xpath = xpath.radio_button(locator, options)
    xpath.select(locator, options)
  end
end

#fieldset(locator) ⇒ Object



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

def fieldset(locator)
  append("//fieldset[@id=#{s(locator)} or contains(legend,#{s(locator)})]")
end

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



48
49
50
51
52
# File 'lib/capybara/xpath.rb', line 48

def fillable_field(locator, options={})
  [:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator, options)) do |all, type|
    all.input_field(type, locator, options)
  end
end

#input_field(type, locator, options = {}) ⇒ Object



96
97
98
99
# File 'lib/capybara/xpath.rb', line 96

def input_field(type, locator, options={})
  options = options.merge(:value => options[:with]) if options.has_key?(:with)
  add_field(locator, "//input[@type='#{type}']", options)
end


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

def link(locator)
  xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)}) or img[contains(@alt,#{s(locator)})]]")
  xpath.prepend("//a[@href][text()=#{s(locator)} or @title=#{s(locator)} or img[@alt=#{s(locator)}]]")
end

#prepend(path) ⇒ Object



113
114
115
# File 'lib/capybara/xpath.rb', line 113

def prepend(path)
  XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
end

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



121
122
123
# File 'lib/capybara/xpath.rb', line 121

def radio_button(locator, options={})
  input_field(:radio, locator, options)
end

#scope(scope) ⇒ Object



101
102
103
# File 'lib/capybara/xpath.rb', line 101

def scope(scope)
  XPath.new(*paths.map { |p| scope + p })
end

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



92
93
94
# File 'lib/capybara/xpath.rb', line 92

def select(locator, options={})
  add_field(locator, "//select", options)
end

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/capybara/xpath.rb', line 58

def table(locator, options={})
  conditions = ""
  if options[:rows]
    row_conditions = options[:rows].map do |row|
      row = row.map { |column| "*[self::td or self::th][text()=#{s(column)}]" }.join(sibling)
      "tr[./#{row}]"
    end.join(sibling)
    conditions << "[.//#{row_conditions}]"
  end
  append("//table[@id=#{s(locator)} or contains(caption,#{s(locator)})]#{conditions}")
end

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



87
88
89
90
# File 'lib/capybara/xpath.rb', line 87

def text_area(locator, options={})
  options = options.merge(:text => options[:with]) if options.has_key?(:with)
  add_field(locator, "//textarea", options)
end

#to_sObject



105
106
107
# File 'lib/capybara/xpath.rb', line 105

def to_s
  @paths.join(' | ')
end