Class: Pincers::Support::XPathBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pincers/support/xpath_builder.rb

Instance Method Summary collapse

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(_options={})
  @parts = []
  by_tag _options.delete(:tag) if _options.key? :tag
  by_content _options.delete(:content) if _options.key? :content

  if _options.key? :class
    _options.delete(:class).split(' ').each { |c| by_class c }
  end

  _options.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, _options={})
  add(if _options.key? :equals
    "@#{_attribute}='#{_options.delete(:equals)}'"
  elsif _options.key? :starts_with
    "starts-with(@#{_attribute}, '#{_options.delete(:starts_with)}')"
  elsif _options.key? :ends_with
    ends_with = _options.delete(:ends_with)
    "substring(@#{_attribute}, string-length(@#{_attribute}) - string-length('#{ends_with}') + 1)='#{ends_with}'"
  elsif _options.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, _options={})
  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, _options={})
  # 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

#expressionObject



18
19
20
# File 'lib/pincers/support/xpath_builder.rb', line 18

def expression
  ".//*[#{@parts.join(' and ')}]"
end