Class: Capybara::Session
- Inherits:
-
Object
- Object
- Capybara::Session
- 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? ]
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #attach_file(locator, path) ⇒ Object
- #body ⇒ Object
- #check(locator) ⇒ Object
- #choose(locator) ⇒ Object
- #cleanup! ⇒ Object
- #click(locator) ⇒ Object
- #click_button(locator) ⇒ Object
- #click_link(locator) ⇒ Object
- #current_url ⇒ Object
- #drag(source_locator, target_locator) ⇒ Object
- #driver ⇒ Object
- #evaluate_script(script) ⇒ Object
- #fill_in(locator, options = {}) ⇒ Object
- #has_button?(locator) ⇒ Boolean
- #has_checked_field?(locator) ⇒ Boolean
- #has_content?(content) ⇒ Boolean
- #has_css?(path, options = {}) ⇒ Boolean
- #has_field?(locator, options = {}) ⇒ Boolean
- #has_link?(locator) ⇒ Boolean
- #has_no_button?(locator) ⇒ Boolean
- #has_no_content?(content) ⇒ Boolean
- #has_no_css?(path, options = {}) ⇒ Boolean
- #has_no_field?(locator, options = {}) ⇒ Boolean
- #has_no_link?(locator) ⇒ Boolean
- #has_no_select?(locator, options = {}) ⇒ Boolean
- #has_no_table?(locator, options = {}) ⇒ Boolean
- #has_no_xpath?(path, options = {}) ⇒ Boolean
- #has_select?(locator, options = {}) ⇒ Boolean
- #has_table?(locator, options = {}) ⇒ Boolean
- #has_unchecked_field?(locator) ⇒ Boolean
- #has_xpath?(path, options = {}) ⇒ Boolean
-
#initialize(mode, app) ⇒ Session
constructor
A new instance of Session.
-
#locate(kind_or_locator, locator = nil, fail_msg = nil) ⇒ Object
return node identified by locator or raise ElementNotFound(using desc).
- #response_headers ⇒ Object
- #save_and_open_page ⇒ Object
- #select(value, options = {}) ⇒ Object
- #source ⇒ Object
- #uncheck(locator) ⇒ Object
- #unselect(value, options = {}) ⇒ Object
- #visit(path) ⇒ Object
- #wait_until(timeout = Capybara.default_wait_time) ⇒ Object
- #within(kind, scope = nil) ⇒ Object
- #within_fieldset(locator) ⇒ Object
- #within_table(locator) ⇒ Object
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.
17 18 19 20 |
# File 'lib/capybara/session.rb', line 17 def initialize(mode, app) @mode = mode @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
15 16 17 |
# File 'lib/capybara/session.rb', line 15 def app @app end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
15 16 17 |
# File 'lib/capybara/session.rb', line 15 def mode @mode end |
Instance Method Details
#attach_file(locator, path) ⇒ Object
104 105 106 107 |
# File 'lib/capybara/session.rb', line 104 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 |
#body ⇒ Object
109 110 111 |
# File 'lib/capybara/session.rb', line 109 def body driver.body end |
#check(locator) ⇒ Object
84 85 86 87 |
# File 'lib/capybara/session.rb', line 84 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
79 80 81 82 |
# File 'lib/capybara/session.rb', line 79 def choose(locator) msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found" locate(:xpath, XPath.(locator), msg).set(true) end |
#cleanup! ⇒ Object
37 38 39 |
# File 'lib/capybara/session.rb', line 37 def cleanup! driver.cleanup! end |
#click(locator) ⇒ Object
53 54 55 56 |
# File 'lib/capybara/session.rb', line 53 def click(locator) msg = "no link or button '#{locator}' found" locate(:xpath, XPath.link(locator).(locator), msg).click end |
#click_button(locator) ⇒ Object
63 64 65 66 |
# File 'lib/capybara/session.rb', line 63 def (locator) msg = "no button with value or id or text '#{locator}' found" locate(:xpath, XPath.(locator), msg).click end |
#click_link(locator) ⇒ Object
58 59 60 61 |
# File 'lib/capybara/session.rb', line 58 def click_link(locator) msg = "no link with title, id or text '#{locator}' found" locate(:xpath, XPath.link(locator), msg).click end |
#current_url ⇒ Object
41 42 43 |
# File 'lib/capybara/session.rb', line 41 def current_url driver.current_url end |
#drag(source_locator, target_locator) ⇒ Object
68 69 70 71 72 |
# File 'lib/capybara/session.rb', line 68 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 |
#driver ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/capybara/session.rb', line 22 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
250 251 252 |
# File 'lib/capybara/session.rb', line 250 def evaluate_script(script) driver.evaluate_script(script) end |
#fill_in(locator, options = {}) ⇒ Object
74 75 76 77 |
# File 'lib/capybara/session.rb', line 74 def fill_in(locator, ={}) 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([:with]) end |
#has_button?(locator) ⇒ Boolean
193 194 195 |
# File 'lib/capybara/session.rb', line 193 def (locator) has_xpath?(XPath.(locator)) end |
#has_checked_field?(locator) ⇒ Boolean
209 210 211 |
# File 'lib/capybara/session.rb', line 209 def has_checked_field?(locator) has_xpath?(XPath.field(locator, :checked => true)) end |
#has_content?(content) ⇒ Boolean
177 178 179 |
# File 'lib/capybara/session.rb', line 177 def has_content?(content) has_xpath?(XPath.content(content)) end |
#has_css?(path, options = {}) ⇒ Boolean
169 170 171 |
# File 'lib/capybara/session.rb', line 169 def has_css?(path, ={}) has_xpath?(XPath.from_css(path), ) end |
#has_field?(locator, options = {}) ⇒ Boolean
201 202 203 |
# File 'lib/capybara/session.rb', line 201 def has_field?(locator, ={}) has_xpath?(XPath.field(locator, )) end |
#has_link?(locator) ⇒ Boolean
185 186 187 |
# File 'lib/capybara/session.rb', line 185 def has_link?(locator) has_xpath?(XPath.link(locator)) end |
#has_no_button?(locator) ⇒ Boolean
197 198 199 |
# File 'lib/capybara/session.rb', line 197 def (locator) has_no_xpath?(XPath.(locator)) end |
#has_no_content?(content) ⇒ Boolean
181 182 183 |
# File 'lib/capybara/session.rb', line 181 def has_no_content?(content) has_no_xpath?(XPath.content(content)) end |
#has_no_css?(path, options = {}) ⇒ Boolean
173 174 175 |
# File 'lib/capybara/session.rb', line 173 def has_no_css?(path, ={}) has_no_xpath?(XPath.from_css(path), ) end |
#has_no_field?(locator, options = {}) ⇒ Boolean
205 206 207 |
# File 'lib/capybara/session.rb', line 205 def has_no_field?(locator, ={}) has_no_xpath?(XPath.field(locator, )) end |
#has_no_link?(locator) ⇒ Boolean
189 190 191 |
# File 'lib/capybara/session.rb', line 189 def has_no_link?(locator) has_no_xpath?(XPath.link(locator)) end |
#has_no_select?(locator, options = {}) ⇒ Boolean
221 222 223 |
# File 'lib/capybara/session.rb', line 221 def has_no_select?(locator, ={}) has_no_xpath?(XPath.select(locator, )) end |
#has_no_table?(locator, options = {}) ⇒ Boolean
229 230 231 |
# File 'lib/capybara/session.rb', line 229 def has_no_table?(locator, ={}) has_no_xpath?(XPath.table(locator, )) end |
#has_no_xpath?(path, options = {}) ⇒ Boolean
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/capybara/session.rb', line 155 def has_no_xpath?(path, ={}) wait_conditionally_until do results = all(:xpath, path, ) if [:count] results.size != [:count] else results.empty? end end rescue Capybara::TimeoutError return false end |
#has_select?(locator, options = {}) ⇒ Boolean
217 218 219 |
# File 'lib/capybara/session.rb', line 217 def has_select?(locator, ={}) has_xpath?(XPath.select(locator, )) end |
#has_table?(locator, options = {}) ⇒ Boolean
225 226 227 |
# File 'lib/capybara/session.rb', line 225 def has_table?(locator, ={}) has_xpath?(XPath.table(locator, )) end |
#has_unchecked_field?(locator) ⇒ Boolean
213 214 215 |
# File 'lib/capybara/session.rb', line 213 def has_unchecked_field?(locator) has_xpath?(XPath.field(locator, :unchecked => true)) end |
#has_xpath?(path, options = {}) ⇒ Boolean
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/capybara/session.rb', line 141 def has_xpath?(path, ={}) wait_conditionally_until do results = all(:xpath, path, ) if [:count] results.size == [: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)
239 240 241 242 243 244 |
# File 'lib/capybara/session.rb', line 239 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_headers ⇒ Object
45 46 47 |
# File 'lib/capybara/session.rb', line 45 def response_headers driver.response_headers end |
#save_and_open_page ⇒ Object
233 234 235 236 |
# File 'lib/capybara/session.rb', line 233 def save_and_open_page require 'capybara/save_and_open_page' Capybara::SaveAndOpenPage.save_and_open_page(body) end |
#select(value, options = {}) ⇒ Object
94 95 96 97 |
# File 'lib/capybara/session.rb', line 94 def select(value, ={}) msg = "cannot select option, no select box with id, name, or label '#{[:from]}' found" locate(:xpath, XPath.select([:from]), msg).select(value) end |
#source ⇒ Object
113 114 115 |
# File 'lib/capybara/session.rb', line 113 def source driver.source end |
#uncheck(locator) ⇒ Object
89 90 91 92 |
# File 'lib/capybara/session.rb', line 89 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
99 100 101 102 |
# File 'lib/capybara/session.rb', line 99 def unselect(value, ={}) msg = "cannot unselect option, no select box with id, name, or label '#{[:from]}' found" locate(:xpath, XPath.select([:from]), msg).unselect(value) end |
#visit(path) ⇒ Object
49 50 51 |
# File 'lib/capybara/session.rb', line 49 def visit(path) driver.visit(path) end |
#wait_until(timeout = Capybara.default_wait_time) ⇒ Object
246 247 248 |
# File 'lib/capybara/session.rb', line 246 def wait_until(timeout = Capybara.default_wait_time) WaitUntil.timeout(timeout) { yield } end |
#within(kind, scope = nil) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/capybara/session.rb', line 117 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 |