Phantomjs 自定义casperjs模块

Phantomjs 自定义casperjs模块,phantomjs,casperjs,Phantomjs,Casperjs,我已经扩展了casperjs,使用了一些新方法,比如: casper.getText=函数(选择器){ 如果(此.存在(选择器)){ 返回此.getHTML(选择器); } 否则{ 返回“”; } }; 我必须在我编写的每个脚本上添加这些函数 因此,我在放置其他模块(colorizer.js,mouse.js等)的同一位置创建了一个新文件custom.js。 custom.js包含以下代码: var require=patchRequire(require); var casper=requi

我已经扩展了casperjs,使用了一些新方法,比如:

casper.getText=函数(选择器){
如果(此.存在(选择器)){
返回此.getHTML(选择器);
}
否则{
返回“”;
}
};
我必须在我编写的每个脚本上添加这些函数

因此,我在放置其他模块(
colorizer.js
mouse.js
等)的同一位置创建了一个新文件
custom.js
。 custom.js包含以下代码:

var require=patchRequire(require);
var casper=require('casper').create();
var getText=函数(选择器){
if(casper.exists(选择器)){
返回casper.getHTML(选择器);
}
否则{
返回“”;
}
};
exports.getText=getText;
在我的脚本中,我:

var cust=require('custom');
this.echo(cust.getText('a'));
但是我得到了一个错误:
Casper没有启动,无法执行exists()

我做错了什么?
重用casperjs代码的正确方法是什么?

这是因为您没有使用start()方法初始化第一个网页(我想)。 您可能会尝试从空白处获取“a”HTML,您必须指定第一页

见下文或

您只需使用自定义方法创建脚本,无需创建其他模块: 例如:
functions.js

casper.getText=函数(选择器){
如果(此.存在(选择器)){
返回此.getHTML(选择器);
}
否则{
返回“”;
}
};

var getText=函数(选择器){
if(casper.exists(选择器)){
返回casper.getHTML(选择器);
}
否则{
返回“”;
}
};
然后在主脚本中调用以下脚本:

main.js

phantom.injectJs(“functions.js”)//注入脚本
/**
*开始一个场景
*/
casper.test.begin('\n*******************计划测试套件:场景1*********************\n',1,功能套件(测试){
/**
*开始:初始化并打开第一页
*/
casper.start('yourUrl',function(){
//现在可以调用自定义方法了
this.echo(this.getText('a'));//或者this.echo(getText('a'))如果函数正常
this.echo(this.getTitle());
this.echo('address:'+this.getCurrentUrl()+'\n');
});
/**
*在堆栈中添加新步骤
*/
casper.then(函数(){
this.test.comment('--------------步骤1---------');
//本文件为echo(“第1步”);
});
/**
*在堆栈中添加第二步
*/
casper.then(函数(){
this.test.comment('--------------步骤2---------');
//这是echo(“第2步”);
var_x=require('casper')。选择XPath;
this.test.assertExists(x('/*[@role=“banner”]'),'header present');
});
/**
*run()执行它们(步骤):
*/
casper.run(函数(){
this.test.comment('--------------为场景1完成的每个步骤-------------------\n');
//test.done()->执行每个步骤时,场景结束,有关测试的反馈
test.done();
});
如果要将其像节点一样导出:

custom.js

var getText=函数(选择器){
if(casper.exists(选择器)){
返回casper.getHTML(选择器);
}
否则{
返回“”;
}
};
exports.getText=getText;
要求:

var cust=require('custom');
/**
*开始
*/
casper.test.begin('\n*******************计划测试套件:场景1*********************\n',1,功能套件(测试){
/**
*开始:打开第一个url
*/
casper.start('yourUrl',function(){
this.echo(cust.getText('a'));
this.echo('address:'+this.getCurrentUrl()+'\n');
});
casper.run(函数(){
this.test.comment('--------------为场景1完成的每个步骤-------------------\n');
//test.done()->执行每个步骤时,场景结束,有关测试的反馈
test.done();
});

另请参见:

这是因为您尚未使用start()方法初始化第一个网页(我认为)。 您可能会尝试从空白处获取“a”HTML,您必须指定第一页

见下文或

您只需使用自定义方法创建脚本,无需创建其他模块: 例如:
functions.js

casper.getText=函数(选择器){
如果(此.存在(选择器)){
返回此.getHTML(选择器);
}
否则{
返回“”;
}
};

var getText=函数(选择器){
if(casper.exists(选择器)){
返回casper.getHTML(选择器);
}
否则{
返回“”;
}
};
然后在主脚本中调用以下脚本:

main.js

phantom.injectJs(“functions.js”);//注入脚本
/**
*开始一个场景
*/
casper.test.begin('\n*******************计划测试套件:场景1*********************\n',1,功能套件(测试){
/**
*开始:初始化并打开第一页
*/
casper.start('yourUrl',function(){
//现在可以调用自定义方法了
this.echo(this.getText('a'));//或者this.echo(getText('a'))如果函数正常
this.echo(this.getTitle());
this.echo('address:'+this.getCurrentUrl()+'\n');
});
/**
*在堆栈中添加新步骤
*/
casper.then(函数(){
this.test.comment('--------------步骤1---------');
//本文件为echo(“第1步”);
});
/**
*在堆栈中添加第二步
*/
casper.then(函数(){
this.test.comment('--------------步骤2---------');
//这是echo(“第2步”);
var_x=require('casper')。选择XPath;
this.test.assertExists(x('/*[@role=“banner”]'),'header present');
});
/**
*