Selenium + Node.js:是否可以监听重复发生的事件? [英] Selenium + Node.js: is it possible to listen for reoccurring events?

查看:64
本文介绍了Selenium + Node.js:是否可以监听重复发生的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个由AJAX/REST API调用大力支持的站点上工作,该站点在完成/失败时广播事件.

I am working on a site that is heavily powered by AJAX/REST API calls, which broadcast events on completion/failure.

我要完成的工作是监听这些文档事件并在Selenium(Node.js)中触发一个函数-现在我正在为 console.log()进行设置--并保持该侦听器运行以报告"customEvent"的任何新出现

What I am trying to accomplish is to listen for these document events and trigger a function in Selenium (Node.js) -- right now I'm settling for a console.log() -- and keep that listener running to report any new occurrences of the "customEvent"

我最新的实现方式如下:

My latest implementation looks like this:

driver = await new Builder().forBrowser('chrome').build();
await driver.get('http://www.google.com/');

...

driver.executeScript(`
  document.addEventListener("customEvent", (e) => {
    return e;
  })`).
then( (e) => { console.log( e ); } );

我也尝试过 executeAsyncScript()

我遇到的问题:

  • 此代码只想运行一次(不会保持打开状态)
  • 它要么没有捕获到"customEvent",要么阻止了代码的继续执行,否则将触发该事件.

推荐答案

感谢一位同事,我们找到了一种解决方法:

Thanks to a co-worker, we have found a workaround solution:

  1. 使用Selenium的.executeScript()
    • 在已加载的网站中创建HTML DIV(具有特定ID)
    • 将事件侦听器添加到Selenium驱动程序创建的document元素中
    • 使用从事件中捕获的数据填充创建的DIV

driver.executeScript(`
    var emptyDiv = document.createElement('div');
        emptyDiv.id = '${target}';
        emptyDiv.style.display = 'none';
    function _trackEvent(e){
        var target = document.getElementById('${target}');
        if( !target ){ document.body.appendChild( emptyDiv ); }
        target.innerHTML += JSON.stringify(e.detail);
    }
    document.addEventListener("${name}", _trackEvent);
`);

  1. 使用 driver.wait(until.elementLocated(By.id('')))引用步骤1中声明的DIV ID
    • 这将确保硒暂停,直到以浏览器的JS速度创建某些内容为止
  1. Use driver.wait(until.elementLocated(By.id(''))) referencing the DIV id declared in step #1
    • This will make sure selenium pauses until something has been created at the browser's JS speed

trackEvent.start('customEvent', 'testTarget');
await driver.wait(until.elementLocated(By.id('testTarget')));
let testEvent = await trackEvent.stop('testTarget');
console.log( testEvent );

  1. 查询创建的元素并将其数据返回给Selenium

return driver.executeScript(`
    return document.getElementById('${target}').innerHTML;
`).then(( data )=>{
    return data;
});

  1. 将所有内容拖放到测试中并进行尝试.

const trackEvent = {
    start  : ( name, target ) => {
        driver.executeScript(`
            var emptyDiv = document.createElement('div');
                emptyDiv.id = '${target}';
                emptyDiv.style.display = 'none';
            function _trackEvent(e){
                var target = document.getElementById('${target}');
                if( !target ){ document.body.appendChild( emptyDiv ); }
                target.innerHTML += JSON.stringify(e.detail);
            }
            document.addEventListener("${name}", _trackEvent);
        `);
    },
    stop  : ( target ) => {
        return driver.executeScript(`
            return document.getElementById('${target}').innerHTML;
        `).then(( data )=>{
            return data;
        });
    } 
};

有了这一切,硒就不会立即尝试做两件事.Selenium正在运行其测试,并且其网站​​(正在启动)正在为我们监听事件.

With all of this, selenium is not trying to do two things at once. Selenium is running its tests and the website (that it spun up) is listening for the event(s) for us.

唯一的暂停是Selenium等待由executeScript JS创建的新DIV的存在.

The only pause is when Selenium waits for the existence of the new DIV that is created by our executeScript JS.

这篇关于Selenium + Node.js:是否可以监听重复发生的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆