Parent

Capybara::XPath

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

Attributes

paths[R]

Public Class Methods

escape(string) click to toggle source
# File lib/capybara/xpath.rb, line 8
def escape(string)
  if string.include?("'")
    string = string.split("'", -1).map do |substr|
      "'#{substr}'"
    end.join(%{,"'",})
    "concat(#{string})"
  else
    "'#{string}'"
  end
end
method_missing(*args) click to toggle source
# File lib/capybara/xpath.rb, line 31
def method_missing(*args)
  new.send(*args)
end
new(*paths) click to toggle source
# File lib/capybara/xpath.rb, line 38
def initialize(*paths)
  @paths = paths
end
respond_to?(method) click to toggle source
# File lib/capybara/xpath.rb, line 27
def respond_to?(method)
  new.respond_to?(method)
end
wrap(path) click to toggle source
# File lib/capybara/xpath.rb, line 19
def wrap(path)
  if path.is_a?(self)
    path
  else
    new(path.to_s)
  end
end

Public Instance Methods

append(path) click to toggle source
# File lib/capybara/xpath.rb, line 50
def append(path)
  XPath.new(*[@paths, XPath.wrap(path).paths].flatten)
end
button(locator) click to toggle source
# File lib/capybara/xpath.rb, line 104
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={}) click to toggle source
# File lib/capybara/xpath.rb, line 126
def checkbox(locator, options={})
  input_field(:checkbox, locator, options)
end
content(locator) click to toggle source
# File lib/capybara/xpath.rb, line 79
def content(locator)
  append("/descendant-or-self::*[contains(normalize-space(.),#{s(locator)})]")
end
field(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 63
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) click to toggle source
# File lib/capybara/xpath.rb, line 95
def fieldset(locator)
  append("//fieldset[@id=#{s(locator)} or contains(legend,#{s(locator)})]")
end
file_field(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 134
def file_field(locator, options={})
  input_field(:file, locator, options)
end
fillable_field(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 75
def fillable_field(locator, options={})
  text_area(locator, options).text_field(locator, options)
end
for_css(css) click to toggle source
Alias for: from_css
from_css(css) click to toggle source
# File lib/capybara/xpath.rb, line 58
def from_css(css)
  append(Nokogiri::CSS.xpath_for(css).first)
end
Also aliased as: for_css
prepend(path) click to toggle source
# File lib/capybara/xpath.rb, line 54
def prepend(path)
  XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
end
radio_button(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 130
def radio_button(locator, options={})
  input_field(:radio, locator, options)
end
scope(scope) click to toggle source
# File lib/capybara/xpath.rb, line 42
def scope(scope)
  XPath.new(*paths.map { |p| scope + p })
end
select(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 122
def select(locator, options={})
  add_field(locator, "//select", options)
end
table(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 83
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={}) click to toggle source
# File lib/capybara/xpath.rb, line 117
def text_area(locator, options={})
  options = options.merge(:text => options[:with]) if options.has_key?(:with)
  add_field(locator, "//textarea", options)
end
text_field(locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 112
def text_field(locator, options={})
  options = options.merge(:value => options[:with]) if options.has_key?(:with)
  add_field(locator, "//input[not(@type) or (@type!='radio' and @type!='checkbox' and @type!='hidden')]", options)
end
to_s() click to toggle source
# File lib/capybara/xpath.rb, line 46
def to_s
  @paths.join(' | ')
end

Protected Instance Methods

add_field(locator, field, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 150
def add_field(locator, field, options={})
  postfix = extract_postfix(options)
  xpath = append("#{field}[@id=#{s(locator)}]#{postfix}")
  xpath = xpath.append("#{field}[@name=#{s(locator)}]#{postfix}")
  xpath = xpath.append("#{field}[@id=//label[contains(.,#{s(locator)})]/@for]#{postfix}")
  xpath = xpath.append("//label[contains(.,#{s(locator)})]#{field}#{postfix}")
  xpath.prepend("#{field}[@id=//label[text()=#{s(locator)}]/@for]#{postfix}")
end
extract_postfix(options) click to toggle source
# File lib/capybara/xpath.rb, line 159
def extract_postfix(options)
  options.inject("") do |postfix, (key, value)|
    case key
      when :value     then postfix += "[@value=#{s(value)}]"
      when :text      then postfix += "[text()=#{s(value)}]"
      when :checked   then postfix += "[@checked]"
      when :unchecked then postfix += "[not(@checked)]"
      when :options   then postfix += value.map { |o| "[.//option/text()=#{s(o)}]" }.join
      when :selected  then postfix += [value].flatten.map { |o| "[.//option[@selected]/text()=#{s(o)}]" }.join
    end
    postfix
  end
end
input_field(type, locator, options={}) click to toggle source
# File lib/capybara/xpath.rb, line 140
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
s(string) click to toggle source

Sanitize a String for putting it into an xpath query

# File lib/capybara/xpath.rb, line 174
def s(string)
  XPath.escape(string)
end
sibling() click to toggle source

place this between to nodes to indicate that they should be siblings

# File lib/capybara/xpath.rb, line 146
def sibling
  '/following-sibling::*[1]/self::'
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.