Class: Capybara::Session
- Inherits:
-
Object
- Object
- Capybara::Session
- Extended by:
- Forwardable
- 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, :within_frame, :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?, :current_path, :scope_to ]
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
- #check(locator) ⇒ Object
- #choose(locator) ⇒ Object
- #click(locator) ⇒ Object
- #click_button(locator) ⇒ Object
- #click_link(locator) ⇒ Object
- #drag(source_locator, target_locator) ⇒ Object
- #driver ⇒ Object
- #evaluate_script(script) ⇒ Object
- #execute_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 = nil) ⇒ 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).
- #save_and_open_page ⇒ Object
- #scope_to(*locator) ⇒ Object
- #select(value, options = {}) ⇒ Object
- #uncheck(locator) ⇒ Object
- #unselect(value, options = {}) ⇒ Object
- #wait_until(timeout = Capybara.default_wait_time) ⇒ Object
- #within(kind, scope = nil) ⇒ Object
- #within_fieldset(locator) ⇒ Object
- #within_frame(frame_id) ⇒ 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 = nil) ⇒ Session
Returns a new instance of Session.
20 21 22 23 |
# File 'lib/capybara/session.rb', line 20 def initialize(mode, app=nil) @mode = mode @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
18 19 20 |
# File 'lib/capybara/session.rb', line 18 def app @app end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
18 19 20 |
# File 'lib/capybara/session.rb', line 18 def mode @mode end |
Instance Method Details
#attach_file(locator, path) ⇒ Object
96 97 98 99 |
# File 'lib/capybara/session.rb', line 96 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 |
#check(locator) ⇒ Object
76 77 78 79 |
# File 'lib/capybara/session.rb', line 76 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
71 72 73 74 |
# File 'lib/capybara/session.rb', line 71 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 |
#click(locator) ⇒ Object
44 45 46 47 |
# File 'lib/capybara/session.rb', line 44 def click(locator) msg = "no link or button '#{locator}' found" locate(:xpath, XPath.link(locator).(locator), msg).click end |
#click_button(locator) ⇒ Object
54 55 56 57 |
# File 'lib/capybara/session.rb', line 54 def (locator) msg = "no button with value or id or text '#{locator}' found" locate(:xpath, XPath.(locator), msg).click end |
#click_link(locator) ⇒ Object
49 50 51 52 |
# File 'lib/capybara/session.rb', line 49 def click_link(locator) msg = "no link with title, id or text '#{locator}' found" locate(:xpath, XPath.link(locator), msg).click end |
#drag(source_locator, target_locator) ⇒ Object
59 60 61 62 63 |
# File 'lib/capybara/session.rb', line 59 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/capybara/session.rb', line 25 def driver @driver ||= begin string = mode.to_s string.gsub!(%r{(^.)|(_.)}) { |m| m[m.length-1,1].upcase } Capybara::Driver.const_get(string.to_sym).new(app) rescue NameError raise Capybara::DriverNotFoundError, "no driver called #{mode} was found" end end |
#evaluate_script(script) ⇒ Object
252 253 254 |
# File 'lib/capybara/session.rb', line 252 def evaluate_script(script) driver.evaluate_script(script) end |
#execute_script(script) ⇒ Object
248 249 250 |
# File 'lib/capybara/session.rb', line 248 def execute_script(script) driver.execute_script(script) end |
#fill_in(locator, options = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/capybara/session.rb', line 65 def fill_in(locator, ={}) msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found" raise "Must pass a hash containing 'with'" if not .is_a?(Hash) or not .has_key?(:with) locate(:xpath, XPath.fillable_field(locator), msg).set([:with]) end |
#has_button?(locator) ⇒ Boolean
191 192 193 |
# File 'lib/capybara/session.rb', line 191 def (locator) has_xpath?(XPath.(locator)) end |
#has_checked_field?(locator) ⇒ Boolean
207 208 209 |
# File 'lib/capybara/session.rb', line 207 def has_checked_field?(locator) has_xpath?(XPath.field(locator, :checked => true)) end |
#has_content?(content) ⇒ Boolean
175 176 177 |
# File 'lib/capybara/session.rb', line 175 def has_content?(content) has_xpath?(XPath.content(content)) end |
#has_css?(path, options = {}) ⇒ Boolean
167 168 169 |
# File 'lib/capybara/session.rb', line 167 def has_css?(path, ={}) has_xpath?(XPath.from_css(path), ) end |
#has_field?(locator, options = {}) ⇒ Boolean
199 200 201 |
# File 'lib/capybara/session.rb', line 199 def has_field?(locator, ={}) has_xpath?(XPath.field(locator, )) end |
#has_link?(locator) ⇒ Boolean
183 184 185 |
# File 'lib/capybara/session.rb', line 183 def has_link?(locator) has_xpath?(XPath.link(locator)) end |
#has_no_button?(locator) ⇒ Boolean
195 196 197 |
# File 'lib/capybara/session.rb', line 195 def (locator) has_no_xpath?(XPath.(locator)) end |
#has_no_content?(content) ⇒ Boolean
179 180 181 |
# File 'lib/capybara/session.rb', line 179 def has_no_content?(content) has_no_xpath?(XPath.content(content)) end |
#has_no_css?(path, options = {}) ⇒ Boolean
171 172 173 |
# File 'lib/capybara/session.rb', line 171 def has_no_css?(path, ={}) has_no_xpath?(XPath.from_css(path), ) end |
#has_no_field?(locator, options = {}) ⇒ Boolean
203 204 205 |
# File 'lib/capybara/session.rb', line 203 def has_no_field?(locator, ={}) has_no_xpath?(XPath.field(locator, )) end |
#has_no_link?(locator) ⇒ Boolean
187 188 189 |
# File 'lib/capybara/session.rb', line 187 def has_no_link?(locator) has_no_xpath?(XPath.link(locator)) end |
#has_no_select?(locator, options = {}) ⇒ Boolean
219 220 221 |
# File 'lib/capybara/session.rb', line 219 def has_no_select?(locator, ={}) has_no_xpath?(XPath.select(locator, )) end |
#has_no_table?(locator, options = {}) ⇒ Boolean
227 228 229 |
# File 'lib/capybara/session.rb', line 227 def has_no_table?(locator, ={}) has_no_xpath?(XPath.table(locator, )) end |
#has_no_xpath?(path, options = {}) ⇒ Boolean
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/capybara/session.rb', line 153 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
215 216 217 |
# File 'lib/capybara/session.rb', line 215 def has_select?(locator, ={}) has_xpath?(XPath.select(locator, )) end |
#has_table?(locator, options = {}) ⇒ Boolean
223 224 225 |
# File 'lib/capybara/session.rb', line 223 def has_table?(locator, ={}) has_xpath?(XPath.table(locator, )) end |
#has_unchecked_field?(locator) ⇒ Boolean
211 212 213 |
# File 'lib/capybara/session.rb', line 211 def has_unchecked_field?(locator) has_xpath?(XPath.field(locator, :unchecked => true)) end |
#has_xpath?(path, options = {}) ⇒ Boolean
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/capybara/session.rb', line 139 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)
237 238 239 240 241 242 |
# File 'lib/capybara/session.rb', line 237 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 '#{locator || kind_or_locator}'" unless node return node end |
#save_and_open_page ⇒ Object
231 232 233 234 |
# File 'lib/capybara/session.rb', line 231 def save_and_open_page require 'capybara/save_and_open_page' Capybara::SaveAndOpenPage.save_and_open_page(body) end |
#scope_to(*locator) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/capybara/session.rb', line 131 def scope_to(*locator) scoped_session = self.clone scoped_session.instance_eval do @scopes = scopes + locator end scoped_session end |
#select(value, options = {}) ⇒ Object
86 87 88 89 |
# File 'lib/capybara/session.rb', line 86 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 |
#uncheck(locator) ⇒ Object
81 82 83 84 |
# File 'lib/capybara/session.rb', line 81 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
91 92 93 94 |
# File 'lib/capybara/session.rb', line 91 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 |
#wait_until(timeout = Capybara.default_wait_time) ⇒ Object
244 245 246 |
# File 'lib/capybara/session.rb', line 244 def wait_until(timeout = Capybara.default_wait_time) WaitUntil.timeout(timeout,driver) { yield } end |
#within(kind, scope = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/capybara/session.rb', line 101 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
113 114 115 116 117 |
# File 'lib/capybara/session.rb', line 113 def within_fieldset(locator) within :xpath, XPath.fieldset(locator) do yield end end |
#within_frame(frame_id) ⇒ Object
125 126 127 128 129 |
# File 'lib/capybara/session.rb', line 125 def within_frame(frame_id) driver.within_frame(frame_id) do yield end end |