Permalink
Browse files

Copy cookies when forking a browser object.

  • Loading branch information...
jadell committed Jan 29, 2011
1 parent 2def6ef commit 9f411cdf83af121d81175e3ebbe10913f62e6d42
Showing with 14 additions and 4 deletions.
  1. +3 −3 spec/browser-spec.coffee
  2. +5 −0 src/zombie/browser.coffee
  3. +6 −1 src/zombie/cookies.coffee
View
@@ -318,7 +318,6 @@ vows.describe("Browser").addBatch(
browser = new zombie.Browser
browser.visit("http://localhost:3003/living")
browser.wait()
- browser.cookies("www.example").update("foo=bar; domain=www.example")
browser.cookies("www.localhost").update("foo=bar; domain=.localhost")
forked = browser.fork()
[forked, browser]
@@ -332,7 +331,8 @@ vows.describe("Browser").addBatch(
assert.equal browser.location.href, "http://localhost:3003/living"
assert.equal forked.location, "http://localhost:3003/dead"
"should manipulate cookies independently": ([forked, browser])->
- assert.equal "bar", browser.cookies("localhost").get("foo")
- assert.equal "bar", forked.cookies("localhost").get("foo")
+ browser.cookies("www.localhost").update("foo=baz; domain=.localhost")
+ assert.equal browser.cookies("localhost").get("foo"), "baz"
+ assert.equal forked.cookies("localhost").get("foo"), "bar"
).export(module)
@@ -71,8 +71,13 @@ class Browser extends require("events").EventEmitter
# Return a new browser with a snapshot of this browser's data.
# Any changes to the forked browser's state do not affect this browser.
this.fork = ->
+ serializedCookies = cookies.dump()
+
forked = new Browser()
+ forked.importCookies(serializedCookies)
return forked
+ this.importCookies = (serializedCookies) ->
+ cookies.from(serializedCookies)
# Windows
# -------
@@ -181,5 +181,10 @@ exports.use = (browser)->
for name, cookie of in_path
dump.push serialize(browser, domain, path, name, cookie)
dump
+ # Import cookies from a dump
+ from = (serialized)->
+ for cookie in serialized
+ unserialized = unserialize(cookie)
+ (new Cookies(browser, cookies, unserialized.domain, unserialized.path)).set(unserialized.name, unserialized.value, unserialized)
- return access: access, extend: extend, dump: dump
+ return access: access, extend: extend, dump: dump, from: from

0 comments on commit 9f411cd

Please sign in to comment.