Permalink
Browse files

Should be able to easily get/set cookies.

  • Loading branch information...
assaf committed Nov 30, 2012
1 parent e873514 commit 2d658e8c29dc91c3c65a21329835bc666d96b5b7
Showing with 25 additions and 13 deletions.
  1. +1 −1 lib/zombie/assert.coffee
  2. +19 −5 lib/zombie/browser.coffee
  3. +2 −6 lib/zombie/cookies.coffee
  4. +3 −1 lib/zombie/window.coffee
View
@@ -22,7 +22,7 @@ class Assert
# Asserts that a cookie with the given name has the expected value.
cookie: (name, expected, message)->
- actual = @browser.cookies().get(name)
+ actual = @browser.getCookie(name)
message ||= "Expected cooking #{name} to have the value '#{expected}', found '#{actual}'"
assertMatch actual, expected, message
View
@@ -861,11 +861,25 @@ class Browser extends EventEmitter
# Returns all the cookies for this domain/path. Domain defaults to hostname of currently open page. Path defaults to
# "/".
cookies: (domain, path)->
- if !domain && location = @location
- domain = @location.hostname
- if !path && location = @location
- path = @location.pathname
- return @_cookies.access(domain || "localhost", path || "/")
+ if location = @location
+ domain ||= location.hostname
+ return @_cookies.access(domain, path || "/")
+
+ getCookie: (name)->
+ return @cookies().get(name)
+
+ setCookie: (name, value, options)->
+ @cookies().set(name, value, options)
+ return
+
+ removeCookie: (name)->
+ @cookies().remove(name)
+ return
+
+ clearCookies: ->
+ @_cookies = new Cookies()
+ return
+
# Save cookies to a text string. You can use this to load them back later on using `browser.loadCookies`.
saveCookies: ->
@@ -53,10 +53,12 @@ class Access
# * name -- Cookie name
remove: (name)->
@_cookies.filter((cookie)=> !(cookie.key == name && cookie.domain == @domain && cookie.path == @path) )
+ return
# Clears all cookies.
clear: ->
@_cookies.filter((cookie)=> !(cookie.domain == @domain && cookie.path == @path) )
+ return
# Update cookies from serialized form. This method works equally well for
# the Set-Cookie header and value passed to document.cookie setter.
@@ -100,12 +102,6 @@ class Cookies
access: (hostname, pathname)->
return new Access(this, hostname, pathname)
- # Add cookies accessor to window: documents need this.
- extend: (window)->
- Object.defineProperty window, "cookies",
- get: ->
- return @browser.cookies(@location.hostname, @location.pathname)
-
# Used to dump state to console (debugging)
dump: ->
for cookie in @_cookies.sort(Tough.cookieCompare)
View
@@ -100,7 +100,9 @@ createWindow = ({ browser, params, encoding, history, method, name, opener, pare
vendor: { value: "Zombie Industries" }
# Add cookies, storage, alerts/confirm, XHR, WebSockets, JSON, Screen, etc
- browser._cookies.extend(window)
+ Object.defineProperty window, "cookies",
+ get: ->
+ return browser.cookies(@location.hostname, @location.pathname)
browser._storages.extend(window)
browser._interact.extend(window)

0 comments on commit 2d658e8

Please sign in to comment.