Hi all,
I am creating a monitor on Synthetics. I need to execute some front end javascript using webdriver.executeScript. The code I need to run is asynchronous so I need to wait for the result of a asynchronous task and send it back to the webdriver context. I am using the Promise interface to achieve this. but in all the cases when I grab the result in the webdriver context I get an empty object.
This is pretty much what I need to do:
$browser.executeScript(function(){
var execute = new Promise(function(resolve, reject) {
// Set interval every n seconds during m seconds and on every
var timer = 0;
var maxTime = 5;
// Run the interval
var interval = window.setInterval(function(){
// Async code ... and then when I get the end result I fulfil the promise
if (timer == maxTime) {
resolve({test:true});
window.clearInterval(interval);
}
timer++;
},1000);
});
return execute.then(function(val){
return val;
}).catch(function(reason){
return reason;
});
}).then(function(val){
// Assert val, etc
console.log(val);
// it logs {} even if I resolve a string, etc.
});
If I do not use promise and I just return any other type such as plain object or string, it works. The thing is I do need to use Promise because I need to execute async code and then return the final result.
Does anybody know why it is not working?
Thanks.
UPDATE
I was able so solve the by problem calling executeAsyncScript in liue of executeScript.