简体   繁体   English

在 Protractor 中监听浏览器事件

[英]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 .我知道 Protractor 是作为 Node 程序运行的,但我很好奇是否有人使用像browserevent这样的 Node 包来完成此操作。

Looking for any examples if so.如果是这样,请寻找任何示例。

In my opinion in the protractor spirit, No it's not possible .在我看来,量角器精神,不,这是不可能的。 Protractor is a layer over selenium webdriver. Protractor 是 selenium webdriver 上的一层。 Webdriver is a kind of JSON protocol that send command to communicate to the browser. Webdriver 是一种 JSON 协议,它发送命令与浏览器进行通信。 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.然后你可以用 jasmine 的“Expect”关键字做断言来检查 DOM。

If you still need to find a way, you try the hacker way :如果你仍然需要找到一种方法,你可以尝试黑客方式:

browser.driver.executeScript("YOUR JAVASCRIPT CODE HERE;"); browser.driver.executeScript("你的 JAVASCRIPT 代码在这里;");

Then you Wrap this call in a browser.wait(), but I would not recommend such way.然后您将这个调用包装在 browser.wait() 中,但我不推荐这种方式。

Thank you谢谢

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 .正如有问题和评论一样,它用于 webdriver 的 Javascript 实现,即webdriverjs Protractor is a wrapper around Webdriverjs , so it should be valid here. ProtractorWebdriverjs的包装器,所以它在这里应该是有效的。

You should be able to use addEventListener command to add any browser supported events您应该能够使用addEventListener命令添加任何浏览器支持的事件

Note, this is only supported in chrome currently请注意,目前仅支持 chrome

Also, this is an experimental feature in webdriver.js so one has to add此外,这是 webdriver.js 中的一项实验性功能,因此必须添加

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您可以使用removeEventListener取消注册任何已注册的侦听器

Evenhandling in Node.js environment is also supported as implied by this也支持 Node.js 环境中的偶数处理,正如这里暗示的那样

WebdriverJS inherits several function from the NodeJS EventEmitter object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM