Javascript 如何在CasperJS中同步调用子进程?
代码: 使用Javascript 如何在CasperJS中同步调用子进程?,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,代码: 使用casperjs test\u child\u process.js运行,输出: var casper = require('casper').create({ waitTimeout: 120000, stepTimeout: 120000, verbose: true, logLevel: "debug" }); var process = require("child_process") var spawn = process.spawn c
casperjs test\u child\u process.js运行,输出:
var casper = require('casper').create({
waitTimeout: 120000,
stepTimeout: 120000,
verbose: true,
logLevel: "debug"
});
var process = require("child_process")
var spawn = process.spawn
casper.start('http://baidu.com')
casper.then(function () {
casper.echo(httpCall('baidu.com', 'GET'))
})
function httpCall(url, method) {
var para = ["-X", method, url]
casper.log('curl with parameters: ' + para)
var child = spawn("curl", para)
var result = "abc"
child.stdout.on("data", function (data) {
casper.echo("curl ended.")
result = data;
})
return result;
}
casper.then(function () {
casper.wait(2000)
})
casper.run()
[info][phantom][2016-05-05T10:23:36.196Z]开始。。。
[信息][幻影][2016-05-05T10:23:36.203Z]跑步套件:4个步骤
[调试][幻影][2016-05-05T10:23:36.226Z]开始url:http://baidu.com/,HTTP-GET
[调试][幻影][2016-05-05T10:23:36.227Z]请求导航:url=http://baidu.com/,type=Other,willNavigate=true,isMainFrame=true
[调试][幻影][2016-05-05T10:23:36.461Z]请求导航:url=https://www.baidu.com/,type=Other,willNavigate=true,isMainFrame=true
[debug][phantom][2016-05-05T10:23:36.684Z]url更改为“https://www.baidu.com/"
[debug][phantom][2016-05-05T10:23:37.364Z]已成功注入Casper客户端实用程序
已加载[调试][幻影][2016-05-05T10:23:37.382Z]起始页
[信息][幻影][2016-05-05T10:23:37.406Z]步骤匿名3/4https://www.baidu.com/ (HTTP://200)
[debug][phantom][2016-05-05T10:23:37.406Z]带参数的卷曲:-X,GET,baidu.com
abc尝试PhantomJS 1.x,在这里它应该是同步的。PhantomJS 2.x不支持wait
,因此您将无法编写同步代码。@ArtjomB。谢谢现在我已经解决了这个问题,然后你可以自己发布答案。
[info] [phantom] [2016-05-05T10:23:36.196Z] Starting...
[info] [phantom] [2016-05-05T10:23:36.203Z] Running suite: 4 steps
[debug] [phantom] [2016-05-05T10:23:36.226Z] opening url: http://baidu.com/, HTTP GET
[debug] [phantom] [2016-05-05T10:23:36.227Z] Navigation requested: url=http://baidu.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] [2016-05-05T10:23:36.461Z] Navigation requested: url=https://www.baidu.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] [2016-05-05T10:23:36.684Z] url changed to "https://www.baidu.com/"
[debug] [phantom] [2016-05-05T10:23:37.364Z] Successfully injected Casper client-side utilities
[debug] [phantom] [2016-05-05T10:23:37.382Z] start page is loaded
[info] [phantom] [2016-05-05T10:23:37.406Z] Step anonymous 3/4 https://www.baidu.com/ (HTTP 200)
[debug] [phantom] [2016-05-05T10:23:37.406Z] curl with parameters: -X,GET,baidu.com
abc <- I want it to be the stdout of curl...
[info] [phantom] [2016-05-05T10:23:37.413Z] Step anonymous 3/4: done in 1216ms.
[info] [phantom] [2016-05-05T10:23:37.429Z] Step anonymous 4/4 https://www.baidu.com/ (HTTP 200)
[info] [phantom] [2016-05-05T10:23:37.430Z] Step anonymous 4/4: done in 1233ms.
[info] [phantom] [2016-05-05T10:23:37.451Z] Step _step 5/5 https://www.baidu.com/ (HTTP 200)
[info] [phantom] [2016-05-05T10:23:37.451Z] Step _step 5/5: done in 1254ms.
curl ended.
[info] [phantom] [2016-05-05T10:23:39.452Z] wait() finished waiting for 2000ms.
[info] [phantom] [2016-05-05T10:23:39.453Z] Done 5 steps in 3256ms
[debug] [phantom] [2016-05-05T10:23:39.455Z] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] [2016-05-05T10:23:39.456Z] url changed to "about:blank"