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

Commit

Permalink
Tabs get dump method and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 20, 2012
1 parent baaf195 commit 52214a7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 47 deletions.
6 changes: 6 additions & 0 deletions doc/new-resources.md
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
86 changes: 86 additions & 0 deletions doc/new-tabs.md
@@ -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.`
12 changes: 6 additions & 6 deletions lib/zombie/browser.coffee
Expand Up @@ -197,7 +197,7 @@ class Browser extends EventEmitter

# Default time to wait (visit, wait, etc).
@waitDuration: "5s"

# Proxy URL.
#
# Example
Expand Down Expand Up @@ -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
# ------
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
50 changes: 9 additions & 41 deletions lib/zombie/tabs.coffee
@@ -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")
Expand All @@ -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.
Expand Down

0 comments on commit 52214a7

Please sign in to comment.