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.



26
27
28
# File 'lib/capybara/xpath.rb', line 26

def initialize(*paths)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



24
25
26
# File 'lib/capybara/xpath.rb', line 24

def paths
  @paths
end

Class Method Details

.method_missing(*args) ⇒ Object



19
20
21
# File 'lib/capybara/xpath.rb', line 19

def method_missing(*args)
  new.send(*args)
end

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/capybara/xpath.rb', line 15

def respond_to?(method)
  new.respond_to?(method)
end

.wrap(path) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/capybara/xpath.rb', line 7

def wrap(path)
  if path.is_a?(self)
    path
  else
    new(path.to_s)
  end
end

Instance Method Details

#append(path) ⇒ Object



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

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

#button(locator) ⇒ Object



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

def button(locator)
  xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or @value=#{s(locator)}]")
  xpath.append("//button[@id=#{s(locator)} or @value=#{s(locator)} or contains(.,#{s(locator)})]")
end

#checkbox(locator) ⇒ Object



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

def checkbox(locator)
  add_field(locator) { |id| "//input[@type='checkbox'][@id=#{id}]" }
end

#content(locator) ⇒ Object



38
39
40
# File 'lib/capybara/xpath.rb', line 38

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

#field(locator) ⇒ Object



30
31
32
# File 'lib/capybara/xpath.rb', line 30

def field(locator)
  fillable_field(locator).file_field(locator).checkbox(locator).radio_button(locator).select(locator)
end

#fieldset(locator) ⇒ Object



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

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

#file_field(locator) ⇒ Object



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

def file_field(locator)
  add_field(locator) { |id| "//input[@type='file'][@id=#{id}]" }
end

#fillable_field(locator) ⇒ Object



34
35
36
# File 'lib/capybara/xpath.rb', line 34

def fillable_field(locator)
  text_field(locator).password_field(locator).text_area(locator)
end


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

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

#password_field(locator) ⇒ Object



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

def password_field(locator)
  add_field(locator) { |id| "//input[@type='password'][@id=#{id}]" }
end

#prepend(path) ⇒ Object



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

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

#radio_button(locator) ⇒ Object



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

def radio_button(locator)
  add_field(locator) { |id| "//input[@type='radio'][@id=#{id}]" }
end

#scope(scope) ⇒ Object



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

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

#select(locator) ⇒ Object



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

def select(locator)
  add_field(locator) { |id| "//select[@id=#{id}]" }
end

#table(locator) ⇒ Object



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

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

#text_area(locator) ⇒ Object



67
68
69
# File 'lib/capybara/xpath.rb', line 67

def text_area(locator)
  add_field(locator) { |id| "//textarea[@id=#{id}]" }
end

#text_field(locator) ⇒ Object



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

def text_field(locator)
  add_field(locator) { |id| "//input[@type='text'][@id=#{id}]" }
end

#to_sObject



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

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