Class: Capybara::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, app) ⇒ Session

Returns a new instance of Session.



6
7
8
9
# File 'lib/capybara/session.rb', line 6

def initialize(mode, app)
  @mode = mode
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/capybara/session.rb', line 4

def app
  @app
end

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/capybara/session.rb', line 4

def mode
  @mode
end

Instance Method Details

#all(locator) ⇒ Object



140
141
142
143
144
# File 'lib/capybara/session.rb', line 140

def all(locator)
  XPath.wrap(locator).scope(current_scope).paths.map do |path|
    driver.find(path)
  end.flatten
end

#attach_file(locator, path) ⇒ Object



84
85
86
87
88
# File 'lib/capybara/session.rb', line 84

def attach_file(locator, path)
  field = wait_for(XPath.file_field(locator))
  raise Capybara::ElementNotFound, "cannot attach file, no file field with id or label '#{locator}' found" unless field
  field.set(path)
end

#bodyObject



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

def body
  driver.body
end

#check(locator) ⇒ Object



66
67
68
69
70
# File 'lib/capybara/session.rb', line 66

def check(locator)
  field = wait_for(XPath.checkbox(locator))
  raise Capybara::ElementNotFound, "cannot check field, no checkbox with id or label '#{locator}' found" unless field
  field.set(true)
end

#choose(locator) ⇒ Object



60
61
62
63
64
# File 'lib/capybara/session.rb', line 60

def choose(locator)
  field = wait_for(XPath.radio_button(locator))
  raise Capybara::ElementNotFound, "cannot choose field, no radio button with id or label '#{locator}' found" unless field
  field.set(true)
end

#click(locator) ⇒ Object



28
29
30
31
32
# File 'lib/capybara/session.rb', line 28

def click(locator)
  link = wait_for(XPath.link(locator).button(locator))
  raise Capybara::ElementNotFound, "no link or button '#{locator}' found" unless link
  link.click
end

#click_button(locator) ⇒ Object



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

def click_button(locator)
  button = wait_for(XPath.button(locator))
  raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless button
  button.click
end


34
35
36
37
38
# File 'lib/capybara/session.rb', line 34

def click_link(locator)
  link = wait_for(XPath.link(locator))
  raise Capybara::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link
  link.click
end

#drag(source_locator, target_locator) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/capybara/session.rb', line 46

def drag(source_locator, target_locator)
  source = find(source_locator)
  raise Capybara::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source
  target = find(target_locator)
  raise Capybara::ElementNotFound, "drag target '#{target_locator}' not found on page" unless target
  source.drag_to(target)
end

#driverObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/capybara/session.rb', line 11

def driver
  @driver ||= case mode
  when :rack_test
    Capybara::Driver::RackTest.new(app)
  when :culerity
    Capybara::Driver::Culerity.new(app)
  when :selenium
    Capybara::Driver::Selenium.new(app)
  else
    raise Capybara::DriverNotFoundError, "no driver called #{mode} was found"
  end
end

#evaluate_script(script) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/capybara/session.rb', line 173

def evaluate_script(script)
  begin
    driver.evaluate_script(script)
  rescue NoMethodError
    raise NotSupportedByDriverError
  end
end

#fill_in(locator, options = {}) ⇒ Object



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

def fill_in(locator, options={})
  field = wait_for(XPath.fillable_field(locator))
  raise Capybara::ElementNotFound, "cannot fill in, no text field, text area or password field with id or label '#{locator}' found" unless field
  field.set(options[:with])
end

#find(locator) ⇒ Object



146
147
148
# File 'lib/capybara/session.rb', line 146

def find(locator)
  all(locator).first
end

#find_button(locator) ⇒ Object



169
170
171
# File 'lib/capybara/session.rb', line 169

def find_button(locator)
  find(XPath.button(locator))
end

#find_field(locator) ⇒ Object Also known as: field_labeled



160
161
162
# File 'lib/capybara/session.rb', line 160

def find_field(locator)
  find(XPath.field(locator))
end


165
166
167
# File 'lib/capybara/session.rb', line 165

def find_link(locator)
  find(XPath.link(locator))
end

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_content?(content)
  has_xpath?(XPath.content(content).to_s)
end

#has_css?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/capybara/session.rb', line 110

def has_css?(path, options={})
  has_xpath?(css_to_xpath(path), options)
end

#has_xpath?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/capybara/session.rb', line 98

def has_xpath?(path, options={})
  results = all(path)
  if options[:text]
    results = filter_by_text(results, options[:text])
  end
  if options[:count]
    results.size == options[:count]
  else
    results.size > 0
  end
end

#save_and_open_pageObject



135
136
137
138
# File 'lib/capybara/session.rb', line 135

def save_and_open_page
  require 'capybara/save_and_open_page'
  Capybara::SaveAndOpenPage.save_and_open_page(body)
end

#select(value, options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/capybara/session.rb', line 78

def select(value, options={})
  field = wait_for(XPath.select(options[:from]))
  raise Capybara::ElementNotFound, "cannot select option, no select box with id or label '#{options[:from]}' found" unless field
  field.select(value)
end

#uncheck(locator) ⇒ Object



72
73
74
75
76
# File 'lib/capybara/session.rb', line 72

def uncheck(locator)
  field = wait_for(XPath.checkbox(locator))
  raise Capybara::ElementNotFound, "cannot uncheck field, no checkbox with id or label '#{locator}' found" unless field
  field.set(false)
end

#visit(path) ⇒ Object



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

def visit(path)
  driver.visit(path)
end

#wait_for(locator) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/capybara/session.rb', line 150

def wait_for(locator)
  return find(locator) unless driver.wait?
  8.times do
    result = find(locator)
    return result if result
    sleep(0.1)
  end
  nil
end

#within(kind, scope = nil) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/capybara/session.rb', line 114

def within(kind, scope=nil)
  kind, scope = Capybara.default_selector, kind unless scope
  scope = css_to_xpath(scope) if kind == :css
  raise Capybara::ElementNotFound, "scope '#{scope}' not found on page" unless wait_for(scope)
  scopes.push(scope)
  yield
  scopes.pop
end

#within_fieldset(locator) ⇒ Object



123
124
125
126
127
# File 'lib/capybara/session.rb', line 123

def within_fieldset(locator)
  within XPath.fieldset(locator) do
    yield
  end
end

#within_table(locator) ⇒ Object



129
130
131
132
133
# File 'lib/capybara/session.rb', line 129

def within_table(locator)
  within XPath.table(locator) do
    yield
  end
end