Class: Capybara::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Searchable

#all, #find, #find_button, #find_by_id, #find_field, #find_link

Constructor Details

#initialize(mode, app) ⇒ Session

Returns a new instance of Session.



9
10
11
12
# File 'lib/capybara/session.rb', line 9

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

Instance Method Details

#attach_file(locator, path) ⇒ Object



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

def attach_file(locator, path)
  msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
  locate(XPath.file_field(locator), msg).set(path)
end

#bodyObject



92
93
94
# File 'lib/capybara/session.rb', line 92

def body
  driver.body
end

#check(locator) ⇒ Object



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

def check(locator)
  msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
  locate(XPath.checkbox(locator), msg).set(true)
end

#choose(locator) ⇒ Object



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

def choose(locator)
  msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
  locate(XPath.radio_button(locator), msg).set(true)
end

#click(locator) ⇒ Object



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

def click(locator)
  msg = "no link or button '#{locator}' found"
  locate(XPath.link(locator).button(locator), msg).click
end

#click_button(locator) ⇒ Object



51
52
53
54
# File 'lib/capybara/session.rb', line 51

def click_button(locator)
  msg = "no button with value or id or text '#{locator}' found"
  locate(XPath.button(locator)).click
end


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

def click_link(locator)
  msg = "no link with title, id or text '#{locator}' found"
  locate(XPath.link(locator), msg).click
end

#current_urlObject



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

def current_url
  driver.current_url
end

#drag(source_locator, target_locator) ⇒ Object



56
57
58
59
60
# File 'lib/capybara/session.rb', line 56

def drag(source_locator, target_locator)
  source = locate(source_locator, "drag source '#{source_locator}' not found on page")
  target = locate(target_locator, "drag target '#{target_locator}' not found on page")
  source.drag_to(target)
end

#driverObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capybara/session.rb', line 14

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

#evaluate_script(script) ⇒ Object



182
183
184
# File 'lib/capybara/session.rb', line 182

def evaluate_script(script)
  driver.evaluate_script(script)
end

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



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

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

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/capybara/session.rb', line 157

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

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

Returns:

  • (Boolean)


149
150
151
# File 'lib/capybara/session.rb', line 149

def has_css?(path, options={})
  has_xpath?(XPath.from_css(path), options)
end

#has_no_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_no_content?(content)
  has_no_xpath?(XPath.content(content))
end

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

Returns:

  • (Boolean)


153
154
155
# File 'lib/capybara/session.rb', line 153

def has_no_css?(path, options={})
  has_no_xpath?(XPath.from_css(path), options)
end

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

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/capybara/session.rb', line 135

def has_no_xpath?(path, options={})
  wait_conditionally_until do
    results = all(path, options)

    if options[:count]
      results.size != options[:count]
    else
      results.empty?
    end
  end
rescue Capybara::TimeoutError
  return false
end

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

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capybara/session.rb', line 121

def has_xpath?(path, options={})
  wait_conditionally_until do
    results = all(path, options)

    if options[:count]
      results.size == options[:count]
    else
      results.size > 0
    end
  end
rescue Capybara::TimeoutError
  return false
end

#locate(locator, fail_msg = nil) ⇒ Object

return node identified by locator or raise ElementNotFound(using desc)



171
172
173
174
175
176
# File 'lib/capybara/session.rb', line 171

def locate(locator, fail_msg = nil)
  node = wait_conditionally_until { find(locator) }
ensure
  raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{locator}'" unless node
  return node
end

#response_headersObject



33
34
35
# File 'lib/capybara/session.rb', line 33

def response_headers
  driver.response_headers
end

#save_and_open_pageObject



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

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

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



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

def select(value, options={})
  msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
  locate(XPath.select(options[:from]), msg).select(value)
end

#sourceObject



96
97
98
# File 'lib/capybara/session.rb', line 96

def source
  driver.source
end

#uncheck(locator) ⇒ Object



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

def uncheck(locator)
  msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
  locate(XPath.checkbox(locator), msg).set(false)
end

#visit(path) ⇒ Object



37
38
39
# File 'lib/capybara/session.rb', line 37

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

#wait_until(timeout = Capybara.default_wait_time) ⇒ Object



178
179
180
# File 'lib/capybara/session.rb', line 178

def wait_until(timeout = Capybara.default_wait_time)
  WaitUntil.timeout(timeout) { yield }
end

#within(kind, scope = nil) ⇒ Object



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

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

#within_fieldset(locator) ⇒ Object



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

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

#within_table(locator) ⇒ Object



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

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