Javascript 如何通过phantomjs单击extjs按钮后接收输出
我正在编写一个脚本,用户可以通过该脚本从命令行下载excel工作表,而无需单击内部网站上的“导出到excel”按钮。我的ExtJS UI上已经有一个“导出到Excel”按钮,我想在脚本中重用该功能 我能够加载该页面,并可以检查是否有Ext可用。我想我需要调用button.handler来模拟单击按钮。事实上,当从firebug调用时,它确实下载了excel工作表 到目前为止,我一直在使用幻影。基本上是和的集合体Javascript 如何通过phantomjs单击extjs按钮后接收输出,javascript,extjs,phantomjs,extjs4.2,Javascript,Extjs,Phantomjs,Extjs4.2,我正在编写一个脚本,用户可以通过该脚本从命令行下载excel工作表,而无需单击内部网站上的“导出到excel”按钮。我的ExtJS UI上已经有一个“导出到Excel”按钮,我想在脚本中重用该功能 我能够加载该页面,并可以检查是否有Ext可用。我想我需要调用button.handler来模拟单击按钮。事实上,当从firebug调用时,它确实下载了excel工作表 到目前为止,我一直在使用幻影。基本上是和的集合体 问题是,内部等待总是超时。你知道如何解决这个问题吗?PhantomJS不能下载一个文
问题是,内部等待总是超时。你知道如何解决这个问题吗?PhantomJS不能下载一个文件,只要你点击相应的按钮。您必须通过XMLHttpRequest显式下载该文件。为此,您必须对下载该文件的JavaScript代码进行反向工程 函数下载页面,url{ 返回页面。evaluatefunctionurl{ var xhr=新的XMLHttpRequest; openGET,url,false; xhr.send; 返回xhr.responseText; },网址; } 即使您单击元素,然后尝试使用等待请求,然后使用现在已知的url触发下载,也无法工作,因为请求不是由PhantomJS发送的
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
var page = require('webpage').create(),
system = require('system'),
t, address;
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
}
t = Date.now();
address = system.args[1];
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading time ' + t + ' msec');
waitFor(function() {
var btn = page.evaluate(function() {
return Ext.getCmp('export_to_excel_btn');
});
return btn !== null;
},
function() {
var xl;
waitFor(function() {
xl = page.evaluate(function() {
var btn = Ext.getCmp('export_to_excel_btn');
return btn.handler(); // Never works.
});
console.log(xl)
return xl !== null;
},
function() {
console.log('In the inner waitFor');
phantom.exit();
}, 10000);
}, 10000);
}
});