将getFormElement的输出与CasperJs中的字符串进行比较

时间:2013-07-18 22:36:47

标签: phantomjs casperjs

我一直在尝试测试网页以查看功能是否有效。我试图这样做的方法是将getFormValues的输出与一个字符串进行比较,如果功能确实有效。我写了以下任务来检查:

casper.then(function seeifsaveworked() {
    if (this.getFormValues('.tf-field-inner') === 'foobar') {
        this.echo("SUCCESS: The site description has been successfully changed, so the save did work", 'INFO');
    } else {
        this.echo("ERROR: The save attempt didn't work successfully, something is wrong with your connection or the page isn't functioning properly.", "ERROR");
        this.die();
    };
});

当我运行脚本时,我得到一个解析错误,因为关于该代码的某些内容在语法上是正确的。我不知道为什么它不对,如果有人可能解决语法错误以及我需要做的事情来完成原始任务,这绝对是非常棒的。

由于

1 个答案:

答案 0 :(得分:0)

我认为不正确的语法是else语句可能出错后的分号。

casper.then(function seeifsaveworked() {
    if (this.getFormValues('.tf-field-inner') === 'foobar') {
        this.echo("SUCCESS: The site description has been successfully changed, so the save did work", 'INFO');
    } else {
        this.echo("ERROR: The save attempt didn't work successfully, something is wrong with your connection or the page isn't functioning properly.", "ERROR");
        this.die();
    };  <----------- Remove this.
}); 

至于检查表单,casper的文档显示了这个:

casper.start('http://www.google.fr/', function() {
    this.fill('form', {q: 'plop'}, false);
    this.echo(this.getFormValues('form').q); // 'plop'
});

我认为您需要更改的是,当您访问表单值时,您需要指定要比较的字段并确保选择表单。

if(this.getFormValues('form#formid').name === 'foobar')...