1

在我正在进行的这个测试中,我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();
})

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

4

1 回答 1

0

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);
于 2017-09-25T12:44:59.763 回答