zneak
zneak

Reputation: 138031

Can Selenium notice script errors?

I'm working with Selenium right now, and I'd like to know if it's possible to make Selenium find out if there were script errors.

I'm conscious that acting upon script errors would require flow control, and that Selenium IDE doesn't have that. I'm also conscious that if the errors were serious, then certainly the test cases would fail, and there we have it. Still, I'd like Selenium to be able to at least store them somewhere, somehow.

We run our tests in Firefox and IE, so we can use window.onerror to record errors. However, I'm not sure about how to integrate it to Selenium. As far as I know, the recorder attaches its handler to window.document, and we would need to attach to window itself. I've tried to decorate Recoreder.prototype.attach from the user extension file to add a handler myself, but it's rather clumsy and causes odd behaviors in the IDE (like nothing records anymore, so I probably did it wrong).

Any idea?

Upvotes: 2

Views: 763

Answers (1)

zneak
zneak

Reputation: 138031

I've dug deep enough in Selenium to get a good answer.

Decorating Recorder.prototype.attach and Recorder.prototype.detach works well; you just have to attach yourself to the window.onerror event to get a good idea of what kind of bad things happen on your page. The problems come when it's time to act upon the errors. There are two choices:

  • Create a custom command that checks if there were errors since the last time it was called;
  • Edit Selenium's source code to make it check if there were errors with the last command before each new command.

It is not possible to implement the latter with extensions because the file you need to change the behavior of is loaded after the extension files.

Here is how you can decorate the appropriate function from an user extension:

function decorate(decoratee, decorator) {
    var decorated = function() {
        decorator.apply(this, arguments);
        if (decoratee && decoratee.apply)
            decoratee.apply(this, arguments);
    }
    decorated.base = decoratee;
    decorated.decorator = decorator;
    return decorated;
}

Recorder.prototype.attach = decorate(Recorder.prototype.attach, function() {
    var win = this.getWrappedWindow();
    win.onerror = decorate(win.onerror, function(message, file, line) {
        // do something with the error
    });
});

Recorder.prototype.detach = decorate(Recorder.prototype.detach, function() {
    var win = this.getWrappedWindow();
    win.onerror = win.onerror.base;
});

Upvotes: 1

Related Questions