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:
parent
5545257735
commit
9a9a4b6893
10 changed files with 91 additions and 8 deletions
|
@ -24,6 +24,10 @@ module Capybara::Poltergeist
|
|||
command 'current_url'
|
||||
end
|
||||
|
||||
def status_code
|
||||
command 'status_code'
|
||||
end
|
||||
|
||||
def body
|
||||
command 'body'
|
||||
end
|
||||
|
|
|
@ -47,6 +47,9 @@ class Poltergeist.Browser
|
|||
current_url: ->
|
||||
this.sendResponse @page.currentUrl()
|
||||
|
||||
status_code: ->
|
||||
this.sendResponse @page.statusCode()
|
||||
|
||||
body: ->
|
||||
this.sendResponse @page.content()
|
||||
|
||||
|
|
|
@ -68,6 +68,10 @@ Poltergeist.Browser = (function() {
|
|||
return this.sendResponse(this.page.currentUrl());
|
||||
};
|
||||
|
||||
Browser.prototype.status_code = function() {
|
||||
return this.sendResponse(this.page.statusCode());
|
||||
};
|
||||
|
||||
Browser.prototype.body = function() {
|
||||
return this.sendResponse(this.page.content());
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ Poltergeist.WebPage = (function() {
|
|||
|
||||
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'];
|
||||
|
||||
|
@ -52,6 +52,13 @@ Poltergeist.WebPage = (function() {
|
|||
_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() {
|
||||
this._source = null;
|
||||
this.injectAgent();
|
||||
|
@ -109,7 +116,14 @@ Poltergeist.WebPage = (function() {
|
|||
};
|
||||
|
||||
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() {
|
||||
|
@ -132,6 +146,10 @@ Poltergeist.WebPage = (function() {
|
|||
return this._errors = [];
|
||||
};
|
||||
|
||||
WebPage.prototype.statusCode = function() {
|
||||
return this._statusCode;
|
||||
};
|
||||
|
||||
WebPage.prototype.viewportSize = function() {
|
||||
return this["native"].viewportSize;
|
||||
};
|
||||
|
|
|
@ -3,7 +3,7 @@ class Poltergeist.WebPage
|
|||
'onLoadStarted', 'onResourceRequested', 'onResourceReceived',
|
||||
'onError', 'onNavigationRequested', 'onUrlChanged']
|
||||
|
||||
@DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render']
|
||||
@DELEGATES = ['sendEvent', 'uploadFile', 'release', 'render']
|
||||
|
||||
@COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize']
|
||||
|
||||
|
@ -30,6 +30,10 @@ class Poltergeist.WebPage
|
|||
this.prototype[delegate] =
|
||||
-> @native[delegate].apply(@native, arguments)
|
||||
|
||||
open: (args...) ->
|
||||
@_url = args[0]
|
||||
@native.open.apply(@native, args)
|
||||
|
||||
onInitializedNative: ->
|
||||
@_source = null
|
||||
this.injectAgent()
|
||||
|
@ -61,17 +65,21 @@ 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)
|
||||
|
||||
if @_url == response.url
|
||||
if response.redirectURL
|
||||
@_url = response.redirectURL
|
||||
else
|
||||
@_statusCode = response.status
|
||||
|
||||
networkTraffic: ->
|
||||
@_networkTraffic
|
||||
|
||||
|
@ -87,6 +95,9 @@ class Poltergeist.WebPage
|
|||
clearErrors: ->
|
||||
@_errors = []
|
||||
|
||||
statusCode: ->
|
||||
@_statusCode
|
||||
|
||||
viewportSize: ->
|
||||
@native.viewportSize
|
||||
|
||||
|
|
|
@ -82,6 +82,10 @@ module Capybara::Poltergeist
|
|||
browser.current_url
|
||||
end
|
||||
|
||||
def status_code
|
||||
browser.status_code
|
||||
end
|
||||
|
||||
def body
|
||||
browser.body
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ module Capybara::Poltergeist
|
|||
it_should_behave_like "driver"
|
||||
it_should_behave_like "driver with javascript 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 'supports a custom phantomjs path' do
|
||||
|
@ -252,6 +252,23 @@ module Capybara::Poltergeist
|
|||
@driver.visit('/poltergeist/with_js')
|
||||
@driver.network_traffic.length.should equal(4)
|
||||
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
|
||||
|
|
|
@ -10,7 +10,7 @@ describe Capybara::Session do
|
|||
it_should_behave_like "session"
|
||||
it_should_behave_like "session with javascript 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
|
||||
it 'raises an error if the element has been removed from the DOM' do
|
||||
|
|
|
@ -16,6 +16,18 @@ class TestApp
|
|||
File.read("#{POLTERGEIST_PUBLIC}/jquery-ui-1.8.14.min.js")
|
||||
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|
|
||||
erb File.read("#{POLTERGEIST_VIEWS}/#{view}.erb")
|
||||
end
|
||||
|
|
10
spec/support/views/with_different_resources.erb
Normal file
10
spec/support/views/with_different_resources.erb
Normal 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>
|
Loading…
Add table
Reference in a new issue