Can my WebDriver script catch a event from the webpage?

Viewed 14610

I would like my WebDriver script to execute some test after the webpage fires specific event. Is this possible?

In the WebDriver script there would be some kind of event listener:

document.addEventListener("hello", function(){
    console.log("doing tests");
});

That would run when the webpage would executes:

var ev = new Event("hello");
document.dispatchEvent(ev);

Is this possible the other way around, so I could fire an event from WebDriver to the webpage?

2 Answers

I know this an old question but I just want to add some information here, maybe it was not there when this question was asked. As mentioned in question and comments that it is for Javascript implementation of webdriver , which is webdriverjs

You should be able to use addEventListener command to add any browser supported events

Note, this is only supported in chrome currently

Also, this is an experimental feature in webdriver.js so one has to add

var client = WebdriverJS.remote({
    logLevel: 'verbose',
    experimental: true, // <-- enables browser side eventhandling
    desiredCapabilities: {
        browserName: 'chrome'
    }
});

And then register events like

client
    .url('http://google.com')
    .addEventListener('dblclick','#hplogo', function(e) {
        console.log(e.target); // -> 'id("hplogo")'
        console.log(e.type); // -> 'dblclick'
        console.log(e.clientX, e.clientY); // -> 239 524
    })
    .doubleClick('#hplogo') // triggers event
    .end();

You can use removeEventListener to unregister any registered listeners

Evenhandling in Node.js environment is also supported as implied by this

WebdriverJS inherits several function from the NodeJS EventEmitter object

Related