Class: Capybara::Driver::Envjs

Inherits:
Base
  • Object
show all
Defined in:
lib/capybara/driver/envjs_driver.rb

Defined Under Namespace

Classes: Headers, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject (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_hostObject



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

#bodyObject



263
264
265
# File 'lib/capybara/driver/envjs_driver.rb', line 263

def body
  browser["window"].document.xml
end

#browserObject



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_urlObject



255
256
257
# File 'lib/capybara/driver/envjs_driver.rb', line 255

def current_url
  browser["window"].location.href
end

#default_hostObject



139
140
141
# File 'lib/capybara/driver/envjs_driver.rb', line 139

def default_host
  (Capybara.default_host || "www.example.com")
end

#default_urlObject



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

Returns:

  • (Boolean)


328
329
330
# File 'lib/capybara/driver/envjs_driver.rb', line 328

def has_shortcircuit_timeout?
  true
end

#rack_test?Boolean

Returns:

  • (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!
  clear_cookies
end

#response_headersObject



281
282
283
# File 'lib/capybara/driver/envjs_driver.rb', line 281

def response_headers
  Headers.new(browser["window"]["document"]["__headers__"])
end

#sourceObject



259
260
261
# File 'lib/capybara/driver/envjs_driver.rb', line 259

def source
  browser["window"].document.__original_text__
end

#status_codeObject



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

Returns:

  • (Boolean)


301
# File 'lib/capybara/driver/envjs_driver.rb', line 301

def wait?; true; end

#wait_until(max) ⇒ Object

Raises:

  • (Capybara::TimeoutError)


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