Custom casperjs modules

自作多情 提交于 2019-12-02 02:11:17
Fanch

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

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