2017-09-22 1 views
1

私が取り組んでいるこのテストでは、executeAsync()を使用してテスト対象のページのイベントをキャッチしています。私は起こる予定のイベントの数を知りません、そして、それが捕まえられたそれぞれについて、.takeScreenshot()でスクリーンショットを撮りたいと思います。現在、私はこのコードを使用しています:internを使用してexecuteまたはexecuteAsync内でスクリーンショットを取る方法

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(); 
}) 

このコードは機能していますが、問題はそれだけで最初のイベントをキャッチするので、それが唯一のスクリーンショットを取ることで、その後、代わりに他のイベントを待っているのテストを終了します。コールバックの完了(イベント)を返すのではなく.executeAsync()の中の.takeScreenshot()に電話をかけることができるか誰にでも教えてください。

答えて

0

takeScreenshotメソッドはexecuteAsyncから呼び出すことができないため、別の処理が必要です。非同期/待機がなければ、再帰はおそらく最も簡単な解決策です(テストされていない)。

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); 
関連する問題