【问题标题】:How to take screenshots inside execute or executeAsync using Intern如何使用实习生在执行或执行异步中截取屏幕截图
【发布时间】:2017-09-22 13:27:17
【问题描述】:

在我正在进行的这个测试中,我使用executeAsync() 来捕捉正在测试的页面的一些事件。我不知道将要发生的事件的数量,对于捕获的每一个事件,我都想用.takeScreenshot() 截屏。目前,我正在使用此代码:

return this.parent
    .setExecuteAsyncTimeout(5000)
    .executeAsync(function(done) {
        window.events = function(event){
            if(event.message == 'takeScreenshot'){
                return done(event);
            } else if(event.message == 'endTest'){
                return done(event);
            }
        };
    }, [])
    .then(function(event){
         return this.parent
             .takeScreenshot()
             .then(function(data) {
                 //previously defined array to save the screenshots
                 bufferArray.push(data);
             })
    .end();
})

这段代码可以运行,但问题是它只截取一个屏幕截图,因为它只捕获第一个事件,然后完成测试,而不是等待其他事件。谁能告诉我是否可以在.executeAsync() 中调用.takeScreenshot() 而不是返回回调完成(事件)?

【问题讨论】:

    标签: javascript intern leadfoot


    【解决方案1】:

    takeScreenshot 方法不能从executeAsync 中调用,所以你需要做些别的事情。如果没有 async/await,递归可能是最直接的解决方案,像这样(未经测试):

    function takeScreenshots() {
        return this.parent
            // Wait for a takeScreenshot or endTest event
            .executeAsync(function (done) {
                window.events = function (event) {
                    if (
                        event.message === 'takeScreenshot' ||
                        event.message === 'endTest'
                    ) {
                        done(event);
                    }
                };
            })
            .then(function (event) {
                // If the event was a takeScreenshot, take it and then
                // call takeScreenshots again to wait for another
                // event.
                if (event.message === 'takeScreenshot') {
                    return this.parent
                        .takeScreenshot()
                        .then(function (data) {
                            bufferArray.push(data);
                        })
                        .then(takeScreenshots);
                }
            });
    }
    
    return this.parent
        .setExecuteAsyncTimeout(5000)
        .then(takeScreenshots);
    

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多