diff --git a/lib/capybara/poltergeist/browser.rb b/lib/capybara/poltergeist/browser.rb index 4d7b937..e49c7be 100644 --- a/lib/capybara/poltergeist/browser.rb +++ b/lib/capybara/poltergeist/browser.rb @@ -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 diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index 5e611c1..2046f48 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -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() diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index 29060dd..aecacac 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -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(); }; diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index 8369a80..e13e257 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -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; }; diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index 8bd16e8..aba52aa 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -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 diff --git a/lib/capybara/poltergeist/driver.rb b/lib/capybara/poltergeist/driver.rb index ca91034..742dd18 100644 --- a/lib/capybara/poltergeist/driver.rb +++ b/lib/capybara/poltergeist/driver.rb @@ -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 diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index e6c926b..6da2fe8 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -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