Permalink
Browse files

Tabs get dump method and documentation.

  • Loading branch information...
assaf committed Nov 20, 2012
1 parent baaf195 commit 52214a7c814d6ea3815e13f9ad35ce1d7fd7b235
Showing with 107 additions and 47 deletions.
  1. +6 −0 doc/new-resources.md
  2. +86 −0 doc/new-tabs.md
  3. +6 −6 lib/zombie/browser.coffee
  4. +9 −41 lib/zombie/tabs.coffee
View
@@ -18,6 +18,7 @@ any of the following:
- Implement new mechanism for retrieving resources, for example, add new
protocols or support new headers
+
### The Resources List
Each browser provides access to its resources list through `browser.resources`.
@@ -57,6 +58,10 @@ a request is being made.
The `target` property associates the resource with an HTML document or element
(only applies to some resources, like documents and scripts).
+Use `browser.resources.dump()` to dump a list of all resources to the console.
+This method accepts an optional output stream.
+
+
### Mocking, Failing and Delaying Responses
To help you in testing, you can use `browser.resources` to mock, fail or delay a
@@ -94,6 +99,7 @@ previous state:
browser.resources.restore("http://3d.party.api/v1/request");
+
### Operating On Resources
If you need to retrieve of operate on resources directly, you can do that as
View
@@ -0,0 +1,86 @@
+# Tabs
+
+Just like your favorite Web browser, Zombie manages multiple open windows as
+tabs. New browsers start without any open tabs. As you visit the first page,
+Zombie will open a tab for it.
+
+All operations against the `browser` object operate on the currently active tab
+(window) and most of the time you only need to interact with that one tab. You
+can access it directly via `browser.window`.
+
+Web pages can open additional tabs using the `window.open` method, or whenever a
+link or form specifies a target (e.g. `target=_blank` or `target=window-name`).
+You can also open additional tabs by calling `browser.open`. To close the
+currently active tab, close the window itself.
+
+You can access all open tabs from `browser.tabs`. This property is an
+associative array, you can access each tab by its index number, and iterate over
+all open tabs using functions like `forEach` and `map`.
+
+If a window was opened with a name, you can also access it by its name. Since
+names may conflict with reserved properties/methods, you may need to use
+`browser.tabs.find`.
+
+The value of a tab is the currently active window. That window changes when you
+navigate forwards and backwards in history. For example, if you visited the URL
+"/foo" and then the URL "/bar", the first tab (`browser.tabs[0]`) would be a
+window with the document from "/bar". If you then navigate back in history, the
+first tab would be the window with the document "/foo".
+
+The following operations are used for managing tabs:
+
+`browser.close(window)`
+
+Closes the tab with the given window.
+
+`browser.close()`
+
+Closes the currently open tab.
+
+`browser.tabs`
+
+Returns an array of all open tabs.
+
+`browser.tabs[number]`
+
+Returns the tab with that index number.
+
+`browser.tabs[string]`
+`browser.tabs.find(string)`
+
+Returns the tab with that name.
+
+`browser.tabs.closeAll()`
+
+Closes all tabs.
+
+`browser.tabs.current`
+
+Returns the currently active tab.
+
+`browser.tabs.current = window`
+
+Changes the currently active tab. You can set it to a window (e.g. as currently
+returned from `browser.current`), a window name or the tab index number.
+
+`browser.tabs.dump(output)`
+
+Dump a list of all open tabs to standard output, or the output stream.
+
+`browser.tabs.index`
+
+Returns the index of the currently active tab.
+
+`browser.tabs.length`
+
+Returns the number of currently opened tabs.
+
+`browser.open(url: "http://example.com")`
+
+Opens and returns a new tab. Supported options are:
+- `name` - Window name.
+- `url` - Load document from this URL.
+
+`browser.window`
+
+Returns the currently active window, same as `browser.tabs.current.`
View
@@ -197,7 +197,7 @@ class Browser extends EventEmitter
# Default time to wait (visit, wait, etc).
@waitDuration: "5s"
-
+
# Proxy URL.
#
# Example
@@ -269,7 +269,7 @@ class Browser extends EventEmitter
# Returns the last error reported while loading this window.
@prototype.__defineGetter__ "error", ->
return @errors[@errors.length - 1]
-
+
# Events
# ------
@@ -325,7 +325,7 @@ class Browser extends EventEmitter
if callback
promise.then(callback, callback)
return promise
-
+
# 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.
@@ -668,7 +668,7 @@ class Browser extends EventEmitter
return field
catch error
# Invalid selector, but may be valid field name
-
+
# Use field name (case sensitive).
for field in @querySelectorAll(":input[name]")
if field.getAttribute("name") == selector
@@ -904,7 +904,7 @@ class Browser extends EventEmitter
if !path && location = @location
path = @location.pathname
return @_cookies.access(domain || "localhost", path || "/")
-
+
# Save cookies to a text string. You can use this to load them back later on using `browser.loadCookies`.
saveCookies: ->
@_cookies.save()
@@ -927,7 +927,7 @@ class Browser extends EventEmitter
# `browser.loadStorage`.
saveStorage: ->
@_storages.save()
-
+
# Load local/session stroage from a text string (e.g. previously created using `browser.saveStorage`.
loadStorage: (serialized)->
@_storages.load serialized
View
@@ -1,45 +1,4 @@
# Tab management.
-#
-# Each browser has a set of open tabs, each tab having one current window.
-#
-# The set of tabs is an array, and you can access each tab by its index number.
-# Note that tab order will shift when you close a window. You can also get the
-# number of tabs (tabs.length) and iterate over then (map, filter, forEach).
-#
-# If a window has a name, you can also access it by name. Since names may
-# conflict with reserved properties/methods, you may need to use the find
-# method.
-#
-# You can get the window of the currently selected tab (tabs.current) or its
-# index number (tabs.index).
-#
-# To change the currently selected tab, set tabs.current to a different window,
-# a window name or the tab index number. This changes the window returned from
-# browser.window.
-#
-# Examples
-#
-# firstTab = browser.tabs[0]
-# fooTab = browser.tabs["foo"]
-# openTab = brower.tabs.find("open")
-#
-# old = browser.current
-# foo = browser.tabs["foo"]
-# browser.current = foo
-# ...
-# browser.current = old
-#
-# window = browser.tabs.open(url: "http://example.com")
-#
-# count = browser.tabs.length
-# browser.tabs.close(window)
-# assert(browser.tabs.length == count - 1)
-# browser.tabs.close()
-# assert(browser.tabs.length == count - 2)
-#
-# browser.tabs.closeAll()
-# assert(browser.tabs.length == 0)
-# assert(browser.tabs.current == null)
createHistory = require("./history")
@@ -65,6 +24,15 @@ createTabs = (browser)->
browser.emit("active", current)
return
+ # Dump list of all open tabs to stdout or output stream.
+ dump:
+ value: (output = process.stdout)->
+ if tabs.length == 0
+ output.write "No open tabs.\n"
+ else
+ for window in tabs
+ output.write "Window #{window.name || "unnamed"} open to #{window.location.href}\n"
+
# Opens and returns a tab. If an open window by the same name already exists,
# opens this window in the same tab. Omit name or use "_blank" to always open
# a new tab.

0 comments on commit 52214a7

Please sign in to comment.