1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Status code support. Closes #37.

This commit is contained in:
Dmitriy Nesteryuk 2012-06-04 08:58:27 +03:00 committed by Jon Leighton
parent 5545257735
commit 9a9a4b6893
10 changed files with 91 additions and 8 deletions

View file

@ -24,6 +24,10 @@ module Capybara::Poltergeist
command 'current_url' command 'current_url'
end end
def status_code
command 'status_code'
end
def body def body
command 'body' command 'body'
end end

View file

@ -47,6 +47,9 @@ class Poltergeist.Browser
current_url: -> current_url: ->
this.sendResponse @page.currentUrl() this.sendResponse @page.currentUrl()
status_code: ->
this.sendResponse @page.statusCode()
body: -> body: ->
this.sendResponse @page.content() this.sendResponse @page.content()

View file

@ -68,6 +68,10 @@ Poltergeist.Browser = (function() {
return this.sendResponse(this.page.currentUrl()); return this.sendResponse(this.page.currentUrl());
}; };
Browser.prototype.status_code = function() {
return this.sendResponse(this.page.statusCode());
};
Browser.prototype.body = function() { Browser.prototype.body = function() {
return this.sendResponse(this.page.content()); return this.sendResponse(this.page.content());
}; };

View file

@ -6,7 +6,7 @@ Poltergeist.WebPage = (function() {
WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged']; WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged'];
WebPage.DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render']; WebPage.DELEGATES = ['sendEvent', 'uploadFile', 'release', 'render'];
WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize']; WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize'];
@ -52,6 +52,13 @@ Poltergeist.WebPage = (function() {
_fn1(delegate); _fn1(delegate);
} }
WebPage.prototype.open = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
this._url = args[0];
return this["native"].open.apply(this["native"], args);
};
WebPage.prototype.onInitializedNative = function() { WebPage.prototype.onInitializedNative = function() {
this._source = null; this._source = null;
this.injectAgent(); this.injectAgent();
@ -109,7 +116,14 @@ Poltergeist.WebPage = (function() {
}; };
WebPage.prototype.onResourceReceivedNative = function(response) { WebPage.prototype.onResourceReceivedNative = function(response) {
return this._networkTraffic[response.id].responseParts.push(response); this._networkTraffic[response.id].responseParts.push(response);
if (this._url === response.url) {
if (response.redirectURL) {
return this._url = response.redirectURL;
} else {
return this._statusCode = response.status;
}
}
}; };
WebPage.prototype.networkTraffic = function() { WebPage.prototype.networkTraffic = function() {
@ -132,6 +146,10 @@ Poltergeist.WebPage = (function() {
return this._errors = []; return this._errors = [];
}; };
WebPage.prototype.statusCode = function() {
return this._statusCode;
};
WebPage.prototype.viewportSize = function() { WebPage.prototype.viewportSize = function() {
return this["native"].viewportSize; return this["native"].viewportSize;
}; };

View file

@ -3,7 +3,7 @@ class Poltergeist.WebPage
'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived',
'onError', 'onNavigationRequested', 'onUrlChanged'] 'onError', 'onNavigationRequested', 'onUrlChanged']
@DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render'] @DELEGATES = ['sendEvent', 'uploadFile', 'release', 'render']
@COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize'] @COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize']
@ -30,6 +30,10 @@ class Poltergeist.WebPage
this.prototype[delegate] = this.prototype[delegate] =
-> @native[delegate].apply(@native, arguments) -> @native[delegate].apply(@native, arguments)
open: (args...) ->
@_url = args[0]
@native.open.apply(@native, args)
onInitializedNative: -> onInitializedNative: ->
@_source = null @_source = null
this.injectAgent() this.injectAgent()
@ -61,17 +65,21 @@ class Poltergeist.WebPage
@_errors.push(message: message, stack: stackString) @_errors.push(message: message, stack: stackString)
# capture any outgoing requests
onResourceRequestedNative: (request) -> onResourceRequestedNative: (request) ->
@_networkTraffic[request.id] = { @_networkTraffic[request.id] = {
request: request, request: request,
responseParts: [] responseParts: []
} }
# capture request responses
onResourceReceivedNative: (response) -> onResourceReceivedNative: (response) ->
@_networkTraffic[response.id].responseParts.push(response) @_networkTraffic[response.id].responseParts.push(response)
if @_url == response.url
if response.redirectURL
@_url = response.redirectURL
else
@_statusCode = response.status
networkTraffic: -> networkTraffic: ->
@_networkTraffic @_networkTraffic
@ -87,6 +95,9 @@ class Poltergeist.WebPage
clearErrors: -> clearErrors: ->
@_errors = [] @_errors = []
statusCode: ->
@_statusCode
viewportSize: -> viewportSize: ->
@native.viewportSize @native.viewportSize

View file

@ -82,6 +82,10 @@ module Capybara::Poltergeist
browser.current_url browser.current_url
end end
def status_code
browser.status_code
end
def body def body
browser.body browser.body
end end

View file

@ -11,7 +11,7 @@ module Capybara::Poltergeist
it_should_behave_like "driver" it_should_behave_like "driver"
it_should_behave_like "driver with javascript support" it_should_behave_like "driver with javascript support"
it_should_behave_like "driver with frame support" it_should_behave_like "driver with frame support"
it_should_behave_like "driver without status code support" it_should_behave_like "driver with status code support"
it_should_behave_like "driver with cookies support" it_should_behave_like "driver with cookies support"
it 'supports a custom phantomjs path' do it 'supports a custom phantomjs path' do
@ -252,6 +252,23 @@ module Capybara::Poltergeist
@driver.visit('/poltergeist/with_js') @driver.visit('/poltergeist/with_js')
@driver.network_traffic.length.should equal(4) @driver.network_traffic.length.should equal(4)
end end
end end
describe 'status code support' do
it 'should determine status from the simple response' do
@driver.visit('/poltergeist/500')
@driver.status_code.should == 500
end
it 'should determine status code when the page has a few resources' do
@driver.visit('/poltergeist/with_different_resources')
@driver.status_code.should == 200
end
it 'should determine status code even after redirect' do
@driver.visit('/poltergeist/redirect')
@driver.status_code.should == 200
end
end
end end
end end

View file

@ -10,7 +10,7 @@ describe Capybara::Session do
it_should_behave_like "session" it_should_behave_like "session"
it_should_behave_like "session with javascript support" it_should_behave_like "session with javascript support"
it_should_behave_like "session without headers support" it_should_behave_like "session without headers support"
it_should_behave_like "session without status code support" it_should_behave_like "session with status code support"
describe Capybara::Poltergeist::Node do describe Capybara::Poltergeist::Node do
it 'raises an error if the element has been removed from the DOM' do it 'raises an error if the element has been removed from the DOM' do

View file

@ -16,6 +16,18 @@ class TestApp
File.read("#{POLTERGEIST_PUBLIC}/jquery-ui-1.8.14.min.js") File.read("#{POLTERGEIST_PUBLIC}/jquery-ui-1.8.14.min.js")
end end
get '/poltergeist/unexist.png' do
halt 404
end
get '/poltergeist/500' do
halt 500
end
get '/poltergeist/redirect' do
redirect '/poltergeist/with_different_resources'
end
get '/poltergeist/:view' do |view| get '/poltergeist/:view' do |view|
erb File.read("#{POLTERGEIST_VIEWS}/#{view}.erb") erb File.read("#{POLTERGEIST_VIEWS}/#{view}.erb")
end end

View file

@ -0,0 +1,10 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>poltergeist with_different_resources</title>
</head>
<body>
<img src="unexist.png">
</body>
</html>