简体   繁体   中英

Protractor: How to wait for executeAsyncScript to finish?

I have the following function with executeAsyncScript :

this.playVideoAtIndex = function(index) {
    return browser.executeAsyncScript((index) => {
        let video = document.querySelectorAll('#video');
            video.oncanplay = () => {
                video.play();
            };
    }, index);
}

I want to run something only after it finished, but it doesn't seem to return a promise that resolves after the script was excuted.

You need to add callback as your last argument, and call it:

this.playVideoAtIndex = function(index) {
    return browser.executeAsyncScript((index, callback) => {
        let video = document.querySelectorAll('#video');
            video.oncanplay = () => {
                video.play();
                callback();
            };
    }, index);
}

You need to wrap your function into Protractor's control flow , so it's executed in the main Protractor asynchronous queue:

this.playVideoAtIndex = function(index) {
  return browser.controlFlow().execute(function() {
    browser.executeAsyncScript((index) => {
      let video = document.querySelectorAll('#video');
        video.oncanplay = () => {
            video.play();
        };
      }, index);
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM