Class: Capybara::Session

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

Constant Summary collapse

DSL_METHODS =
[
  :all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script,
  :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?,
  :has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck,
  :visit, :wait_until, :within, :within_fieldset, :within_table, :has_link?, :has_no_link?, :has_button?, :has_no_button?,
  :has_field?, :has_no_field?, :has_checked_field?, :has_unchecked_field?, :has_no_table?, :has_table?, :unselect,
  :has_select?, :has_no_select?
]

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 = nil) ⇒ Session

Returns a new instance of Session.



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

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/capybara/session.rb', line 16

def app
  @app
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/capybara/session.rb', line 16

def mode
  @mode
end

Instance Method Details

#attach_file(locator, path) ⇒ Object



105
106
107
108
# File 'lib/capybara/session.rb', line 105

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

#bodyObject



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

def body
  driver.body
end

#check(locator) ⇒ Object



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

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

#choose(locator) ⇒ Object



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

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

#cleanup!Object



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

def cleanup!
  driver.cleanup!
end

#click(locator) ⇒ Object



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

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

#click_button(locator) ⇒ Object



64
65
66
67
# File 'lib/capybara/session.rb', line 64

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


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

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

#current_urlObject



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

def current_url
  driver.current_url
end

#drag(source_locator, target_locator) ⇒ Object



69
70
71
72
73
# File 'lib/capybara/session.rb', line 69

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

#driverObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capybara/session.rb', line 23

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



251
252
253
# File 'lib/capybara/session.rb', line 251

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

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



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

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, XPath.fillable_field(locator), msg).set(options[:with])
end

#has_button?(locator) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/capybara/session.rb', line 194

def has_button?(locator)
  has_xpath?(XPath.button(locator))
end

#has_checked_field?(locator) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/capybara/session.rb', line 210

def has_checked_field?(locator)
  has_xpath?(XPath.field(locator, :checked => true))
end

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#has_field?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/capybara/session.rb', line 202

def has_field?(locator, options={})
  has_xpath?(XPath.field(locator, options))
end

#has_link?(locator) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/capybara/session.rb', line 186

def has_link?(locator)
  has_xpath?(XPath.link(locator))
end

#has_no_button?(locator) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/capybara/session.rb', line 198

def has_no_button?(locator)
  has_no_xpath?(XPath.button(locator))
end

#has_no_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#has_no_field?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/capybara/session.rb', line 206

def has_no_field?(locator, options={})
  has_no_xpath?(XPath.field(locator, options))
end

#has_no_link?(locator) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/capybara/session.rb', line 190

def has_no_link?(locator)
  has_no_xpath?(XPath.link(locator))
end

#has_no_select?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/capybara/session.rb', line 222

def has_no_select?(locator, options={})
  has_no_xpath?(XPath.select(locator, options))
end

#has_no_table?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/capybara/session.rb', line 230

def has_no_table?(locator, options={})
  has_no_xpath?(XPath.table(locator, options))
end

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

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/capybara/session.rb', line 156

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

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

#has_select?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/capybara/session.rb', line 218

def has_select?(locator, options={})
  has_xpath?(XPath.select(locator, options))
end

#has_table?(locator, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/capybara/session.rb', line 226

def has_table?(locator, options={})
  has_xpath?(XPath.table(locator, options))
end

#has_unchecked_field?(locator) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/capybara/session.rb', line 214

def has_unchecked_field?(locator)
  has_xpath?(XPath.field(locator, :unchecked => true))
end

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

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/capybara/session.rb', line 142

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

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

#locate(kind_or_locator, locator = nil, fail_msg = nil) ⇒ Object

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



240
241
242
243
244
245
# File 'lib/capybara/session.rb', line 240

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

#response_headersObject



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

def response_headers
  driver.response_headers
end

#save_and_open_pageObject



234
235
236
237
# File 'lib/capybara/session.rb', line 234

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

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



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

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

#sourceObject



114
115
116
# File 'lib/capybara/session.rb', line 114

def source
  driver.source
end

#uncheck(locator) ⇒ Object



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

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

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



100
101
102
103
# File 'lib/capybara/session.rb', line 100

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

#visit(path) ⇒ Object



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

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

#wait_until(timeout = Capybara.default_wait_time) ⇒ Object



247
248
249
# File 'lib/capybara/session.rb', line 247

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

#within(kind, scope = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/capybara/session.rb', line 118

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

#within_fieldset(locator) ⇒ Object



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

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

#within_table(locator) ⇒ Object



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

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