Class: Capybara::Driver::Envjs
- Inherits:
-
Base
- Object
- Base
- Capybara::Driver::Envjs
- Defined in:
- lib/capybara/driver/envjs_driver.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
- #app_host ⇒ Object
- #body ⇒ Object
- #browser ⇒ Object
- #current_url ⇒ Object
- #default_host ⇒ Object
- #default_url ⇒ Object
- #evaluate_script(script) ⇒ Object
- #execute_script(script) ⇒ Object
- #find(selector) ⇒ Object
- #has_shortcircuit_timeout? ⇒ Boolean
-
#initialize(app) ⇒ Envjs
constructor
A new instance of Envjs.
- #rack_test? ⇒ Boolean
- #reset! ⇒ Object
- #response_headers ⇒ Object
- #source ⇒ Object
- #status_code ⇒ Object
- #visit(path) ⇒ Object
- #wait? ⇒ Boolean
- #wait_until(max) ⇒ Object
Constructor Details
#initialize(app) ⇒ Envjs
Returns a new instance of Envjs.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/capybara/driver/envjs_driver.rb', line 151 def initialize(app) if rack_test? require 'rack/test' class << self; self; end.instance_eval do include ::Rack::Test::Methods alias_method :response, :last_response alias_method :request, :last_request define_method :build_rack_mock_session do # p default_host Rack::MockSession.new(app, default_host) end end end @app = app master_load = browser.master["load"] if rack_test? browser.master["load"] = proc do |*args| if args.size == 2 and args[1].to_s != "[object split_global]" file, window = *args body = nil if file.index(app_host) == 0 get(file, {}, env) body = response.body else body = Net::HTTP.get(URI.parse(file)) end window["evaluate"].call body else master_load.call *args end end browser["window"]["$envx"]["connection"] = browser.master["connection"] = @connection = proc do |*args| xhr, responseHandler, data = *args url = xhr.url params = data || "" method = xhr["method"].downcase.to_sym e = env; if method == :post or method == :put e.merge! "CONTENT_TYPE" => xhr.headers["Content-Type"] end e.merge! "HTTP_ACCEPT" => xhr.headers["Accept"] if xhr.headers["Accept"] if e["CONTENT_TYPE"] =~ %r{^multipart/form-data;} e["CONTENT_LENGTH"] ||= params.length end times = 0 begin # p url, app_host if url.index(app_host) == 0 url.slice! 0..(app_host.length-1) end # p url # puts "send #{method} #{url} #{params} #{e}" send method, url, params, e # p "after" #, response while response.status == 302 || response.status == 301 if (times += 1) > 5 raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." end params = {} method = :get url = response.location if url.index(app_host) == 0 url.slice! 0..(app_host.length-1) end # puts "redirect #{method} #{url} #{params}" send method, url, params, env end rescue Exception => e # print "got #{e} #{response.inspect}\n" raise e end @source = response.body response.headers.each do |k,v| xhr.responseHeaders[k] = v end xhr.status = response.status xhr.responseText = response.body xhr.readyState = 4 if url.index(app_host) == 0 url.slice! 0..(app_host.length-1) end if url.slice(0..0) == "/" url = app_host+url end xhr.__url = url responseHandler.call end end end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
132 133 134 |
# File 'lib/capybara/driver/envjs_driver.rb', line 132 def app @app end |
Instance Method Details
#app_host ⇒ Object
147 148 149 |
# File 'lib/capybara/driver/envjs_driver.rb', line 147 def app_host (ENV["CAPYBARA_APP_HOST"] || Capybara.app_host || default_url) end |
#body ⇒ Object
263 264 265 |
# File 'lib/capybara/driver/envjs_driver.rb', line 263 def body browser["window"].document.xml end |
#browser ⇒ Object
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/capybara/driver/envjs_driver.rb', line 317 def browser unless @_browser require 'johnson/tracemonkey' require 'envjs/runtime' @_browser = Johnson::Runtime.new :size => Integer(ENV["JOHNSON_HEAP_SIZE"] || 0x4000000) @_browser.extend Envjs::Runtime end @_browser end |
#current_url ⇒ Object
255 256 257 |
# File 'lib/capybara/driver/envjs_driver.rb', line 255 def current_url browser["window"].location.href end |
#default_host ⇒ Object
139 140 141 |
# File 'lib/capybara/driver/envjs_driver.rb', line 139 def default_host (Capybara.default_host || "www.example.com") end |
#default_url ⇒ Object
143 144 145 |
# File 'lib/capybara/driver/envjs_driver.rb', line 143 def default_url "http://"+default_host end |
#evaluate_script(script) ⇒ Object
313 314 315 |
# File 'lib/capybara/driver/envjs_driver.rb', line 313 def evaluate_script(script) browser["window"]["evaluate"].call(script) end |
#execute_script(script) ⇒ Object
308 309 310 311 |
# File 'lib/capybara/driver/envjs_driver.rb', line 308 def execute_script(script) browser["window"]["evaluate"].call(script) nil end |
#find(selector) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/capybara/driver/envjs_driver.rb', line 289 def find(selector) window = browser["window"] null = browser["null"] type = window["XPathResult"]["ANY_TYPE"] result_set = window.document.evaluate selector, window.document, null, type, null nodes = [] while n = result_set.iterateNext() nodes << Node.new(self, n) end nodes end |
#has_shortcircuit_timeout? ⇒ Boolean
328 329 330 |
# File 'lib/capybara/driver/envjs_driver.rb', line 328 def has_shortcircuit_timeout? true end |
#rack_test? ⇒ Boolean
134 135 136 137 |
# File 'lib/capybara/driver/envjs_driver.rb', line 134 def rack_test? # p "rt?", app_host, default_url, (app_host == default_url) app_host == default_url end |
#reset! ⇒ Object
267 268 269 |
# File 'lib/capybara/driver/envjs_driver.rb', line 267 def reset! end |
#response_headers ⇒ Object
281 282 283 |
# File 'lib/capybara/driver/envjs_driver.rb', line 281 def response_headers Headers.new(browser["window"]["document"]["__headers__"]) end |
#source ⇒ Object
259 260 261 |
# File 'lib/capybara/driver/envjs_driver.rb', line 259 def source browser["window"].document.__original_text__ end |
#status_code ⇒ Object
285 286 287 |
# File 'lib/capybara/driver/envjs_driver.rb', line 285 def status_code response.status end |
#visit(path) ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/capybara/driver/envjs_driver.rb', line 247 def visit(path) as_url = URI.parse path base = URI.parse app_host path = (base + as_url).to_s # p path browser["window"].location.href = path end |
#wait? ⇒ Boolean
301 |
# File 'lib/capybara/driver/envjs_driver.rb', line 301 def wait?; true; end |
#wait_until(max) ⇒ Object
303 304 305 306 |
# File 'lib/capybara/driver/envjs_driver.rb', line 303 def wait_until max fired, wait = *browser["Envjs"].wait(-max*1000) raise Capybara::TimeoutError if !fired && wait.nil? end |