自定义casperjs模块 [英] Custom casperjs modules

查看:71
本文介绍了自定义casperjs模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将casperjs扩展为使用一些像这样的新方法:

  casper .getTxt = function(selector){
if(this.exists(selector)){
返回this.getHTML(selector);
}
else {
return’’;
}
};

我必须在编写的每个脚本上添加这些功能。



所以我在其他模块所在的相同位置创建了一个新文件 custom.js colorizer.js mouse.js 等)。
custom.js具有以下代码:

  var require = patchRequire(require); 
var casper = require(‘casper’)。create();

var getTxt = function(selector){
if(casper.exists(selector)){
return casper.getHTML(selector);
}
else {
return’’;
}
};

exports.getTxt = getTxt;

在我的脚本中,我已经:



< pre class = lang-js prettyprint-override> var cust = require('custom');
this.echo(cust.getTxt(’a’));

但是我遇到了错误: Casper没有启动,可以吗? t execute exist()



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

解决方案

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



请参见下文或我如何将我的casperjs脚本的一部分变成一个函数,以便可以多次使用它



您可以使用自定义方法制作脚本,您不需要制作其他模块。 :
ex: functions.js

  casper.getTxt = function(selector){
if(this.exists(selector)){
返回this.getHTML(selector);
}
else {
return’’;
}
};

  var getTxt = function(selector){
if(casper.exists(selector)){
return casper.getHTML(selector);
}
else {
return’’;
}
};

然后在您的主脚本中调用此脚本:



main.js

  phantom.injectJs( functions.js); //注入脚本
/ **
*开始场景
* /

casper.test.begin('\n ****** *********计划的测试套件:场景1 ************** \n',1,功能套件(测试){
/ **
* start:初始化并打开第一页
* /
casper.start('yourUrl',function(){
//现在可以调用自定义方法
this.echo(this.getTxt('a')); //或this.echo(getTxt('a'))如果正常功能
this.echo(this.getTitle());
this.echo('Adresse:'+ this.getCurrentUrl()+'\n');
});

/ **
*添加新
* /
casper.then(function(){
this.test.comment('-------------步骤1- -----------:');
//this.echo(\"step 1);
});

/ **
*在堆栈中添加第二步
* /
casper.then(function(){
this.test.comment('---------- - - 第2步 - -----------:’);
//this.echo(\"step 2);
var _x = require('casper')。selectXPath;
this.test.assertExists(_x(’// * [@ role = banner]’),标题存在);
});


/ **
* run()执行它们(步骤):
* /
casper.run(function(){
this.test.comment('----------------为场景1完成的每个步骤---------------- \n' );
//test.done()->执行完每个步骤后,场景结束,有关测试的反馈
test.done();
});

如果要导出该节点,例如:



custom.js

  var getTxt = function(selector){
if(casper.exists(selector)){
return casper.getHTML(selector);
}
else {
return’’;
}
};

exports.getTxt = getTxt;

有要求:

  var cust = require('custom'); 

/ **
*开始
* /

casper.test.begin('\n ********* ******计划的测试套件:场景1 ************** \n',1,功能套件(测试){
/ **
* start:打开第一个URL
* /
casper.start('yourUrl',function(){
this.echo(cust.getTxt('a'));
this.echo('Adresse:'+ this.getCurrentUrl()+'\n');
});
casper.run(function(){
this.test .comment('----------------为方案1完成的每个步骤---------------- \n');
//test.done()->执行完每个步骤后,场景结束,有关测试的反馈
test.done();
});

另请参见: https: //gist.github.com/n1k0/3813361


I've extended my casperjs to use some new methods like this one :

casper.getTxt = function(selector) {
    if(this.exists(selector)) {
        return this.getHTML(selector);
    }
    else {
        return '';
    }
};

I've to add these functions on every script that I write.

so I made a new file custom.js on the same location where other modules(colorizer.js, mouse.js etc) were placed. custom.js has following code :

var require = patchRequire(require);
var casper = require('casper').create();

var getTxt = function(selector) {
    if(casper.exists(selector)) {
        return casper.getHTML(selector);
    }
    else {
        return '';
    }
};

exports.getTxt = getTxt;

In my script, I've :

var cust = require('custom');
this.echo(cust.getTxt('a'));

But I'm getting the error : Casper is not started, can't execute exists()

What am I doing wrong? What's the correct way of reusing casperjs code?

解决方案

It's because you haven't initialized your first webpage with the start() method (I think). You might try to get back 'a' HTML from nothing, you have to specify the first page.

See below or how can i turn part of my casperjs script into a function so i can use it multipul times

You could just make a script with your custom methods, you don't need to make another module. : ex : functions.js

casper.getTxt = function(selector) {
    if(this.exists(selector)) {
        return this.getHTML(selector);
    }
    else {
        return '';
    }
};

or

var getTxt = function(selector) {
    if(casper.exists(selector)) {
        return casper.getHTML(selector);
    }
    else {
        return '';
    }
};

Then in your main script call this script :

main.js

phantom.injectJs("functions.js"); //inject your script
    /**
     *  Begin a scenario
     */

casper.test.begin('\n*************** Suite of planned test : scenario 1 **************\n', 1, function suite(test) {
    /**
     * start : initialize and open the first page
     */
    casper.start('yourUrl', function() {
        //now you can call your custom methods  
        this.echo(this.getTxt('a')); //or this.echo(getTxt('a')) if normal function
        this.echo(this.getTitle());
        this.echo('Adresse : ' + this.getCurrentUrl() + '\n');
    });

    /**
     * add a new step in the stack 
     */
    casper.then(function () {
        this.test.comment('------------- step 1 ------------- : ');
        //this.echo("step 1");
        });

    /**
     * add a second step in the stack 
     */
    casper.then(function () {
        this.test.comment('------------- step 2 ------------- : ');
        //this.echo("step 2");
        var _x = require('casper').selectXPath;
        this.test.assertExists(_x('//*[@role="banner"]'),'header present');
    });


    /**
     *  run() executes them (steps): 
     */
    casper.run(function() {
        this.test.comment('---------------- Every steps done for scenario 1 ----------------\n');
        //test.done() -> when every steps executed, scenario over, feedback about tests
        test.done();
    });

If you want to export it nodeLike :

custom.js

var getTxt = function(selector) {
    if(casper.exists(selector)) {
        return casper.getHTML(selector);
    }
    else {
        return '';
    }
};

exports.getTxt = getTxt;

With a require :

var cust = require('custom');

    /**
     *  Begin
     */

casper.test.begin('\n*************** Suite of planned test : scenario 1 **************\n', 1, function suite(test) {
    /**
     * start : open the first url
     */
    casper.start('yourUrl', function() {
        this.echo(cust.getTxt('a'));
        this.echo('Adresse : ' + this.getCurrentUrl() + '\n');
    });
    casper.run(function() {
        this.test.comment('---------------- Every steps done for scenario 1 ----------------\n');
        //test.done() -> when every steps executed, scenario over, feedback about tests
        test.done();
    });

See also : https://gist.github.com/n1k0/3813361

这篇关于自定义casperjs模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆