简体   繁体   中英

Casperjs Fill Method make next function only run after the previouse function has finished

heres my casperjs script

var ccommss = require('node_modules/ccomms');
var system = require('system');
var casper = require('casper').create({
    logLevel: "debug",
    waitTimeout: 10,
    stepTimeout: 10,
    verbose: true,
    viewportSize: {
        width: 1280,
        height: 960
    },
    pageSettings: {
        "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
        "webSecurityEnabled": false,
        "ignoreSslErrors": true
    },
    onWaitTimeout: function () {
        // casper.capture('./out/wait-timeout:_' + TimeTidy() + '.png');
        // throw new Error stuff;
    },
    onStepTimeout: function () {
        // casper.capture('./out/step-timeout' + TimeTidy() + '.png');
        // throw new Error stuff;
    }
});


var Visit = "https://example.com/login.html";
var use = {};
casper.start(Visit, function () {

    casper.then(function () {
        ccommss.CaptchaGet();
    });

    casper.then(function () {
        ccommss.Login();
        console.log(use.name + " " + use.pass + " " + use.turing);
    });

    casper.then(function () {
        this.echo(this.getCurrentUrl());
    });

});

casper.run();

the problem is when i run the ccommss.Login() function i have built wich is below the casper.then statment this.getcurrenturl() is ran before the page redirects

heres is my function

exports.Login = function() {

var line = system.stdin.readLine();
    var split = line.split (':');
    casper.fill('form[name="reg"]', {
           'login': split[0],
           'password': split[1],
           'turing': split[2]
        }, false);
casper.click("input#sbt.submit[type='submit']");

return use.name = split[0],
       use.pass = split[1],
       use.turing = split[2];



};

problem is when this runs

    casper.then(function () {
        this.echo(this.getCurrentUrl());
    });

it is useless as this function is still working

ccommss.Login();

and the url change has not happend yet i know i may be able to fix it with a callback of somekind can anybod help me fix this please

You set stepTimeout to 10 milli seconds. It is just not enough time to process anything in the login step. So my guess is that the login step is aborted and the next step is executed. By default the script would die , but you provided a stepTimeout callback which disables the die() .

Solution would be to increase the timeout to something like 5 seconds ( 5000 ). Btw, waitTimeout is also too short.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM