mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00
keep track of outgoing connections
Used PhantomJS's onResourceRequested callback to capture requests. Added driver requested_resources() endpoint that returns an array of outgoing page requests and accepts an optional filter.
This commit is contained in:
parent
0db1eb9767
commit
24e4c2496c
7 changed files with 94 additions and 0 deletions
|
@ -111,6 +111,10 @@ module Capybara::Poltergeist
|
|||
command 'resize', width, height
|
||||
end
|
||||
|
||||
def requested_resources(filter=nil)
|
||||
command 'requestedResources', filter
|
||||
end
|
||||
|
||||
def command(name, *args)
|
||||
message = { 'name' => name, 'args' => args }
|
||||
log message.inspect
|
||||
|
|
|
@ -191,6 +191,16 @@ class Poltergeist.Browser
|
|||
@page.setViewportSize(width: width, height: height)
|
||||
this.sendResponse(true)
|
||||
|
||||
requestedResources: (filter) ->
|
||||
if filter
|
||||
matches = []
|
||||
for request in @page.requestedResources()
|
||||
matches.push(request) if request.match(filter)
|
||||
else
|
||||
matches = @page.requestedResources()
|
||||
|
||||
this.sendResponse(matches)
|
||||
|
||||
exit: ->
|
||||
phantom.exit()
|
||||
|
||||
|
|
|
@ -237,6 +237,23 @@ Poltergeist.Browser = (function() {
|
|||
return this.sendResponse(true);
|
||||
};
|
||||
|
||||
Browser.prototype.requestedResources = function(filter) {
|
||||
var matches, request, _i, _len, _ref;
|
||||
if (filter) {
|
||||
matches = [];
|
||||
_ref = this.page.requestedResources();
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
request = _ref[_i];
|
||||
if (request.match(filter)) {
|
||||
matches.push(request);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
matches = this.page.requestedResources();
|
||||
}
|
||||
return this.sendResponse(matches);
|
||||
};
|
||||
|
||||
Browser.prototype.exit = function() {
|
||||
return phantom.exit();
|
||||
};
|
||||
|
|
|
@ -15,6 +15,7 @@ Poltergeist.WebPage = (function() {
|
|||
this["native"] = require('webpage').create();
|
||||
this._source = "";
|
||||
this._errors = [];
|
||||
this._requestedResources = [];
|
||||
this.setViewportSize({
|
||||
width: width,
|
||||
height: height
|
||||
|
@ -93,6 +94,14 @@ Poltergeist.WebPage = (function() {
|
|||
});
|
||||
};
|
||||
|
||||
WebPage.prototype.onResourceRequestedNative = function(request) {
|
||||
return this._requestedResources.push(request.url);
|
||||
};
|
||||
|
||||
WebPage.prototype.requestedResources = function() {
|
||||
return this._requestedResources;
|
||||
};
|
||||
|
||||
WebPage.prototype.content = function() {
|
||||
return this["native"].content;
|
||||
};
|
||||
|
|
|
@ -11,6 +11,7 @@ class Poltergeist.WebPage
|
|||
@native = require('webpage').create()
|
||||
@_source = ""
|
||||
@_errors = []
|
||||
@_requestedResources = []
|
||||
|
||||
this.setViewportSize(width: width, height: height)
|
||||
|
||||
|
@ -58,6 +59,13 @@ class Poltergeist.WebPage
|
|||
onErrorNative: (message, stack) ->
|
||||
@_errors.push(message: message, stack: stack)
|
||||
|
||||
# capture any outgoing requests
|
||||
onResourceRequestedNative: (request) ->
|
||||
@_requestedResources.push(request.url)
|
||||
|
||||
requestedResources: ->
|
||||
@_requestedResources
|
||||
|
||||
content: ->
|
||||
@native.content
|
||||
|
||||
|
|
|
@ -110,6 +110,10 @@ module Capybara::Poltergeist
|
|||
browser.resize(width, height)
|
||||
end
|
||||
|
||||
def requested_resources(filter=nil)
|
||||
browser.requested_resources(filter)
|
||||
end
|
||||
|
||||
def debug
|
||||
inspector.open
|
||||
pause
|
||||
|
|
|
@ -174,5 +174,47 @@ module Capybara::Poltergeist
|
|||
expect { driver.execute_script "" }.to_not raise_error(JavascriptError)
|
||||
end
|
||||
end
|
||||
|
||||
context "requested resources" do
|
||||
before do
|
||||
@driver.restart
|
||||
end
|
||||
|
||||
it "keeps track of outgoing resource requests" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
urls = @driver.requested_resources
|
||||
|
||||
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 "keeps a running list between multiple web page views" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.requested_resources.length.should equal(4)
|
||||
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.requested_resources.length.should equal(8)
|
||||
end
|
||||
|
||||
it "gets cleared on restart" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.requested_resources.length.should equal(4)
|
||||
|
||||
@driver.restart
|
||||
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.requested_resources.length.should equal(4)
|
||||
end
|
||||
|
||||
it "supports filtering" do
|
||||
@driver.visit('/poltergeist/with_js')
|
||||
urls = @driver.requested_resources('jquery')
|
||||
|
||||
urls.length.should equal(2)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue