Class: Capybara::XPath
- Inherits:
-
Object
- Object
- Capybara::XPath
- 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
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Class Method Summary collapse
- .escape(string) ⇒ Object
- .method_missing(*args) ⇒ Object
- .respond_to?(method) ⇒ Boolean
- .wrap(path) ⇒ Object
Instance Method Summary collapse
- #append(path) ⇒ Object
- #button(locator) ⇒ Object
- #checkbox(locator, options = {}) ⇒ Object
- #content(locator) ⇒ Object
- #field(locator, options = {}) ⇒ Object
- #fieldset(locator) ⇒ Object
- #file_field(locator, options = {}) ⇒ Object
- #fillable_field(locator, options = {}) ⇒ Object
- #from_css(css) ⇒ Object (also: #for_css)
-
#initialize(*paths) ⇒ XPath
constructor
A new instance of XPath.
- #link(locator) ⇒ Object
- #prepend(path) ⇒ Object
- #radio_button(locator, options = {}) ⇒ Object
- #scope(scope) ⇒ Object
- #select(locator, options = {}) ⇒ Object
- #table(locator, options = {}) ⇒ Object
- #text_area(locator, options = {}) ⇒ Object
- #text_field(locator, options = {}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*paths) ⇒ XPath
Returns a new instance of XPath.
38 39 40 |
# File 'lib/capybara/xpath.rb', line 38 def initialize(*paths) @paths = paths end |
Instance Attribute Details
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
36 37 38 |
# File 'lib/capybara/xpath.rb', line 36 def paths @paths end |
Class Method Details
.escape(string) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/capybara/xpath.rb', line 8 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
31 32 33 |
# File 'lib/capybara/xpath.rb', line 31 def method_missing(*args) new.send(*args) end |
.respond_to?(method) ⇒ Boolean
27 28 29 |
# File 'lib/capybara/xpath.rb', line 27 def respond_to?(method) new.respond_to?(method) end |
.wrap(path) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/capybara/xpath.rb', line 19 def wrap(path) if path.is_a?(self) path else new(path.to_s) end end |
Instance Method Details
#append(path) ⇒ Object
50 51 52 |
# File 'lib/capybara/xpath.rb', line 50 def append(path) XPath.new(*[@paths, XPath.wrap(path).paths].flatten) end |
#button(locator) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/capybara/xpath.rb', line 104 def (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
126 127 128 |
# File 'lib/capybara/xpath.rb', line 126 def checkbox(locator, ={}) input_field(:checkbox, locator, ) end |
#content(locator) ⇒ Object
79 80 81 |
# File 'lib/capybara/xpath.rb', line 79 def content(locator) append("/descendant-or-self::*[contains(normalize-space(.),#{s(locator)})]") end |
#field(locator, options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/capybara/xpath.rb', line 63 def field(locator, ={}) if [:with] fillable_field(locator, ) else xpath = fillable_field(locator) xpath = xpath.input_field(:file, locator, ) xpath = xpath.checkbox(locator, ) xpath = xpath.(locator, ) xpath.select(locator, ) end end |
#fieldset(locator) ⇒ Object
95 96 97 |
# 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 = {}) ⇒ Object
134 135 136 |
# File 'lib/capybara/xpath.rb', line 134 def file_field(locator, ={}) input_field(:file, locator, ) end |
#fillable_field(locator, options = {}) ⇒ Object
75 76 77 |
# File 'lib/capybara/xpath.rb', line 75 def fillable_field(locator, ={}) text_area(locator, ).text_field(locator, ) end |
#from_css(css) ⇒ Object Also known as: for_css
58 59 60 |
# File 'lib/capybara/xpath.rb', line 58 def from_css(css) append(Nokogiri::CSS.xpath_for(css).first) end |
#link(locator) ⇒ Object
99 100 101 102 |
# File 'lib/capybara/xpath.rb', line 99 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
54 55 56 |
# File 'lib/capybara/xpath.rb', line 54 def prepend(path) XPath.new(*[XPath.wrap(path).paths, @paths].flatten) end |
#radio_button(locator, options = {}) ⇒ Object
130 131 132 |
# File 'lib/capybara/xpath.rb', line 130 def (locator, ={}) input_field(:radio, locator, ) end |
#scope(scope) ⇒ Object
42 43 44 |
# File 'lib/capybara/xpath.rb', line 42 def scope(scope) XPath.new(*paths.map { |p| scope + p }) end |
#select(locator, options = {}) ⇒ Object
122 123 124 |
# File 'lib/capybara/xpath.rb', line 122 def select(locator, ={}) add_field(locator, "//select", ) end |
#table(locator, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/capybara/xpath.rb', line 83 def table(locator, ={}) conditions = "" if [:rows] row_conditions = [: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
117 118 119 120 |
# File 'lib/capybara/xpath.rb', line 117 def text_area(locator, ={}) = .merge(:text => [:with]) if .has_key?(:with) add_field(locator, "//textarea", ) end |
#text_field(locator, options = {}) ⇒ Object
112 113 114 115 |
# File 'lib/capybara/xpath.rb', line 112 def text_field(locator, ={}) = .merge(:value => [:with]) if .has_key?(:with) add_field(locator, "//input[@type!='radio' and @type!='checkbox' and @type!='hidden']", ) end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/capybara/xpath.rb', line 46 def to_s @paths.join(' | ') end |