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

所以我就打电话给我的功能于一身的新的一步。