Mankz’s Blog

Selenium Core extension for finding unexpected global variables

Posted in Uncategorized by Mats Bryntse on February 11, 2009

The FrameworkScanner does a decent job finding the global symbols of the Ajax frameworks. The analysis however isn’t 100% truthful though: new variables could be accidentally introduced as you’re running your application. Consider a function doing


for (p in window) {
// Do stuff
}

Now we have ourself a new global, and in worst case, a mini-timebomb. So I was playing around with Selenium and I found a new twist on global variables! :) Let’s analyze the window object as the last step of a Selenium test to see if it has unexpected variables. This kind of analysis is probably overkill in many situations but I think it could be useful in a heavy client side app especially if you’re using a lot of 3rd party libraries (to make sure they behave).

To use:

1. Download this file or paste the code below into a new file and reference it in “Selenium Core Extensions” (in Options of Selenium IDE).

2. Define which global symbols you expect to exist such as any 3rd party namespaces as well as your own application namespaces to the list of expected globals, something like this:

Selenium.prototype.assertNoUnknownGlobals = function() {
	// List expected global symbols
    var acceptedGlobals = [
        // Your accepted global symbols go here
        // JQuery,
        // $
        // etc...

        // Selenium properties
        '_locator_pageBot',

        // Firebug properties
        'getInterface',
        '_firebug',
        '_FirebugCommandLine',
        '_getFirebugConsole',
        '_getFirebugConsoleElement',
        'loadFirebugConsole',
        'console'
    ];

    var frame = document.createElement('iframe');
    var root = document.body || document.documentElement;
    root.appendChild(frame);
    var cleanWin = frame.contentWindow;

    var symbols = [];

    var appWin = this.browserbot.getCurrentWindow();

    for (var p in appWin) {
        if (typeof (cleanWin[p]) === 'undefined' &&
            acceptedGlobals.indexOf(p) < 0 &&
            !p.match(/selenium/i)) {
            symbols.push(p);
        }
    }
    root.removeChild(frame);

    if (symbols.length > 0) {
        Assert.fail(symbols.length + " unexpected globals detected: " + symbols.sort().join(',	'));
    }
};

Selenium IDE

Tagged with: ,