2016-08-12 9 views
0

属性の変更をページで確認するのに問題がありましたが、やりました。 今私のメインのコードは次のようになります。if文の後にアクションを実行するCasperJS

casper.thenOpen('pageurl', function() { 
    this.then(function() { 
     function checkReload() 
     { 
      var cur = this.getElementAttribute("classname", "attr"); 
      if (cur == "target") {here 
       return; // finished 
      } 
      else 
      { 
       this.echo(cur); 
      } 
      this.reload(); 
      this.wait(1, checkReload); // check again in a second 
     } 
     this.then(checkReload);   
    }).thenEvaluate(function(){ 
     this.click("#new_add"); 
     this.echo("done567"); 
     this.then.waitForSelector(".cartGdList", 
      function pass() { 
       this.click("..."); 
       this.echo("done8"); 
       this.then(function() { 
        this.waitForSelector(".cart_s_Box", 
         function pass() { 
          this.click("#js_upFormBtn"); 
          this.echo("step4"); 
          var end = new Date().getTime(); 
          var time = end - start; 
          this.echo('time: ' + time + 'ms*'); 
         }, 
         function fail() { 
          this.echo('fail'); 
         } 
        ); 
       }); 
      }, 
      function fail() { 
       this.echo('fail'); 
      } 
     ); 
    }); 
}); 

そして文がOKであるかどう後if(cur == "target") 私はこのコードを実行したい:私はこれを行うことができ、私はこれを入れてみましたどのように

this.click("#new_add"); 
this.echo("done567"); 
this.then.waitForSelector(".cartGdList", 
    function pass() { 
     this.click("..."); 
     this.echo("done8"); 
     this.then(function() { 
      this.waitForSelector(".cart_s_Box", 
       function pass() { 
        this.click("#js_upFormBtn"); 
        this.echo("step4"); 
        var end = new Date().getTime(); 
        var time = end - start; 
        this.echo('time: ' + time + 'ms*'); 
       }, 
       function fail() { 
        this.echo('fail'); 
       } 
      ); 
     }); 
    }, 
    function fail() { 
     this.echo('fail'); 
    } 
); 

thenEvaluateにありますが、これは機能しません。

答えて

0

あなたは多くの不必要なネスティングを持っていますが、まずは最初のものがまずあります。あるコードをif -branchで実行したいとします。 [OK]をクリックし、そのコードをif -branchに入れます。

インデントをあまり必要としない場合は、コードを関数に入れることもできます。

function myIfBranch(){ 
    this.click("#new_add"); 
    this.echo("done567"); 
    // ... 
} 

casper.thenOpen('pageurl', function() { 
    this.then(function() { 
     function checkReload() 
     { 
      var cur = this.getElementAttribute("classname", "attr"); 
      if (cur == "target") { 
       myIfBranch.call(this); 
       return; // stop recursion 
      } 
      else 
      { 
       this.echo(cur); 
      } 
      this.reload(); 
      this.wait(1, checkReload); // check again in a second 
     } 
     this.then(checkReload); 
     // ... 

不必要なネスティングについては、 then*wait*関数は実際には非同期ステップ関数であり、両方とも同じ非同期式で動作することに注意してください。


function myIfBranch(){ 
    this.click("#new_add"); 
    this.echo("done567"); 
    this.waitForSelector(".cartGdList", 
     function pass() { 
      this.click("..."); 
      this.echo("done8"); 
      this.waitForSelector(".cart_s_Box", 
       function pass() { 
        this.click("#js_upFormBtn"); 
        this.echo("step4"); 
        var end = new Date().getTime(); 
        var time = end - start; 
        this.echo('time: ' + time + 'ms*'); 
       }, 
       function fail() { 
        this.echo('fail'); 
       } 
      ); 
     }, 
     function fail() { 
      this.echo('fail'); 
     } 
    ); 
} 

casper.thenOpen('pageurl', function() { 
    function checkReload() { 
     var cur = this.getElementAttribute("classname", "attr"); 
     if (cur == "target") { 
      myIfBranch.call(this); 
      return; // finished 
     } 
     else 
     { 
      this.echo(cur); 
     } 
     this.reload(); 
     this.wait(1, checkReload); // check again in a second 
    } 
    this.then(checkReload); 
this casper.evaluateの内側と casper.thenEvaluateはなく、 windowに、 casperを参照していないことに注意してください:ここでは、クリーンアップの例です。単に window.click, window.echoまたは window.thenの機能はありません。また、 this.then.waitForSelector(...)は意味をなさない。

+0

このコードの後に​​ – kaska3er

+0

さんの完全なソリューションありがとうございます。 – kaska3er

+0

'this.click( "#js_upFormBtn");' 'this.echo( "step4");' 実行機能の表示時間がありますが、エコー時間はありません – kaska3er

関連する問題