2014-02-24 26 views
8

我做這個小測試:casper.then不等待我的指令結束執行下一步

casper.test.begin('Test', function() { 
casper.start(); 

casper.then(function() { 
    casper = this; 
    setTimeout(function(casper) { 
     casper.echo('wait 5s'); 
    }, 5000); 
}); 

casper.then(function() { 
    this.echo('should appear after 5s'); 
}); 

casper.run(function() { 
    this.test.done(); 
}); 
}); 

當我執行這個測試,我的控制檯顯示僅此行「應該出現5秒後「但不是第一句,實際上第二個'那麼'不等5秒。

這是一個巨大的問題,因爲它是我的casperjs測試套件中許多隨機失敗的可能原因。

也許我必須使用異步(與系列)來執行每個步驟。

你有這個問題嗎?在casperjs測試中一個接一個地執行一些javascript函數的最佳做法是什麼?

回答

6

這裏嘗試使用wait()

casper.then(function() { 
    casper = this; 
    casper.echo('wait 5s'); 
}); 

casper.then(function() { 
    casper.wait(5000, function() { 
     this.echo('should appear after 5s'); 
    }); 
}); 
+0

這只是一個例子,不要忘記,你永遠不必等待(一段時間)。它對UI測試不敏捷,你必須等待特定的事件,這裏的目的是強調casper.then函數的失敗和不穩定性。我放棄了這個函數的時間,因爲它不能等待我的函數的結果,我必須使用另一種方法,我會告訴你 –

0

「什麼在casperjs測試執行後,其他一些JavaScript函數一個最好的做法是什麼?」

這裏怎麼我做的:

login.js

casper.login = function (id,password) { 
    casper.then(function(){ //or this.then(...) 
     this.test.comment('----------------Connexion Only ! : --------------------'); 
     if (!this.visible('#div_boxhover')) { 
      this.echo('Connexion box not visible before mouse on it!'); 
     } 
     this.mouse.move(x('//*[@id="identification"]')); 
     if (this.visible('#div_boxhover')) { 
      this.echo('Connexion box visible after mouse on it!'); 
     } else { this.echo("FAIL : Connexion box doesn't appear");} 
     //this.echo("I can see the logo"); 
     this.fill(x('//*[@id="div_boxhover"][1]'), { 
       login: id, 
       pass: password 
      }, false); 
     this.click(x('//*[@name="log_in"]')); 
     this.waitForSelector(('.boxValid'), function() { 
       this.test.assertSelectorHasText('.boxValid', 'Vous êtes identifié en tant que Membre !'); 
       this.test.assertTextExists('Succès', 'Page contains "succès" : so identification done');  
      }); 
    }); 
}; 

scenario1.js

var x = require('casper').selectXPath; 

phantom.injectJs('C:/bin/Aptana_casperjs/ccm/_functions/login.js'); 

casper.test.begin('\n********* Stack title : ***********\n', 3 , function suite(test) { 
    casper.start(yourUrl,function(){ 
     //check one main element in the page 
     this.test.assertExists('.search','Search toolbar present'); 
    }) 
    //set a viewport (for slimerJS) 
    .viewport(1200,800) 
    //call an external function 
    .then(function() { 
     casper.login("pseudo","password");//this.login("pseudo","password"); 
    }) 
    //then click on a link + fill the form which appears 
    .thenClick('div.colMiddle > a.button', function(){ 
     this.fillSelectors('form#frmqa', { 
       'select[name="cat"]' : "2", 
       'input[name="titre"]' : title, 
       'textarea[name="message"]' : message 
       }, false); 
     this.click('input#submitqa'); 
    }) 
    //wait for the redirection after a click at the end of a step 
    .waitForText(topic, function() { 
     this.test.assertSelectorHasText('.boxTitle', 'Mes discussions suivies', "New topic in 'Mes dicussions suivies'"); 
     this.test.assertExists('a[actid="shqafrm"].button','Button answer topic present'); 
    }) 
    //click on a new link 
    .thenClick('a[actid="shqafrm"].button', function(){ 
     //wait for the textarea to appear 
     this.waitForSelector('textarea[name="message"]',function(){ 
      //send message and submit 
      this.sendKeys('textarea[name="message"]', newPost); 
      this.click('input#submitqa'); 
     }); 
    }) 
    //check redirection or validation after a click on button/form/link 
    .waitForSelector('.article',function(){ 
     test.assertTextExists(newPost, 'Answer in topic ' + title + ' done'); 
    }) 
    .run(function() { 
      this.test.comment('----------------------- Stack title over ------------------------\n'); 
      test.done(); 
    }); 
}); 

所以我就打電話給我的功能於一身的新的一步。