2017-04-06 18 views
0

JSファイルにあるTakeScreenshot関数を記述しました。base.jsファイルと呼ばれるスクリーンショットを保存し、スクリーンショットのパスを返します。エラーの取得mesasge "this.TakeScreenshotは分度器の関数ではありません"

this.TakeScreenshot = function(screenShotName) { 
//some stuff..... 
     browser.takeScreenshot().then(function (png) { 
      var stream = fs.createWriteStream(screenshotPath); 
      stream.write(new Buffer(png, 'base64')); 
      stream.end(); 
    }); 
     return screenshotPath; 
    }; 

I am calling the above function in another function called "isTruelyPresent". This function will take a screenshot if element is present/not present on UI. The implementation is as below: 

this.isTrulyPresent = function(elementToCheckVisibilityOf, element2) { 
      return elementToCheckVisibilityOf.isDisplayed().then(function (isDisplayedd) { 
      var myObj; 
      myObj = this.TakeScreenshot(element2); 
      // some stuff 
      console.log('isDisplayed'+isDisplayedd); 
      return isDisplayedd; 
    }).then(null, function (error) { 
      var myObj; 
      myObj = this.TakeScreenshot("ErrorSS"); 
      console.log('A NoSuchElement exception was throw because the element is NOT displayed so we return false'); 
      return false; 
     }); 
    }; 


Protractor script is throwing an error "this.TakeScreenshot is not a function". Could any one please help me to resolve the issue. 
+0

注:非同期プログラミングでisTrulyPresent関数が同じJSファイル(base.js) –

+0

に存在する場合、コールバックメソッドで 'this'キーワードを使用することはできません。その代わりに、関数スコープ内に新しい変数を宣言し、新しく作成された変数に 'this'を割り当てます。あなたのケースでは、 'isTrulyPresent'メソッドの中で' var self = this; 'のような変数を作り、' self.TakeScreenshot() 'を使ってメソッドを呼び出します。 –

+0

ありがとう@SudharsanSelvaraj ..あなたの助けが大変ありがとうございます。私はあなたの解決策を試し、私は何か問題がある場合はあなたに戻って取得します。ありがとう、たくさんの友達。 –

答えて

0

まずはisPresent(チェック要素がDOMに存在する場合)およびisDisplayed(チェック要素がDOMに存在し、表示されている場合)との間に差があることに注意してください。要素がDOMに存在しない場合は、isDisplayed()メソッドが失敗します。あなたは今、このエラーをキャッチしています、これもあなたが望むものですか?

第2に、それぞれfunctionは、それ自身のthisスコープを持つことができます。つまり、this.TakeScreenshot("ErrorSS")には、参照するthisの代わりにfunction(error){}thisスコープへの参照が含まれている可能性があります。

これは;-) docがこれを修正する際に役立つ場合があります。私が通常使っているのは、arrow関数です。リンクを参照してください。

+0

おかげで友達もありがとう。ドキュメントにはさらに詳しい情報があります。間違いなくそれは私を助けます。 –

+0

あなたの問題を解決しましたか? – wswebcreation

+0

いいえ、@ wswebcreation。私は運がない:( –

関連する問題