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.



44
45
46
# File 'lib/capybara/xpath.rb', line 44

def initialize(*paths)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



42
43
44
# File 'lib/capybara/xpath.rb', line 42

def paths
  @paths
end

Class Method Details

.escape(string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/capybara/xpath.rb', line 14

def escape(string)
  if string.include?("'")
    string = string.split("'", -1).map do |substr|
      "'#{substr}'"
    end.join(%q{,"'",})
    "concat(#{string})"
  else
    "'#{string}'"
  end
end

.method_missing(*args) ⇒ Object



37
38
39
# File 'lib/capybara/xpath.rb', line 37

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

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.wrap(path) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/capybara/xpath.rb', line 25

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

Instance Method Details

#append(path) ⇒ Object



52
53
54
# File 'lib/capybara/xpath.rb', line 52

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

#button(locator) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/capybara/xpath.rb', line 106

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



128
129
130
# File 'lib/capybara/xpath.rb', line 128

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

#content(locator) ⇒ Object



81
82
83
# File 'lib/capybara/xpath.rb', line 81

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

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/capybara/xpath.rb', line 65

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



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

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

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



136
137
138
# File 'lib/capybara/xpath.rb', line 136

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

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



77
78
79
# File 'lib/capybara/xpath.rb', line 77

def fillable_field(locator, options={})
  text_area(locator, options).text_field(locator, options)
end

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



60
61
62
# File 'lib/capybara/xpath.rb', line 60

def from_css(css)
  XPath.new(*[@paths, Nokogiri::CSS.xpath_for(css).map { |selector| '.' + selector }].flatten)
end


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

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



56
57
58
# File 'lib/capybara/xpath.rb', line 56

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

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



132
133
134
# File 'lib/capybara/xpath.rb', line 132

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

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



124
125
126
# File 'lib/capybara/xpath.rb', line 124

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

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/capybara/xpath.rb', line 85

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



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

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 = {}) ⇒ Object



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

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_sObject



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

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