mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00
commit
fad740f90a
10 changed files with 173 additions and 3 deletions
|
@ -13,6 +13,8 @@ module Capybara
|
|||
autoload :Inspector, 'capybara/poltergeist/inspector'
|
||||
autoload :Spawn, 'capybara/poltergeist/spawn'
|
||||
autoload :JSON, 'capybara/poltergeist/json'
|
||||
autoload :Request, 'capybara/poltergeist/request'
|
||||
autoload :Response, 'capybara/poltergeist/response'
|
||||
|
||||
require 'capybara/poltergeist/errors'
|
||||
end
|
||||
|
|
|
@ -111,6 +111,18 @@ module Capybara::Poltergeist
|
|||
command 'resize', width, height
|
||||
end
|
||||
|
||||
def network_traffic
|
||||
response = command 'networkTraffic'
|
||||
|
||||
response.values.map do |event|
|
||||
request = Request.new(event['request'])
|
||||
event['responseParts'].each do |response|
|
||||
request.response_parts.push(Response.new(response))
|
||||
end
|
||||
request
|
||||
end
|
||||
end
|
||||
|
||||
def command(name, *args)
|
||||
message = { 'name' => name, 'args' => args }
|
||||
log message.inspect
|
||||
|
|
|
@ -154,6 +154,9 @@ class Poltergeist.Browser
|
|||
@page.setViewportSize(width: width, height: height)
|
||||
this.sendResponse(true)
|
||||
|
||||
networkTraffic: ->
|
||||
this.sendResponse(@page.networkTraffic())
|
||||
|
||||
exit: ->
|
||||
phantom.exit()
|
||||
|
||||
|
|
|
@ -211,6 +211,10 @@ Poltergeist.Browser = (function() {
|
|||
return this.sendResponse(true);
|
||||
};
|
||||
|
||||
Browser.prototype.networkTraffic = function() {
|
||||
return this.sendResponse(this.page.networkTraffic());
|
||||
};
|
||||
|
||||
Browser.prototype.exit = function() {
|
||||
return phantom.exit();
|
||||
};
|
||||
|
|
|
@ -15,6 +15,7 @@ Poltergeist.WebPage = (function() {
|
|||
this["native"] = require('webpage').create();
|
||||
this._source = "";
|
||||
this._errors = [];
|
||||
this._networkTraffic = {};
|
||||
this.setViewportSize({
|
||||
width: width,
|
||||
height: height
|
||||
|
@ -100,6 +101,21 @@ Poltergeist.WebPage = (function() {
|
|||
});
|
||||
};
|
||||
|
||||
WebPage.prototype.onResourceRequestedNative = function(request) {
|
||||
return this._networkTraffic[request.id] = {
|
||||
request: request,
|
||||
responseParts: []
|
||||
};
|
||||
};
|
||||
|
||||
WebPage.prototype.onResourceReceivedNative = function(response) {
|
||||
return this._networkTraffic[response.id].responseParts.push(response);
|
||||
};
|
||||
|
||||
WebPage.prototype.networkTraffic = function() {
|
||||
return this._networkTraffic;
|
||||
};
|
||||
|
||||
WebPage.prototype.content = function() {
|
||||
return this["native"].content;
|
||||
};
|
||||
|
|
|
@ -8,9 +8,10 @@ class Poltergeist.WebPage
|
|||
@COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize']
|
||||
|
||||
constructor: (width, height) ->
|
||||
@native = require('webpage').create()
|
||||
@_source = ""
|
||||
@_errors = []
|
||||
@native = require('webpage').create()
|
||||
@_source = ""
|
||||
@_errors = []
|
||||
@_networkTraffic = {}
|
||||
|
||||
this.setViewportSize(width: width, height: height)
|
||||
|
||||
|
@ -60,6 +61,20 @@ class Poltergeist.WebPage
|
|||
|
||||
@_errors.push(message: message, stack: stackString)
|
||||
|
||||
# capture any outgoing requests
|
||||
onResourceRequestedNative: (request) ->
|
||||
@_networkTraffic[request.id] = {
|
||||
request: request,
|
||||
responseParts: []
|
||||
}
|
||||
|
||||
# capture request responses
|
||||
onResourceReceivedNative: (response) ->
|
||||
@_networkTraffic[response.id].responseParts.push(response)
|
||||
|
||||
networkTraffic: ->
|
||||
@_networkTraffic
|
||||
|
||||
content: ->
|
||||
@native.content
|
||||
|
||||
|
|
|
@ -113,6 +113,10 @@ module Capybara::Poltergeist
|
|||
browser.resize(width, height)
|
||||
end
|
||||
|
||||
def network_traffic
|
||||
browser.network_traffic
|
||||
end
|
||||
|
||||
def debug
|
||||
inspector.open
|
||||
pause
|
||||
|
|
30
lib/capybara/poltergeist/request.rb
Normal file
30
lib/capybara/poltergeist/request.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Capybara::Poltergeist
|
||||
|
||||
class Request
|
||||
|
||||
attr_reader :response_parts
|
||||
|
||||
def initialize(data)
|
||||
@data = data
|
||||
@response_parts = []
|
||||
end
|
||||
|
||||
def url
|
||||
@data['url']
|
||||
end
|
||||
|
||||
def method
|
||||
@data['method']
|
||||
end
|
||||
|
||||
def headers
|
||||
@data['headers']
|
||||
end
|
||||
|
||||
def time
|
||||
@data['time'] && Time.parse(@data['time'])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
44
lib/capybara/poltergeist/response.rb
Normal file
44
lib/capybara/poltergeist/response.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
module Capybara::Poltergeist
|
||||
|
||||
class Response
|
||||
|
||||
def initialize(data)
|
||||
@data = data
|
||||
end
|
||||
|
||||
def url
|
||||
@data['url']
|
||||
end
|
||||
|
||||
def status
|
||||
@data['status']
|
||||
end
|
||||
|
||||
def status_text
|
||||
@data['statusText']
|
||||
end
|
||||
|
||||
def headers
|
||||
@data['headers']
|
||||
end
|
||||
|
||||
def redirect_url
|
||||
@data['redirectUrl']
|
||||
end
|
||||
|
||||
def body_size
|
||||
@data['bodySize']
|
||||
end
|
||||
|
||||
def content_type
|
||||
@data['contentType']
|
||||
end
|
||||
|
||||
def time
|
||||
@data['time'] && Time.parse(@data['time'])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -205,5 +205,45 @@ module Capybara::Poltergeist
|
|||
expect { driver.execute_script "" }.to_not raise_error(JavascriptError)
|
||||
end
|
||||
end
|
||||
|
||||
context "network traffic" do
|
||||
before do
|
||||
@driver.restart
|
||||
end
|
||||
|
||||
it "keeps track of network traffic" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
urls = @driver.network_traffic.map(&:url)
|
||||
|
||||
urls.grep(%r{/poltergeist/jquery-1.6.2.min.js$}).size.should == 1
|
||||
urls.grep(%r{/poltergeist/jquery-ui-1.8.14.min.js$}).size.should == 1
|
||||
urls.grep(%r{/poltergeist/test.js$}).size.should == 1
|
||||
end
|
||||
|
||||
it "captures responses" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
request = @driver.network_traffic.last
|
||||
|
||||
request.response_parts.last.status.should == 200
|
||||
end
|
||||
|
||||
it "keeps a running list between multiple web page views" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.network_traffic.length.should equal(4)
|
||||
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.network_traffic.length.should equal(8)
|
||||
end
|
||||
|
||||
it "gets cleared on restart" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.network_traffic.length.should equal(4)
|
||||
|
||||
@driver.restart
|
||||
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.network_traffic.length.should equal(4)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue