在if语句CasperJS之后执行操作

时间:2016-08-12 14:55:45

标签: javascript phantomjs casperjs

我遇到了连续检查页面属性更改的问题,但我做到了。 现在我的主要代码如下:

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {here
                return; // finished
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);         
    }).thenEvaluate(function(){
        this.click("#new_add");
        this.echo("done567");   
        this.then.waitForSelector(".cartGdList",
            function pass () {
                this.click("...");
                this.echo("done8");
                this.then(function () {
                    this.waitForSelector(".cart_s_Box",
                        function pass () {
                            this.click("#js_upFormBtn");
                            this.echo("step4");
                            var end = new Date().getTime();
                            var time = end - start;
                            this.echo('time: ' + time + 'ms*');
                        },
                        function fail () {
                            this.echo('fail');
                        }
                    );
                });
            },
            function fail () {
                this.echo('fail');
            }
        );
    });
});

if if statement is OK if(cur == "target") 我想执行此代码:

this.click("#new_add");
this.echo("done567");   
this.then.waitForSelector(".cartGdList",
    function pass () {
        this.click("...");
        this.echo("done8");
        this.then(function () {
            this.waitForSelector(".cart_s_Box",
                function pass () {
                    this.click("#js_upFormBtn");
                    this.echo("step4");
                    var end = new Date().getTime();
                    var time = end - start;
                    this.echo('time: ' + time + 'ms*');
                },
                function fail () {
                    this.echo('fail');
                }
            );
        });
    },
    function fail () {
        this.echo('fail');
    }
);

我怎么能这样做,我试着把它放在thenEvaluate上,但这不起作用。

1 个答案:

答案 0 :(得分:0)

你有很多不必要的嵌套,但首先要做的事情。您想在if - 分支中执行一些代码。好的,然后将代码放在if - 分支。

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {
                this.click("#new_add");
                this.echo("done567");
                // ...
                return; // stop recursion
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);
        // ...

如果您不想要这么多缩进,您也可以将代码放在一个函数中。

function myIfBranch(){
    this.click("#new_add");
    this.echo("done567");
    // ...
}

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {
                myIfBranch.call(this);
                return; // stop recursion
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);
        // ...

至于不必要的嵌套。请记住,then*wait*函数实际上都是异步步骤函数,并且都以相同的异步方式运行。这是一个清理过的例子:

function myIfBranch(){
    this.click("#new_add");
    this.echo("done567");   
    this.waitForSelector(".cartGdList",
        function pass () {
            this.click("...");
            this.echo("done8");
            this.waitForSelector(".cart_s_Box",
                function pass () {
                    this.click("#js_upFormBtn");
                    this.echo("step4");
                    var end = new Date().getTime();
                    var time = end - start;
                    this.echo('time: ' + time + 'ms*');
                },
                function fail () {
                    this.echo('fail');
                }
            );
        },
        function fail () {
            this.echo('fail');
        }
    );
}

casper.thenOpen('pageurl', function() {
    function checkReload() {
        var cur = this.getElementAttribute("classname", "attr");
        if (cur == "target") {
            myIfBranch.call(this);
            return; // finished
        }
        else
        {
            this.echo(cur);
        }
        this.reload();
        this.wait(1, checkReload); // check again in a second
    }
    this.then(checkReload);

请注意thiscasper.evaluate内的casper.thenEvaluate并不是casper,而是window。根本没有window.clickwindow.echowindow.then功能。此外,this.then.waitForSelector(...)没有意义。