2017-01-15 105 views
0

我有以下功能與executeAsyncScript量角器:如何等待executeAsyncScript完成?

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

我想運行後,才完成了一些東西,但它似乎並沒有返回腳本excuted後能解決的承諾。

回答

1

您需要添加callback作爲最後一個參數,並將其命名爲:

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

你需要用你的函數爲Protractor's control flow,所以它在主量角器異步隊列執行:

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