reloadAndWaitFor

This extension adds assertions similar to waitFor* called reloadAndWaitFor*. If an assertions is called with that name, the current page is reloaded every 20 seconds. Additionally the whole assertion times out after 70 seconds.

This is a useful extension in the context of insertions done by the tests. In big environments a change may only be visible after a few seconds and the page has to be reloaded again for the change to be visible.

The extension is used to test the local.ch market, a Swiss classifieds platform.

Licence

Apache License, V2. This code was developed by Patrice Neff for local.ch.

How to add it to selenium:

1. Add the following code into the 'user-extensions.js' file:

user-extensions.js
/*
 * Extend Selenium core with reloadAndWaitFor* asserts (reload as often as necessary for up to 60 seconds)
 * Copyright 2007 - local.ch ag
 * Version 0.2
 *   0.1: Initial release
 *   0.2: Remove redundancies
 *   0.3: Fix reloading
 */
CommandHandlerFactory.prototype._oldRegisterWaitForCommandsForPredicate = CommandHandlerFactory.prototype._registerWaitForCommandsForPredicate;
CommandHandlerFactory.prototype._registerWaitForCommandsForPredicate = function(commandTarget, baseName, predicateBlock) {
    this._oldRegisterWaitForCommandsForPredicate(commandTarget, baseName, predicateBlock);

    // Register a reloadAndWaitForBlah and reloadAndWaitForNotBlah based on the specified accessor.
    var cmdBlock = this._reloadAndWaitForActionForPredicate(predicateBlock);
    this.registerAction("reloadAndWaitFor" + baseName, cmdBlock, false, true);
    var invertedCmdBlock = this._invertPredicate(cmdBlock);
    var invertedNotCmdBlock = this._reloadAndWaitForActionForPredicate(invertedCmdBlock);
    this.registerAction("reloadAndWaitFor" + this._invertPredicateName(baseName), invertedNotCmdBlock, false, true);
    this.registerAction("reloadAndWaitForNot" + baseName, invertedNotCmdBlock, false, true);
}
CommandHandlerFactory.prototype._reloadAndWaitForActionForPredicate = function(predicateBlock) {
    // Convert an isBlahBlah(target, value) function into a reloadAndWaitForBlahBlah(target, value) function.
    return function(target, value) {
        var retval = Selenium.decorateFunctionWithTimeout(function() {
            var status = false;
            if (this.lastReload == undefined)
                this.lastReload = new Date().getTime();
            try {
                status = predicateBlock(target, value).isTrue;
            } catch (e) {
                // Treat exceptions as meaning the condition is not yet met.
            }

            if (status == false) {
                if (new Date().getTime() > this.lastReload+1000) {
                    // Reload every 20 seconds
                    selenium.doRefresh();
                    this.lastReload = new Date().getTime();
                }
            }
            return status;
        }, 70000);
        return retval;
    };
};

Example:

Sample Selenium code:
open /test-page  
reloadAndWaitForTextPresent New username  

File: 

AttachmentSize
Plain text icon reloadAndWaitFor.txt2.22 KB

Type: 

User-Extension