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

Commit

Permalink
Browser now has a property called debug that you can set to
Browse files Browse the repository at this point in the history
true/false (was a function), and separately a method called `log`
that logs messages when debugging is enabled.
  • Loading branch information
assaf committed Dec 30, 2010
1 parent be9554b commit 88d2364
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 65 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@ zombie.js-changelog(1) -- Changelog
===================================


## Version 0.7.8 2010-12-29
## Version 0.8.0 2010-12-29

Fixed issue 8, wrong location of package.json.

Expand All @@ -15,6 +15,10 @@ You can now set browser options when initializing a new browser, on
existing `Browser` object or for the duration of a request by passing
them as second argument to `visit`.

Browser now has a property called `debug` that you can set to true/false
(was a function), and separately a method called `log` that logs
messages when debugging is enabled.

194 Tests
2.5 sec to complete

Expand Down
36 changes: 27 additions & 9 deletions README.md
Expand Up @@ -221,14 +221,8 @@ The current system clock according to the browser (see also `browser.now`).

Returns all the cookies for this domain/path. Path defaults to "/".

### browser.debug(boolean, function?)

Call with `true`/`false` to turn debugging on/off. Call with flag and
function to turn debugging on/off only for duration of that function
call.

### browser.debug(arguments)
### browser.debug(function)
### browser.log(arguments)
### browser.log(function)

Call with multiple arguments to spit them out to the console when
debugging enabled (same as `console.log`). Call with function to spit
Expand All @@ -240,7 +234,9 @@ Returns the main window's document. Only valid after opening a document (see `br

### browser.dump

Dump a lot of information about the browser state to the console.
Dump information to the consolt: Zombie version, current URL, history,
cookies, event loop, etc. Useful for debugging and submitting error
reports.

### browser.fill(field, value) : this

Expand Down Expand Up @@ -372,6 +368,28 @@ Zombie.js supports the following:
- Scripts that use `document.write`


## Reporting Glitches

**Step 1:** Run Zombie with debugging turned on, the trace will help
figure out what it's doing. For example:

var browser = new zombie.Browser({ debug: true });
browser.visit("http://thedead", function(err, browser) {
if (err)
throw(err.message);
...
});

**Step 2:** Wait for it to finish processing, then dump the current
browser state:

brower.dump();

**Step 3:** If publicly available, include the URL of the page you're
trying to access. Even better, provide a test script I can run from the
Node.js console (similar to step 1 above).


## The Guts

Zombie.js is written in
Expand Down
29 changes: 14 additions & 15 deletions doc/troubleshoot.md
Expand Up @@ -33,6 +33,8 @@ portion of the current page:
<body>
...

The actual report will have much more information.


## Debugging

Expand All @@ -41,36 +43,33 @@ console. These could help you see what's going on as your tests
execute, especially useful around stuff that happens in the background,
like XHR requests.

To turn debugging on/off call `browser.debug` with a boolean. You can
also call it with a boolean and a function, it will change the debug
status, call the function, and then revent the debug status to its
previous setting.
To turn debugging on/off set `browser.debug` to true/false. You can
also set this option when creating a new `Browser` object (the
constructor takes an options argument), or for the duration of a single
call to `visit` (the second argument being the options).

For example:

browser.debug(true);
// Everything that follows shows debug statements
browser.debug(false, function() {
// Except here, where debug is turned off
...
zombie.visit("http://thedead", { debug: true}, function(err, browser) {
if (err)
throw(err.message);
...
});


If you're working on the code and you want to add more debug statements,
call `browser.debug` with any sequence of arguments (same as
call `browser.log` with any sequence of arguments (same as
`console.log`), or with a function. In the later case, it will call the
function only when debugging is turned on, and spit the value returned
from the console.

For example:

browser.debug("Currently visiting", browser.location);
browser.debug(function() {
browser.log("Currently visiting", browser.location);
browser.log(function() {
return "Currently visiting " + browser.location;
});

To figure out if debugging is on, call `browser.debug` with no
arguments.


## Request/response

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "zombie",
"version": "0.7.8",
"version": "0.8.0",
"description": "Insanely fast, full-stack, headless testing using node.js",
"homepage": "http://zombie.labnotes.org/",
"author": "Assaf Arkin <assaf@labnotes.org> (http://labnotes.org/)",
Expand Down
56 changes: 21 additions & 35 deletions src/zombie/browser.coffee
Expand Up @@ -38,17 +38,20 @@ class Browser extends require("events").EventEmitter
# Options
# -------

@OPTIONS = ["runScripts"]
@OPTIONS = ["debug", "runScripts"]

# ### runScripts
#
# Run scripts included in or loaded from the page. Defaults to true.
@runScripts = true
# ### debug
#
# True to have Zombie report what it's doing.
@debug = false

if options
for k,v of options
@[k] = v if @OPTIONS.indexOf(k) >= 0
debug options.debug unless options.debug == undefined


# Events
Expand Down Expand Up @@ -432,49 +435,32 @@ class Browser extends require("events").EventEmitter
# Returns the last error received by this browser in lieu of response.
@__defineGetter__ "lastError", -> trail[trail.length - 1]?.error

debug = false
# Zombie can spit out messages to help you figure out what's going
# on as your code executes.
#
# To turn debugging on, call this method with true; to turn it off,
# call with false. You can also call with a setting and a function,
# in which case it will turn debugging on or off, execute the function
# and then switch it back to its current settings.
#
# For example:
# browser.debug(true, function() {
# // Need to you be verbose here
# ...
# });
#
# To spit a message to the console when running in debug mode, call
# this method with one or more values (same as `console.log`). You
# can also call it with a function that will be evaluated only when
# running in debug mode.
#
# For example:
# browser.debug("Opening page:", url);
# browser.debug(function() { return "Opening page: " + url });
#
# With no arguments returns the current debug state.
this.debug = ->
return debug if arguments.length == 0
if typeof arguments[0] == "boolean"
old = debug
debug = arguments[0]
if typeof arguments[1] == "function"
try
arguments[1]()
finally
debug = old
else if debug
fields = ["Zombie:"]
# browser.log("Opening page:", url);
# browser.log(function() { return "Opening page: " + url });
this.log = ->
if @debug
values = ["Zombie:"]
if typeof arguments[0] == "function"
fields.push arguments[0]()
else if debug
fields.push arg for arg in arguments
console.log.apply null, fields

try
values.push arguments[0]()
catch ex
values.push ex
else
values.push arg for arg in arguments
console.log.apply null, values

# Dump information to the consolt: Zombie version, current URL,
# history, cookies, event loop, etc. Useful for debugging and
# submitting error reports.
this.dump = ->
indent = (lines)-> lines.map((l) -> " #{l}\n").join("")
console.log "Zombie: #{exports.version}\n"
Expand Down
6 changes: 3 additions & 3 deletions src/zombie/eventloop.coffee
Expand Up @@ -138,15 +138,15 @@ class EventLoop
# completes processing, passing error and response arguments.
this.request = (request, fn)->
url = request.url.toString()
browser.debug -> "#{request.method} #{url}"
browser.log -> "#{request.method} #{url}"
requests.push url
pending = browser.record request
fn (err, response)->
if err
browser.debug -> "Error loading #{url}: #{err}"
browser.log -> "Error loading #{url}: #{err}"
pending.error = err
else
browser.debug -> "#{request.method} #{url} => #{response.status}"
browser.log -> "#{request.method} #{url} => #{response.status}"
pending.response = response
index = requests.indexOf(url)
requests.splice index, 1
Expand Down
2 changes: 1 addition & 1 deletion src/zombie/jsdom_patches.coffee
Expand Up @@ -64,7 +64,7 @@ core.languageProcessors =
javascript: (element, code, filename)->
document = element.ownerDocument
window = document.parentWindow
window.browser.debug -> "Running script from #{filename}" if filename
window.browser.log -> "Running script from #{filename}" if filename
if window
ctx = vm.Script.createContext(window)
script = new vm.Script(code, filename)
Expand Down

0 comments on commit 88d2364

Please sign in to comment.