gpt4 book ai didi

javascript - 守夜人 : Is there a way to find out if executeAsync has executed the javascript?

转载 作者:搜寻专家 更新时间:2023-11-01 04:14:41 25 4
gpt4 key购买 nike

我正在使用 Nightwatch 编写浏览器自动化程序。我遇到 Nightwatch 命令的 executeAsync 函数问题。

executeAsync 的 Nightwatch 文档:

Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be asynchronous and the result of evaluating the script is returned to the client.

Asynchronous script commands may not span page loads. If an unload event is fired while waiting for a script result, an error should be returned to the client.

this.demoTest = function (browser) {
browser.executeAsync(function(data, done) {
someAsyncOperation(function() {
done(true);
});
}, [imagedata], function(result) {
// ...
});
};

最后一个可选参数应该是一个函数,当异步任务完成时被调用。

如何检查异步任务是否已开始执行?我想在异步任务的 Javascript 主体被浏览器执行后立即执行一些操作。有没有办法查明 executeAsync 是否已开始在 Nightwatch 代码中执行?

最佳答案

executeAsync 调用保持同步,并且在执行流中表现得像 execute。要异步执行一些代码,您首先需要使用 execute 启动脚本,然后使用 executeAsync 等待结果。

这是一个例子:

'Demo asynchronous script' : function (client) {
client.timeoutsAsyncScript(10000);
client.url('http://stackoverflow.com/');

// execute a piece of script asynchroniously
client.execute(function(data) {
window._asyncResult = undefined;
setTimeout(function(){
window._asyncResult = "abcde";
}, 2000);
}, ["1234"]);

// execute a task while the asynchroniously script is running
client.assert.title('Stack Overflow');

// wait for the asynchronous script to set a result
client.executeAsync(function(done) {
(function fn(){
if(window._asyncResult !== undefined)
return done(window._asyncResult);
setTimeout(fn, 30);
})();
}, [], function(result) {
// evaluate the result
client.assert.equal(result.value, "abcde");
});

client.end();
}

关于javascript - 守夜人 : Is there a way to find out if executeAsync has executed the javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37044543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com