Class: Pincers::Support::XPathBuilder
- Inherits:
-
Object
- Object
- Pincers::Support::XPathBuilder
- Defined in:
- lib/pincers/support/xpath_builder.rb
Instance Method Summary collapse
- #by_attribute(_attribute, _options = {}) ⇒ Object
- #by_class(_class, _options = {}) ⇒ Object
- #by_content(_contents, _options = {}) ⇒ Object
- #by_tag(_name) ⇒ Object
- #expression ⇒ Object
-
#initialize(_options = {}) ⇒ XPathBuilder
constructor
A new instance of XPathBuilder.
Constructor Details
#initialize(_options = {}) ⇒ XPathBuilder
Returns a new instance of XPathBuilder.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pincers/support/xpath_builder.rb', line 4 def initialize(={}) @parts = [] by_tag .delete(:tag) if .key? :tag by_content .delete(:content) if .key? :content if .key? :class .delete(:class).split(' ').each { |c| by_class c } end .each do |key, value| by_attribute key, contains: value end end |
Instance Method Details
#by_attribute(_attribute, _options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pincers/support/xpath_builder.rb', line 37 def by_attribute(_attribute, ={}) add(if .key? :equals "@#{_attribute}='#{_options.delete(:equals)}'" elsif .key? :starts_with "starts-with(@#{_attribute}, '#{_options.delete(:starts_with)}')" elsif .key? :ends_with ends_with = .delete(:ends_with) "substring(@#{_attribute}, string-length(@#{_attribute}) - string-length('#{ends_with}') + 1)='#{ends_with}'" elsif .key? :contains "contains(@#{_attribute}, '#{_options.delete(:contains)}')" else "#{_attribute}" end) end |
#by_class(_class, _options = {}) ⇒ Object
33 34 35 |
# File 'lib/pincers/support/xpath_builder.rb', line 33 def by_class(_class, ={}) add "contains(concat(' ', @class, ' '), ' #{_class} ')" end |
#by_content(_contents, _options = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/pincers/support/xpath_builder.rb', line 27 def by_content(_contents, ={}) # TODO: more options, lowercase support? # xpath("//*[text()='#{_contents}'] | //*[@value='#{_contents}']", _options) add "(contains(text(), '#{_contents}') or contains(@value, '#{_contents}'))" end |
#by_tag(_name) ⇒ Object
22 23 24 25 |
# File 'lib/pincers/support/xpath_builder.rb', line 22 def by_tag(_name) add "name()='#{_name.downcase}'" self end |
#expression ⇒ Object
18 19 20 |
# File 'lib/pincers/support/xpath_builder.rb', line 18 def expression ".//*[#{@parts.join(' and ')}]" end |