1

Okay, so here is a part of my casperjs script below which works fine

if(casper.exists(ac1)){

    var uel = "https://example.ws/send.html?f=1099817";
    this.thenOpen(uel, function() {
        casper.wait(10000, function() {    
            casper.then(function() {
                this.evaluate(function() {
                    var amount = 0.29
                    var result = amount * 0.019
                    var result2 = result.toFixed(6);
                    var fresult = amount - result2;
                    var needed = fresult.toFixed(3);

                    document.getElementById('account').value = 'ydfg028';
                    document.getElementsByName('data')[0].value = needed;

                });

                this.click("input#sbt.button[type='submit']");

                casper.wait(10000, function() {
                    casper.then(function() {
                        this.capture("filenadfgmedsfg.jpg");
                        var el2 = this.getHTML();
                        fs.write('results23.html', el2, 'w');
                    });
                });
            });
        });
    });

} else {
    this.exit();
}

The problem I have is over 14 of the following statements

if(casper.exists()){

So what I am trying to do, is use the casperjs steps as a function. This is what I have tried below, but it just does nothing and casperjs ends when it reaches the function. Here's what I am trying

This is the casperjs function I have made

function casperstep(amount, user, location) {

    var uel = "https://example.ws/send.html?f=" + location;
    this.thenOpen(uel, function() {
        casper.wait(10000, function() {    
            casper.then(function() {
                this.evaluate(function() {

                    var result = amount * 0.019
                    var result2 = result.toFixed(6);
                    var fresult = amount - result2;
                    var needed = fresult.toFixed(3);

                    document.getElementById('account').value = user;
                    document.getElementsByName('data')[0].value = needed;

                });


                this.click("input#sbt.button[type='submit']");

                casper.wait(10000, function() {
                    casper.then(function() {
                        this.capture("filenadfgmedsfg.jpg");
                        var el2 = this.getHTML();
                        fs.write('results23.html', el2, 'w');
                    });
                });
            });
        });
    });
}

Then when I try the following

if(casper.exists(ac1)){
    casperstep(0.29, "username", "3245324");
}

it just does not work at all. The casper steps just do not fire. How can I fix this in theory? It should have worked.

What I have been trying with your answers...

My function

casper.captchaget = function (selector) {
    var Loc = this.getHTML(selector, true).match(/src="(.*?)"/)[1];
    var Ilocation = 'https://perfectmoney.is' + Loc;
    var image = Loc;
    var imagesplit = image.split ('?');
    var split1 = imagesplit[1];
    var string = split1 + ".jpg";
    this.download(Ilocation, string);
}

and how I am trying to use it

casper.then(function(){
    this.captchaget('img#cpt_img');//this.casperstep(0.29, "username", "3245324");
});

I tried the above to test out using casper extension.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user3407899
  • 139
  • 4
  • 16

1 Answers1

2

Well, you want to add your own method to a casper object instance : http://casperjs.readthedocs.org/en/latest/extending.html

so :

casper.casperstep = function (amount, user, location) {
        {your instructions....}
}

Then call it :

casper.start();
casper.then(function(){
    if(casper.exists(ac1)){
        casper.casperstep(0.29, "username", "3245324");//this.casperstep(0.29, "username", "3245324");
    }
})
.run(function() {
       test.done();
});

Old-monkey patching :)

To see other ways to do it : Custom casperjs modules

Community
  • 1
  • 1
Fanch
  • 3,274
  • 3
  • 20
  • 51
  • i cannot get my function to work i have simplified it to just capture a image from the page but when it come to firing my function it just skips past – user3407899 Mar 24 '14 at 18:10