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.



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

def initialize(*paths)
  @paths = paths
end

Instance Attribute Details

#pathsObject (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

Returns:

  • (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



90
91
92
# File 'lib/capybara/xpath.rb', line 90

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

#button(locator) ⇒ Object



63
64
65
66
67
68
# File 'lib/capybara/xpath.rb', line 63

def button(locator)
  xpath = append("//input[@type='submit' or @type='image'][@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'][@value=#{s(locator)}]")
  xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end

#checkbox(locator) ⇒ Object



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

def checkbox(locator)
  input_field(:checkbox, locator)
end

#content(locator) ⇒ Object



46
47
48
# File 'lib/capybara/xpath.rb', line 46

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

#field(locator) ⇒ Object



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

def field(locator)
  fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator)
end

#fieldset(locator) ⇒ Object



54
55
56
# File 'lib/capybara/xpath.rb', line 54

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

#fillable_field(locator) ⇒ Object



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

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) ⇒ Object



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

def input_field(type, locator)
  add_field(locator, "//input[@type='#{type}']")
end


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

def link(locator)
  xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]")
  xpath.prepend("//a[@href][text()=#{s(locator)} or @title=#{s(locator)}]")
end

#prepend(path) ⇒ Object



94
95
96
# File 'lib/capybara/xpath.rb', line 94

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

#radio_button(locator) ⇒ Object



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

def radio_button(locator)
  input_field(:radio, locator)
end

#scope(scope) ⇒ Object



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

def scope(scope)
  XPath.new(*paths.map { |p| scope + p })
end

#select(locator) ⇒ Object



74
75
76
# File 'lib/capybara/xpath.rb', line 74

def select(locator)
  add_field(locator, "//select")
end

#table(locator) ⇒ Object



50
51
52
# File 'lib/capybara/xpath.rb', line 50

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

#text_area(locator) ⇒ Object



70
71
72
# File 'lib/capybara/xpath.rb', line 70

def text_area(locator)
  add_field(locator, "//textarea")
end

#to_sObject



86
87
88
# File 'lib/capybara/xpath.rb', line 86

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