views:

431

answers:

3

I'm looking for a way to read the most recent command that was logged to the firebug console.

For example, I could have something that does

console.debug('The most current request URI is /sweatsocks');

And then another piece of (pseudo)code could then

if (mostRecentConsoleEntry().endsWith('/sweatsocks')) {
  // do some stuff
}

The context being the debug statement would be in the code under test, and the console checking would be done inside a selenium script. This would let me observe information buried deep in js functions as well as stuff that is built at runtime.

A: 

Could you rewrite the console.log, and append all logs to an array? Then fire up the original console.log and repeat what it's doing to get your debug output on the console?

alex
+2  A: 

You could overwrite the console.log function to add whatever extra functionality you need.

console.oldLog = console.log;
var lastLog;
console.log = function() {
    // do whatever you need to do here: store the logs into a different variable, etc
    // eg:
    lastLog = arguments[0];

    // then call the regular log command
    console.oldLog.apply(arguments); // i'm not sure if this syntax is totally right.
};

This won't be the most bulletproof solution, since console allows printf style syntax:

console.log("%d + %d = %s", 1, 3, "four");

...but it's probably a start for you.

nickf
That's what I wished I knew :) +1
alex
It don't work in FF3 and Firebug 1.3.3 since console.log is read-only. Also you are not allowed to add properties to the console object.
some
The syntax for apply is .apply(thisobject,array), so if it had worked to change the console object, it should have been: console.oldLog.apply(console.oldLog,arguments); but I suggest using a selfexecuting function to store the private variable with instead.
some
A: 

Can you really override firebug functions? I tried this, but it does not work ...

http://zembly.com/things/366ba4b32c05401eadcb513874cca688