Custom casperjs modules

2020-04-14 08:16发布

问题:

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