“然后”在CasperJS中真正意味着什么 [英] What Does 'Then' Really Mean in CasperJS

查看:150
本文介绍了“然后”在CasperJS中真正意味着什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CasperJS通过网站自动执行一系列点击,完成表单,解析数据等。

I'm using CasperJS to automate a series of clicks, completed forms, parsing data, etc through a website.

Casper似乎被组织成一个列表以然后语句的形式预设步骤(请参阅此处的示例: http://casperjs.org/quickstart.html )但不清楚是什么触发了实际运行的下一个语句。

Casper seems to be organized into a list of preset steps in the form of then statements (see their example here: http://casperjs.org/quickstart.html) but it's unclear what triggers the next statement to actually run.

例如,然后等待所有待处理的请求完成? injectJS 是否计为待处理请求?如果我有一个嵌套的然后语句会发生什么 - 链接到 open 语句的末尾?

For example, does then wait for all pending requests to complete? Does injectJS count as a pending request? What happens if I have a then statement nested - chained to the end of an open statement?

casper.thenOpen('http://example.com/list', function(){
    casper.page.injectJs('/libs/jquery.js');
    casper.evaluate(function(){
        var id = jQuery("span:contains('"+itemName+"')").closest("tr").find("input:first").val();
        casper.open("http://example.com/show/"+id); //what if 'then' was added here?
    });
});

casper.then(function(){
    //parse the 'show' page
});

我正在寻找有关CasperJS中流程如何工作的技术说明。我的具体问题是我的最后一个然后语句(上面)在我的 casper.open 语句&之前运行。我不知道为什么。

I'm looking for a technical explanation of how the flow works in CasperJS. My specific problem is that my last then statement (above) runs before my casper.open statement & I don't know why.

推荐答案

然后()基本上添加堆栈中的新导航步骤。步骤是一个javascript函数,它可以做两件事:

then() basically adds a new navigation step in a stack. A step is a javascript function which can do two different things:


  1. 等待上一步 - 如果有的话 - 正在执行

  2. 等待请求的网址和相关页面加载

让我们来看一个简单的导航场景:

Let's take a simple navigation scenario:

var casper = require('casper').create();

casper.start();

casper.then(function step1() {
    this.echo('this is step one');
});

casper.then(function step2() {
    this.echo('this is step two');
});

casper.thenOpen('http://google.com/', function step3() {
    this.echo('this is step 3 (google.com is loaded)');
});

您可以打印出堆栈中所有已创建的步骤,如下所示:

You can print out all the created steps within the stack like this:

require('utils').dump(casper.steps.map(function(step) {
    return step.toString();
}));

这给出:

$ casperjs test-steps.js
[
    "function step1() { this.echo('this is step one'); }",
    "function step2() { this.echo('this is step two'); }",
    "function _step() { this.open(location, settings); }",
    "function step3() { this.echo('this is step 3 (google.com is loaded)'); }"
]

注意 _step()函数,CasperJS自动添加该函数以加载我们的URL;当加载url时,将调用堆栈中可用的下一步 - 即 step3() - 。

Notice the _step() function which has been added automatically by CasperJS to load the url for us; when the url is loaded, the next step available in the stack — which is step3() — is called.

定义导航步骤后, run()按顺序逐个执行:

When you have defined your navigation steps, run() executes them one by one sequentially:

casper.run();

脚注:回调/监听器的内容是承诺模式。

Footnote: the callback/listener stuff is an implementation of the Promise pattern.

这篇关于“然后”在CasperJS中真正意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆