From 72e5d2d276f4610e78bb14b619da108d2fb3e1bc Mon Sep 17 00:00:00 2001 From: Dmitriy Nesteryuk Date: Thu, 19 Jul 2012 22:48:12 +0300 Subject: [PATCH] merged new version of code for identifying HTTP code of an loaded page --- README.md | 1 + .../poltergeist/client/compiled/web_page.js | 14 ++++++------- .../poltergeist/client/web_page.coffee | 12 +++++------ spec/integration/driver_spec.rb | 4 ++-- spec/integration/session_spec.rb | 20 +++++++++++++++++++ spec/support/test_app.rb | 11 ++++++++-- .../views/with_different_resources.erb | 5 +++++ 7 files changed, 50 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7bb165f..f73e424 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ makes debugging easier). Running `rake autocompile` will watch the (wynst) [Issue #97] * Scroll element into viewport if needed on click (Gabriel Sobrinho) [Issue #83] +* Added status code support. (Dmitriy Nesteryuk and Jon Leighton) [Issue #37] #### Bug fixes ### diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index 7ae8ea6..e31bd89 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -77,6 +77,10 @@ Poltergeist.WebPage = (function() { } }; + WebPage.prototype.onLoadStartedNative = function() { + return this.requestId = this.lastRequestId; + }; + WebPage.prototype.onLoadFinishedNative = function() { return this._source || (this._source = this["native"].content); }; @@ -101,12 +105,8 @@ Poltergeist.WebPage = (function() { }); }; - WebPage.prototype.onLoadStartedNative = function() { - return this._url = this.lastUrl; - }; - WebPage.prototype.onResourceRequestedNative = function(request) { - this.lastUrl = request.url; + this.lastRequestId = request.id; return this._networkTraffic[request.id] = { request: request, responseParts: [] @@ -115,9 +115,9 @@ Poltergeist.WebPage = (function() { WebPage.prototype.onResourceReceivedNative = function(response) { this._networkTraffic[response.id].responseParts.push(response); - if (this._url === response.url) { + if (this.requestId === response.id) { if (response.redirectURL) { - return this._url = response.redirectURL; + return this.requestId = response.id; } else { return this._statusCode = response.status; } diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index 20f438b..7dd1da7 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -45,6 +45,9 @@ class Poltergeist.WebPage @_source = @native.content false + onLoadStartedNative: -> + @requestId = @lastRequestId + onLoadFinishedNative: -> @_source or= @native.content @@ -61,11 +64,8 @@ class Poltergeist.WebPage @_errors.push(message: message, stack: stackString) - onLoadStartedNative: -> - @_url = @lastUrl - onResourceRequestedNative: (request) -> - @lastUrl = request.url + @lastRequestId = request.id @_networkTraffic[request.id] = { request: request, @@ -75,9 +75,9 @@ class Poltergeist.WebPage onResourceReceivedNative: (response) -> @_networkTraffic[response.id].responseParts.push(response) - if @_url == response.url + if @requestId == response.id if response.redirectURL - @_url = response.redirectURL + @requestId = response.id else @_statusCode = response.status diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index 4294cd5..2a8d732 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -254,9 +254,9 @@ module Capybara::Poltergeist end end - describe 'status code support' do + context 'status code support' do it 'should determine status from the simple response' do - @driver.visit('/poltergeist/500') + @driver.visit('/poltergeist/status/500') @driver.status_code.should == 500 end diff --git a/spec/integration/session_spec.rb b/spec/integration/session_spec.rb index b933a8b..bb99328 100644 --- a/spec/integration/session_spec.rb +++ b/spec/integration/session_spec.rb @@ -253,5 +253,25 @@ describe Capybara::Session do @session.evaluate_script("3;").should == 3 end end + + context 'status code support', :status_code_support => true do + it 'should determine status code when an user goes to a page by using a link on it' do + @session.visit '/poltergeist/with_different_resources' + + @session.click_link 'Go to 500' + + @session.status_code.should == 500 + end + + it 'should determine properly status code when an user goes through a few pages' do + @session.visit '/poltergeist/with_different_resources' + + @session.click_link 'Go to 201' + @session.click_link 'Do redirect' + @session.click_link 'Go to 402' + + @session.status_code.should == 402 + end + end end end diff --git a/spec/support/test_app.rb b/spec/support/test_app.rb index b8a8612..42d956a 100644 --- a/spec/support/test_app.rb +++ b/spec/support/test_app.rb @@ -20,8 +20,9 @@ class TestApp halt 404 end - get '/poltergeist/500' do - halt 500 + get '/poltergeist/status/:status' do + status params['status'] + render_view 'with_different_resources' end get '/poltergeist/redirect' do @@ -29,6 +30,12 @@ class TestApp end get '/poltergeist/:view' do |view| + render_view view + end + + protected + + def render_view(view) erb File.read("#{POLTERGEIST_VIEWS}/#{view}.erb") end end diff --git a/spec/support/views/with_different_resources.erb b/spec/support/views/with_different_resources.erb index 3035a42..a36d277 100644 --- a/spec/support/views/with_different_resources.erb +++ b/spec/support/views/with_different_resources.erb @@ -6,5 +6,10 @@ + + Do redirect + Go to 201 + Go to 402 + Go to 500 \ No newline at end of file