Selenium Extensions: RecordSpecialKeys, RecordAllClicks

Selenium Extensions: RecordSpecialKeys, RecordAllClicks

Saturday, April 14, 2012

Today I wrote two extensions for the Selenium IDE: RecordSpecialKeys and RecordAllClicks. These have been tested with the Selenium IDE 1.7.2 and are known to work on even the most convoluted web cloud app-ish bullshit, except for the meta key in RecordSpecialKeys.js: that's untested. If someone tries it out and the meta key works like the rest of the keys, let me know. ;)

RecordAllClicks was just a slight modification of the click capturing mechanism in Selenium IDE to remove the filter placed on which clicks were to be recorded. I'm usually careful enough, while recording a script, not to click on too many things I don't want clicked during testing. It works out well. Now, when I click on some ugly javascript dropdown menu meant to replace the beautiful <select> element, Selenium will record my mouseclicks and I only have to worry whether or not the dipshit behind the menu had the good sense to handle click events (as opposed to the jackass who decided mouseup was the way to go...). Anyway, you can get the extension here: http://wiki.openqa.org/download/attachments/407/RecordAllClicks.js or copy the code further down this page.

RecordSpecialKeys is an extension that does the obvious, it makes Selenium record when you press the following keys: ctrl, alt, shift, or meta. Again, I was dealing with a web form where the author had the good sense to use a javascript dropdown where I had to select multiple elements by ctrl click. Nevermind that reinventing the checkbox or radio button would have probably been easier to code and much easier to use. . . Imagine vertical and horizontal scrollbars on a dropdown list, an illegible font until set to 24px, and only 15 to 20 items that need ctrl clicked while scrolling around trying not to accidentally click without holding ctrl even once... It's much easier to write a test for the most common options and automate this nonsense so I can get back to work turning screws and pushing buttons. ;) The first time I pushed ctrl and saw nothing in the Selenium IDE I thought "well shit, I hope there's some documentation so I can make it work". Lucky for us, there was, and I'm sharing. XD You can download the extension here: http://wiki.openqa.org/download/attachments/407/RecordSpecialKeys.js or copy the code further down this page.

RecordAllClicks.js

Recorder.removeEventHandler('clickLocator');
Recorder.addEventHandler('clickLocator', 'click', function(event) {
        if (event.button == 0) {
            var clickable = this.findClickableElement(event.target);
            if (clickable) {
                // prepend any required mouseovers. These are defined as
                // handlers that set the "mouseoverLocator" attribute of the
                // interacted element to the locator that is to be used for the
                // mouseover command. For example:
                //
                // Recorder.addEventHandler('mouseoverLocator', 'mouseover', function(event) {
                //     var target = event.target;
                //     if (target.id == 'mmlink0') {
                //         this.mouseoverLocator = 'img' + target._itemRef;
                //     }
                //     else if (target.id.match(/^mmlink\d+$/)) {
                //         this.mouseoverLocator = 'lnk' + target._itemRef;
                //     }
                // }, { alwaysRecord: true, capture: true });
                //
                if (this.mouseoverLocator) {
                    this.record('mouseOver', this.mouseoverLocator, '');
                    delete this.mouseoverLocator;
                }
            }
            this.record("click", this.findLocators(event.target), '');
        }
    }, { capture: true });

RecordSpecialKeys.js

Recorder.specialKeys = {};

Recorder.specialKeys.ctrl = {};
Recorder.specialKeys.alt = {};
Recorder.specialKeys.shift = {};
Recorder.specialKeys.meta = {};

Recorder.specialKeys.ctrl.state = false;
Recorder.specialKeys.alt.state = false;
Recorder.specialKeys.shift.state = false;
Recorder.specialKeys.meta.state = false;


Recorder.addEventHandler('ctrlKeyDown', 'keydown', function(e) {
    if ( (e.ctrlKey === true) && (Recorder.specialKeys.ctrl.state === false) ) {
        this.record("controlKeyDown", '', '');
        Recorder.specialKeys.ctrl.state = true;
    }
}, { capture: true });

Recorder.addEventHandler('ctrlKeyUp', 'keyup', function(e) {
    if ( (e.ctrlKey === false) && (Recorder.specialKeys.ctrl.state === true) ) {
        this.record("controlKeyUp", '', '');
        Recorder.specialKeys.ctrl.state = false;
    }
}, { capture: true });


Recorder.addEventHandler('altKeyDown', 'keydown', function(e) {
    if ( (e.altKey === true) && (Recorder.specialKeys.alt.state === false) ) {
        this.record("altKeyDown", '', '');
        Recorder.specialKeys.alt.state = true;
    }
}, { capture: true });

Recorder.addEventHandler('altKeyUp', 'keyup', function(e) {
    if ( (e.altKey === false) && (Recorder.specialKeys.alt.state === true) ) {
        this.record("altKeyUp", '', '');
        Recorder.specialKeys.alt.state = false;
    }
}, { capture: true });


Recorder.addEventHandler('shiftKeyDown', 'keydown', function(e) {
    if ( (e.shiftKey === true) && (Recorder.specialKeys.shift.state === false) ) {
        this.record("shiftKeyDown", '', '');
        Recorder.specialKeys.shift.state = true;
    }
}, { capture: true });

Recorder.addEventHandler('shiftKeyUp', 'keyup', function(e) {
    if ( (e.shiftKey === false) && (Recorder.specialKeys.shift.state === true) ) {
        this.record("shiftKeyUp", '', '');
        Recorder.specialKeys.shift.state = false;
    }
}, { capture: true });


Recorder.addEventHandler('metaKeyDown', 'keydown', function(e) {
    if ( (e.metaKey === true) && (Recorder.specialKeys.meta.state === false) ) {
        this.record("metaKeyDown", '', '');
        Recorder.specialKeys.meta.state = true;
    }
}, { capture: true });

Recorder.addEventHandler('metaKeyUp', 'keyup', function(e) {
    if ( (e.metaKey === false) && (Recorder.specialKeys.meta.state === true) ) {
        this.record("metaKeyUp", '', '');
        Recorder.specialKeys.meta.state = false;
    }
}, { capture: true });

☭ Hial Atropa!! ☭