Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Closes assaf#314] Added browser.load.
  • Loading branch information
assaf committed May 28, 2012
1 parent 6476744 commit a4ca575
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,8 @@ browser (though the spec insists on empty string).

Reload document when forking browser.

Added `browser.load` for loading HTML instead of hitting a URL.


## Version 1.1.7 2012-05-27

Expand Down
6 changes: 6 additions & 0 deletions doc/API.md
Expand Up @@ -254,6 +254,12 @@ Returns the status code returned for this page request (200, 303, etc).

Returns true if the status code is 2xx.

### browser.load(html, callback)

Loads this HTML, processes events and calls the callback.

Without a callback, returns a promise.

### browser.visit(url, callback)
### browser.visit(url, options, callback)

Expand Down
67 changes: 43 additions & 24 deletions lib/zombie/browser.coffee
Expand Up @@ -184,22 +184,18 @@ class Browser extends EventEmitter
[callback, duration] = [duration, null]

deferred = Q.defer()
promise = deferred.promise

last = @errors[@errors.length - 1]
@_eventloop.wait @window, duration, (error)=>
newest = @errors[@errors.length - 1]
unless error || last == newest
error = newest
if error
deferred.reject(error)
if callback
callback(error)
else
deferred.resolve()
if callback
callback(error)
return promise unless callback
if callback
callback(error)
return deferred.promise unless callback

# Fire a DOM event. You can use this to simulate a DOM event, e.g. clicking a link. These events will bubble up and
# can be cancelled. Like `wait` this method either takes a callback or returns a promise.
Expand Down Expand Up @@ -359,29 +355,52 @@ class Browser extends EventEmitter
[duration, options] = [options, null]

deferred = Q.defer()
promise = deferred.promise
if callback
promise.then =>
# This serves two purposes, one is yielding, the other is propagating
# any error thrown from the callback (then/fail swallow errors).
process.nextTick =>
callback null, this, @statusCode, @errors
promise.fail (error)=>
process.nextTick =>
callback(error, this, @statusCode, @errors)

reset_options = @withOptions(options)
if site = @site
site = "http://#{site}" unless /^(https?:|file:)/i.test(site)
url = URL.resolve(site, URL.parse(URL.format(url)))
@window.history._assign url
@wait(duration).then ->
reset_options()
deferred.resolve()
.fail (error)->
@wait duration, (error)=>
reset_options()
deferred.reject(error)
return promise unless callback
if error
deferred.reject(error)
else
deferred.resolve()
if callback
callback error, this, @statusCode, @errors
return deferred.promise unless callback


# ### browser.load(html, callback)
#
# Loads the HTML, processes events and calls the callback.
#
# Without a callback, returns a promise.
load: (html, callback)->
try
@errors = []
@document.open()
@document.write html
@document.close()
catch error
@emit "error", error

# Find (first of any) errors caught during document.write
first = @errors[0]
if first
# Call callback or resolve promise
if callback
process.nextTick ->
callback(first)
return
else
deferred = Q.defer()
deferred.reject(first)
return deferred.promise
else
# Otherwise wait for all events to process, wait handles errors
return @wait(callback)


# ### browser.location => Location
#
Expand Down
20 changes: 20 additions & 0 deletions test/browser_test.coffee
Expand Up @@ -372,6 +372,26 @@ describe "Browser", ->
assert.equal browser.text("body"), "This is plain text"


describe "load HTML string", ->
before (done)->
@browser = new Browser()
@browser.load("""
<title>Load</title>
<script>document.title = document.title + " html"</script>
<div id="main"></div>
""")
.then(done, done)

it "should use about:blank URL", ->
assert.equal @browser.location.href, "about:blank"

it "should load document", ->
assert @browser.query("#main")

it "should execute JavaScript", ->
assert.equal @browser.document.title, "Load html"


describe "windows", ->

describe "new browser", ->
Expand Down

0 comments on commit a4ca575

Please sign in to comment.