“什么在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();
});
});
所以我就打电话给我的功能于一身的新的一步。
这只是一个例子,不要忘记,你永远不必等待(一段时间)。它对UI测试不敏捷,你必须等待特定的事件,这里的目的是强调casper.then函数的失败和不稳定性。我放弃了这个函数的时间,因为它不能等待我的函数的结果,我必须使用另一种方法,我会告诉你 –