Custom casperjs modules

和自甴很熟 提交于 2019-12-02 05:31:36

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23870978/custom-casperjs-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!