我有一个文本字段,当用户输入一些字符时,会出现一个集团并显示一些结果建议。我想要数一下建议的数量,但我不知道怎么做。
我可以通过CasperJS API输入一些字符,但不可能获得出现的自动完成块的内容。 ( “#UI-ID-6”)
我的页面测试: http://www.spareka.fr/pieces_detachees_accessoires_electromenager
我想要测试的形式是在左上角的“Recherchedétaillée”中。所以,我必须为第一个下拉字段选择一个值(“Quel type d'appareil”),然后在第二个下拉字段中选择一个值(“Quelle marque?”)。 因此,第三个块“Quelleréférenced'appareil”变为活动状态,这是我想测试的自动完成字段。
我的示例代码:( 10月18日更新)
var casper = require("casper").create({});
casper.on("page.error", function(msg,trace){ //No message in the shell
this.echo("error message : " + msg, "ERROR");
});
casper.on("remote.message", function(msg,trace){ //No message in the shell
this.echo("remote message : " + msg);
});
casper.on("ressource.error", function(ressource){ //No message in the shell
this.echo("ressource message errorCode : " + ressource.errorCode);
this.echo("ressource message errorString : " + ressource.errorString);
this.echo("ressource message errorUrl : " + ressource.url);
this.echo("ressource message errorID : " + ressource.id);
});
casper.start("http://www.spareka.fr/pieces_detachees_accessoires_electromenager", function(){
// The first select "Quel type d'appareil"
this.then(function(){
this.fillSelectors("form#search_form_4", {
'select[id="finalProductType"]': 'Appareil à Fondue',
}, true);
});
// No problem here
// The second select "Quelle marque?"
this.then(function(){
this.fillSelectors("form#search_form_4", {
'select[id="manufacturer"]': 'TEFAL',
}, true);
});
// No problem here
this.then(function(){
this.sendKeys('#productReference', '*', {keepFocus: true}); //so, an element "ui-id-6" appear and in firebug, there are the suggestions of results
this.capture("test88_screen01.png");
//try with wait...
/*
this.wait(2000, function then(){ //error : No element matching selector found : #ui-id-6
this.echo(this.getHTML("#ui-id-6").length); // wall of text
});
*/
this.waitUntilVisible("#ui-id-6", function(){ // error : Wait timeout of 5000ms expired, existing
this.echo(this.getHTML("#ui-id-6").length);
});
this.echo(this.getHTML("#ui-id-6").length); //selector found (no error), but return 0
});
this.then(function(){
this.capture("test88_screen02.png"); // very strange ! this screen shows the page after submitting the form, I don't understand, I didn't understand why the form is submited
this.echo("end");
});
});
casper.run();
我的casper版本:1.1.0-beta3和phantomjs:1.9.7
答案 0 :(得分:1)
如果您使用其then
回调,则等待有效。所有then*
和wait*
casper步骤函数都是异步的。这意味着在步骤内部,您不应该在其他异步函数之后调用同步函数。
this.then(function(){
this.sendKeys('#productReference', '*', {keepFocus: true}); //so, an element "ui-id-6" appear and in firebug, there are the suggestions of results
this.capture("screen01.png");
//try with wait...
this.wait(2000, function then(){
this.echo(this.getHTML("#ui-id-6")); // wall of text
});
// or
this.waitUntilVisible("#ui-id-6", function(){
this.echo(this.getHTML("#ui-id-6"));
});
});