sma
sma

Reputation: 9597

Listen for Browser Events in Protractor

Is there a way to attach event listeners to browser events in a Protractor test?

I know Protractor is running as a Node program, but I'm curious if anyone has done this with a Node package like browserevent.

Looking for any examples if so.

Upvotes: 5

Views: 2560

Answers (2)

user1207289
user1207289

Reputation: 3253

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 it seems in question and comments that it is for Javascript implementation of webdriver , which is webdriverjs . Protractor is a wrapper around Webdriverjs , so it should be valid here.

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

Upvotes: 1

Hugo Doyon
Hugo Doyon

Reputation: 21

In my opinion in the protractor spirit, No it's not possible . Protractor is a layer over selenium webdriver. Webdriver is a kind of JSON protocol that send command to communicate to the browser. Those command are store in a queue of promises and then come back async to Protractor. Then you could do Assertion with the "Expect" keyword of jasmine to inspect the DOM.

If you still need to find a way, you try the hacker way :

browser.driver.executeScript("YOUR JAVASCRIPT CODE HERE;");

Then you Wrap this call in a browser.wait(), but I would not recommend such way.

Thank you

Upvotes: 1

Related Questions