Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Methods like visit now pass error to the callback if they fail to l…
Browse files Browse the repository at this point in the history
…oad or

parse the page.  JavaScript execution errors are handled separately.
  • Loading branch information
assaf committed May 1, 2012
1 parent 3cd0b63 commit 68e746b
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 90 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ zombie.js-changelog(7) -- Changelog

Upgraded to JSDOM 0.2.14.

Methods like `visit` now pass error to the callback if they fail to load or
parse the page. JavaScript execution errors are handled separately.

434 tests
8.9 sec to complete

Expand Down
9 changes: 8 additions & 1 deletion lib/zombie/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,16 @@ class Browser extends EventEmitter
wait: (duration, callback)->
if !callback && typeof duration == "function"
[callback, duration] = [duration, null]
@_eventloop.wait @window, duration, (error)=>

completed = (error)=>
@removeListener "done", completed
@removeListener "error", completed
if callback
callback error, this
@addListener "done", completed
@addListener "error", completed

@_eventloop.wait @window, duration
return

# Fire a DOM event. You can use this to simulate a DOM event, e.g. clicking a link. These events will bubble up and
Expand Down
13 changes: 9 additions & 4 deletions lib/zombie/eventloop.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,18 @@ class EventLoop
# Remove from waiting list, pause timers if last waiting.
@_waiting = (fn for fn in @_waiting when fn != waiting)
@_pause() if @_waiting.length == 0
process.nextTick ->
callback error, window
if terminate
clearTimeout(terminate)

# Callback and event emitter, pick your poison.
if callback
process.nextTick ->
callback error, window
if error
@_browser.emit "error", error
else
@_browser.emit "done"

# don't block forever
terminate = setTimeout(done, 5000)

Expand All @@ -172,10 +179,8 @@ class EventLoop
# If there are no timers, next is Infinity, larger then done_at, no waiting
if next <= done_at
return
@_browser.emit "done", @_browser
done()
catch error
@_browser.emit "error", error
done(error)

# No one is waiting, resume firing timers.
Expand Down
6 changes: 4 additions & 2 deletions lib/zombie/history.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ class History
html = "<html><body></body></html>"
document.write html
document.close()
if document.documentElement
if response.statusCode >= 400
@_browser.emit "error", new Error("Server returned status code #{response.statusCode}")
else if document.documentElement && response.statusCode < 300
@_browser.emit "loaded", @_browser
else
#@_browser.emit "error", "Could not parse document at #{URL.format(url)}"
@_browser.emit "error", new Error("Could not parse document at #{URL.format(url)}")

# ### history.forward()
forward: -> @go(1)
Expand Down
2 changes: 2 additions & 0 deletions lib/zombie/jsdom_patches.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ HTML.languageProcessors.javascript = (element, code, filename)->
try
window._evaluate code, filename
catch error
unless error instanceof Error
error = new Error(error.message)
raise element: element, location: filename, from: __filename, error: error


Expand Down
12 changes: 8 additions & 4 deletions test/authentication_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe "Authentication", ->
describe "without credentials", ->
browser = new Browser()
before (done)->
browser.visit "http://localhost:3003/auth/basic", done
browser.visit "http://localhost:3003/auth/basic", ->
done()

it "should return status code 401", ->
assert.equal browser.statusCode, 401
Expand All @@ -27,7 +28,8 @@ describe "Authentication", ->
browser = new Browser()
before (done)->
credentials = { scheme: "basic", user: "username", password: "wrong" }
browser.visit "http://localhost:3003/auth/basic", credentials: credentials, done
browser.visit "http://localhost:3003/auth/basic", credentials: credentials, ->
done()

it "should return status code 401", ->
assert.equal browser.statusCode, 401
Expand Down Expand Up @@ -57,7 +59,8 @@ describe "Authentication", ->
describe "without credentials", ->
browser = new Browser()
before (done)->
browser.visit "http://localhost:3003/auth/oauth2", done
browser.visit "http://localhost:3003/auth/oauth2", ->
done()

it "should return status code 401", ->
assert.equal browser.statusCode, 401
Expand All @@ -66,7 +69,8 @@ describe "Authentication", ->
browser = new Browser()
before (done)->
credentials = { scheme: "bearer", token: "wrong" }
browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, done
browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, ->
done()

it "should return status code 401", ->
assert.equal browser.statusCode, 401
Expand Down
47 changes: 26 additions & 21 deletions test/browser_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ describe "Browser", ->
<title>Whatever</title>
<script src="/jquery.js"></script>
</head>
<body>Hello World</body>
<script>
document.title = "Nice";
$(function() { $("title").text("Awesome") })
</script>
<script type="text/x-do-not-parse">
<p>this is not valid JavaScript</p>
</script>
<body>
<h1>Hello World</h1>
<script>
document.title = "Nice";
$(function() { $("title").text("Awesome") })
</script>
<script type="text/x-do-not-parse">
<p>this is not valid JavaScript</p>
</script>
</body>
</html>
"""

brains.get "/browser/errored", (req, res)->
res.send """
<script>this.is.wrong</script>
<html>
<head>
<script>this.is.wrong</script>
</head>
</html>
"""

brains.ready done
Expand All @@ -41,7 +47,7 @@ describe "Browser", ->
it "should create HTML document", ->
assert browser.document instanceof JSDOM.dom.level3.html.HTMLDocument
it "should load document from server", ->
assert /<body>Hello World/.test(browser.html())
assert.equal browser.text("body h1"), "Hello World"
it "should load external scripts", ->
assert jQuery = browser.window.jQuery, "window.jQuery not available"
assert.equal typeof jQuery.ajax, "function"
Expand All @@ -66,7 +72,7 @@ describe "Browser", ->
done()

it "should call callback without error", ->
assert true
assert !error
it "should pass browser to callback", ->
assert browser instanceof Browser
it "should pass status code to callback", ->
Expand All @@ -90,10 +96,12 @@ describe "Browser", ->
done()

it "should call callback without error", ->
assert true
console.dir error
assert error instanceof Error
it "should indicate success", ->
assert browser.success
it "should pass errors to callback", ->
console.log errors
assert.equal errors.length, 1
assert.equal errors[0].message, "Cannot read property 'wrong' of undefined"
it "should set browser errors", ->
Expand All @@ -109,8 +117,8 @@ describe "Browser", ->
[error, browser, status, errors] = arguments
done()

it "should call callback without error", ->
assert true
it "should call with error", ->
assert error instanceof Error
it "should return status code", ->
assert.equal status, 404
it "should not indicate success", ->
Expand All @@ -133,8 +141,8 @@ describe "Browser", ->
[error, browser, status, errors] = arguments
done()

it "should call callback without error", ->
assert true
it "should call callback with error", ->
assert error instanceof Error
it "should return status code 500", ->
assert.equal status, 500
it "should not indicate success", ->
Expand Down Expand Up @@ -181,18 +189,15 @@ describe "Browser", ->

describe "wait over", ->
browser = new Browser()
args = null

before (done)->
browser.on "done", ->
args = arguments
done()
browser.window.location = "http://localhost:3003/browser/scripted"
browser.wait()

it "should fire done event with browser", ->
assert args
assert args[0].visit
it "should fire done event", ->
assert true

describe "error", ->
browser = new Browser()
Expand Down
3 changes: 2 additions & 1 deletion test/forms_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,8 @@ describe "Forms", ->
</html>
"""
browser.visit "http://localhost:3003/forms/urlencoded/empty", ->
browser.pressButton "submit", done
browser.pressButton "submit", ->
done() # 404 since there's no get for this form

it "should send content-length header", ->
assert browser.lastRequest.headers.hasOwnProperty("content-length")
Expand Down
28 changes: 24 additions & 4 deletions test/script_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,42 @@ describe "Scripts", ->
describe "failing", ->
describe "incomplete", ->
browser = new Browser()
error = null

before (done)->
brains.get "/script/incomplete", (req, res)->
res.send "<script>1+</script>"
browser.visit "http://localhost:3003/script/incomplete", done
res.send """
<html>
<script>1+</script>
</html>
"""
browser.visit "http://localhost:3003/script/incomplete", (_)->
error = _
done()

it "should pass error to callback", ->
assert.equal error.message, "Unexpected end of input"

it "should propagate error to window", ->
assert.equal browser.error.message, "Unexpected end of input"

describe "error", ->
browser = new Browser()
error = null

before (done)->
brains.get "/script/error", (req, res)->
res.send "<script>(function(foo) { foo.bar })()</script>"
browser.visit "http://localhost:3003/script/error", done
res.send """
<html
<script>(function(foo) { foo.bar })()</script>
</html>
"""
browser.visit "http://localhost:3003/script/error", (_)->
error = _
done()

it "should pass error to callback", ->
assert.equal error.message, "Cannot read property 'bar' of undefined"

it "should propagate error to window", ->
assert.equal browser.error.message, "Cannot read property 'bar' of undefined"
Expand Down
36 changes: 21 additions & 15 deletions test/window_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ describe "Window", ->
before (done)->
brains.get "/window/alert", (req, res)->
res.send """
<script>
alert("Hi");
alert("Me again");
</script>
<html>
<script>
alert("Hi");
alert("Me again");
</script>
</html>
"""
browser.onalert (message)->
if message = "Me again"
Expand All @@ -55,11 +57,13 @@ describe "Window", ->
before (done)->
brains.get "/window/confirm", (req, res)->
res.send """
<script>
window.first = confirm("continue?");
window.second = confirm("more?");
window.third = confirm("silent?");
</script>
<html>
<script>
window.first = confirm("continue?");
window.second = confirm("more?");
window.third = confirm("silent?");
</script>
</html>
"""
browser.onconfirm "continue?", true
browser.onconfirm (prompt)->
Expand All @@ -85,12 +89,14 @@ describe "Window", ->
before (done)->
brains.get "/window/prompt", (req, res)->
res.send """
<script>
window.first = prompt("age");
window.second = prompt("gender");
window.third = prompt("location");
window.fourth = prompt("weight");
</script>
<html>
<script>
window.first = prompt("age");
window.second = prompt("gender");
window.third = prompt("location");
window.fourth = prompt("weight");
</script>
</html>
"""
browser.onprompt "age", 31
browser.onprompt (message, def)->
Expand Down
Loading

0 comments on commit 68e746b

Please sign in to comment.