Javascript 捕获casper上的网络资源请求。单击()

Javascript 捕获casper上的网络资源请求。单击(),javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,通过casper.on(“resource.requested”),我们可以捕获资源请求并执行评估检查 在页面加载时,我们将所有网络请求URL推送到一个数组中,然后遍历该数组以查找对GOOGLE Analytics(即_utm.gif)的调用次数 //谷歌分析呼叫测试 casper.test.begin('testcontainer Tags',函数套件(test){ casper.start(“http://www.viget.com/“,函数(){ }); var url=[], 链接=[]

通过casper.on(“resource.requested”),我们可以捕获资源请求并执行评估检查

在页面加载时,我们将所有网络请求URL推送到一个数组中,然后遍历该数组以查找对GOOGLE Analytics(即_utm.gif)的调用次数

//谷歌分析呼叫测试
casper.test.begin('testcontainer Tags',函数套件(test){
casper.start(“http://www.viget.com/“,函数(){
});
var url=[],
链接=[];
casper.on('resource.requested',函数(requestData,resource){
push(decodeURI(requestData.url));
});
casper.then(函数(){
var指数=-1;
var=0;
对于(var i=0;i-1)
发现=发现+1;
}
casper.echo('found'+found);
断言(发现>0,“页面加载测试完成”);
});
//发出“resource.requested”以在单击链接时捕获网络请求
casper.then(函数(self){
var utils=require('utils');
var x=require('casper')。选择XPath;
单击(x(“//a[数据类型]”);
emit('resource.requested');
});
casper.run(函数(){
test.done();
});
});
但是,现在下一个任务是验证超链接单击事件上的网络资源请求。尝试使用casper.emit(“resource.requested”)使其正常工作,但没有成功


已经花了整整一天的时间来寻找解决方法。如果您有任何反馈,我们将不胜感激。

您可以在单击后使用casper.waitForResource()进行验证

casper.test.begin('Test Container Tags', function suite(test) {

casper.start("http://www.viget.com/", function() {

});

var urls = [],
    links = [];

casper.on('resource.requested', function(requestData, resource) {
    urls.push(decodeURI(requestData.url));
});

casper.then(function() {
    var index = -1;
    var found = 0;
    for (var i = 0; i < urls.length; i++) 
    {
        index = urls[i].indexOf('__utm.gif');
        if (index > -1)
            found = found+1;
    }
    casper.echo('found' + found);
    test.assert(found > 0, 'Page Load Test Complete');
});

//Emit "resource.requested" to capture the network request on link click
casper.then(function(self) {
    var utils = require('utils');
    var x = require('casper').selectXPath;
    casper.click(x("//a[data-type]"));

});

casper.waitForResource(function testResource(resource) {
    console.log('----->' + resource.url);
});

casper.run(function() { 
    test.done();
});
casper.test.begin('testcontainer Tags',函数套件(test){
casper.start(“http://www.viget.com/“,函数(){
});
var url=[],
链接=[];
casper.on('resource.requested',函数(requestData,resource){
push(decodeURI(requestData.url));
});
casper.then(函数(){
var指数=-1;
var=0;
对于(var i=0;i-1)
发现=发现+1;
}
casper.echo('found'+found);
断言(发现>0,“页面加载测试完成”);
});
//发出“resource.requested”以在单击链接时捕获网络请求
casper.then(函数(self){
var utils=require('utils');
var x=require('casper')。选择XPath;
单击(x(“//a[数据类型]”);
});
waitForResource(函数testResource(资源){
console.log('---->'+resource.url);
});
casper.run(函数(){
test.done();
});
}))

casper.test.begin('Test Container Tags', function suite(test) {

casper.start("http://www.viget.com/", function() {

});

var urls = [],
    links = [];

casper.on('resource.requested', function(requestData, resource) {
    urls.push(decodeURI(requestData.url));
});

casper.then(function() {
    var index = -1;
    var found = 0;
    for (var i = 0; i < urls.length; i++) 
    {
        index = urls[i].indexOf('__utm.gif');
        if (index > -1)
            found = found+1;
    }
    casper.echo('found' + found);
    test.assert(found > 0, 'Page Load Test Complete');
});

//Emit "resource.requested" to capture the network request on link click
casper.then(function(self) {
    var utils = require('utils');
    var x = require('casper').selectXPath;
    casper.click(x("//a[data-type]"));

});

casper.waitForResource(function testResource(resource) {
    console.log('----->' + resource.url);
});

casper.run(function() { 
    test.done();
});