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
- .from_css(css) ⇒ Object (also: for_css)
- .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
- #fillable_field(locator, options = {}) ⇒ Object
-
#initialize(*paths) ⇒ XPath
constructor
A new instance of XPath.
- #input_field(type, locator, options = {}) ⇒ Object
- #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
- #to_s ⇒ Object
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
#paths ⇒ Object (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
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 (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, ={}) input_field(:checkbox, locator, ) 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, ={}) 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
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, ={}) [:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator, )) do |all, type| all.input_field(type, locator, ) end end |
#input_field(type, locator, options = {}) ⇒ Object
96 97 98 99 |
# File 'lib/capybara/xpath.rb', line 96 def input_field(type, locator, ={}) = .merge(:value => [:with]) if .has_key?(:with) add_field(locator, "//input[@type='#{type}']", ) end |
#link(locator) ⇒ Object
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 (locator, ={}) input_field(:radio, locator, ) 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, ={}) add_field(locator, "//select", ) 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, ={}) 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
87 88 89 90 |
# File 'lib/capybara/xpath.rb', line 87 def text_area(locator, ={}) = .merge(:text => [:with]) if .has_key?(:with) add_field(locator, "//textarea", ) end |
#to_s ⇒ Object
105 106 107 |
# File 'lib/capybara/xpath.rb', line 105 def to_s @paths.join(' | ') end |