Skip to content

Commit

Permalink
[Closes assaf#220] Added silent option to suppress all console.log
Browse files Browse the repository at this point in the history
output from scripts.
  • Loading branch information
assaf committed Dec 13, 2011
1 parent 7079f78 commit 802e899
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ You can put Zombie in debug mode by setting environment variable `DEBUG`, for ex

$ DEBUG=true vows

Also added `silent` option to suppress all `console.log` output from scripts.

Support origin in websockets (Glen Mailer)

Proper support for CSS style `opacity` property.
Expand Down
1 change: 1 addition & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ You can use the following options:
- `loadCSS` -- Loads external stylesheets. Defaults to true.
- `runScripts` -- Run scripts included in or loaded from the page. Defaults to true.
- `userAgent` -- The User-Agent string to send to the server.
- `silent` -- If true, supress all `console.log` output from scripts. You can still view it with `window.console.output`.
- `site` -- Base URL for all requests. If set, you can call `visit` with relative URL.
- `waitFor` -- Tells `wait` function how long to wait (in milliseconds) while timers fire. Defaults to 5 seconds.

Expand Down
49 changes: 23 additions & 26 deletions lib/zombie/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ require "./jsdom_patches"
require "./forms"
require "./xpath"

{ deprecated } = require("./helpers")
{ Cache } = require("./cache")
{ Cookies } = require("./cookies")
{ EventEmitter } = require("events")
{ EventLoop } = require("./eventloop")
{ History } = require("./history")
{ HTML5 } = require("html5")
Interact = require("./interact")
JSDom = require("jsdom")
{ Resources } = require("./resources")
{ Storages } = require("./storage")
URL = require("url")
WebSocket = require("./websocket")
XHR = require("./xhr")
{ deprecated } = require("./helpers")
{ Cache } = require("./cache")
{ Console } = require("./console")
{ Cookies } = require("./cookies")
{ EventEmitter } = require("events")
{ EventLoop } = require("./eventloop")
{ History } = require("./history")
{ HTML5 } = require("html5")
Interact = require("./interact")
JSDom = require("jsdom")
{ Resources } = require("./resources")
{ Storages } = require("./storage")
URL = require("url")
WebSocket = require("./websocket")
XHR = require("./xhr")


HTML = JSDom.dom.level3.html
MOUSE_EVENT_NAMES = ["mousedown", "mousemove", "mouseup"]
BROWSER_OPTIONS = ["credentials", "debug", "htmlParser", "loadCSS", "runScripts", "site", "userAgent", "waitFor"]
BROWSER_OPTIONS = ["credentials", "debug", "htmlParser", "loadCSS", "runScripts", "silent", "site", "userAgent", "waitFor"]


PACKAGE = JSON.parse(require("fs").readFileSync(__dirname + "/../../package.json"))
Expand All @@ -45,8 +46,7 @@ class Browser extends EventEmitter
# reported while processing resources/JavaScript.
@on "error", (error)=>
@errors.push error
if @debug
console.error error.message, error.stack
@log error.message, error.stack


# Options
Expand Down Expand Up @@ -90,6 +90,11 @@ class Browser extends EventEmitter
# Run scripts included in or loaded from the page. Defaults to true.
@runScripts = true

# ### silent
#
# If true, supress `console.log` output from scripts.
@silent = false

# ### userAgent
#
# User agent string sent to server.
Expand All @@ -114,14 +119,6 @@ class Browser extends EventEmitter
@[name] = value
else
@[name] = value
###
if options
for k,v of options
if BROWSER_OPTIONS.indexOf(k) >= 0
@[k] = v
else
throw "I don't recognize the option #{k}"
###

# ### browser.errors => Array
#
Expand Down Expand Up @@ -211,7 +208,7 @@ class Browser extends EventEmitter
img.width = width
img.height = height
return img
newWindow.console = console
newWindow.console = new Console(@silent)

# Default onerror handler.
newWindow.onerror = (event)=>
Expand Down
66 changes: 66 additions & 0 deletions lib/zombie/console.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
try
Util = require("util")
catch e
Util = require("sys")


class Console
constructor: (@_silent)->

assert: console.assert.bind(console)

count: (name)->
@_counters ||= {}
@_counters[name] ||= 0
@_counters[name]++
@log "#{name}: #{@_counters[name]}"

debug: ->
@log.apply(this, arguments)

error: ->
@log.apply(this, arguments)

group: ->
groupCollapsed: ->
groupEnd: ->

info: ->
@log.apply(this, arguments)

log: ->
formatted = ((if typeof arg == "string" then arg else Util.inspect(arg, console.showHidden, console.depth)) for arg in arguments)
if typeof Util.format == "function"
output = Util.format.apply(this, formatted) + "\n"
else
output = formatted.join(" ") + "\n"
@_output ||= []
@_output.push output
unless @_silent
process.stdout.write output

warn: ->
@log.apply(this, arguments)

time: (name)->
@_timers ||= {}
@_timers[name] = Date.now()

timeEnd: (name)->
return unless @_timers
start = @_timers[name]
return unless start
delete @_timers[name]
@log "#{name}: #{Date.now() - start}ms"

trace: ->
stack = (new Error).stack.split("\n")
stack[0] = "console.trace()"
@log stack.join("\n")

# Returns all output captured by this console.
@prototype.__defineGetter__ "output", ->
return (if @_output then @_output.join("\n") else "")


exports.Console = Console
5 changes: 3 additions & 2 deletions spec/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Browser = Zombie.Browser

# Always run in verbose mode on Travis.
Zombie.debug = true if process.env.TRAVIS
Zombie.silent = !Zombie.debug


# An express server we use to test the browser.
Expand All @@ -24,8 +25,8 @@ brains.get "/", (req, res)->
# messages for debugging.
brains.get "/sammy.js", (req, res)->
File.readFile "#{__dirname}/scripts/sammy.js", (err, data)->
unless process.env.DEBUG
data = data + ";window.Sammy.log = function() {}"
# unless process.env.DEBUG
# data = data + ";window.Sammy.log = function() {}"
res.send data

brains.get "/jquery.js", (req, res)->
Expand Down

0 comments on commit 802e899

Please sign in to comment.