Javascript 使用PhantomJS将自定义变量传递给onResourceRequested
为了避免我们的分析标签出现倒退,我想使用PhantomJS自动测试Adobe分析标签 为此,我希望能够测试两件事:Javascript 使用PhantomJS将自定义变量传递给onResourceRequested,javascript,phantomjs,automated-tests,build-automation,headless,Javascript,Phantomjs,Automated Tests,Build Automation,Headless,为了避免我们的分析标签出现倒退,我想使用PhantomJS自动测试Adobe分析标签 为此,我希望能够测试两件事: HTML源代码中存在某些js变量声明 将HTML源代码中的变量与对Adobe Analytics进行的Ajax调用进行比较,确保它们具有相同的值 我想监视的HTML源中的变量示例: 我希望能够将s_account和s_events变量传递给onResourceRequested函数,以便我可以断言这两个变量之间的相等性,并在calledUrl var中获得参数 但我不知道怎么做
- HTML源代码中存在某些js变量声明
- 将HTML源代码中的变量与对Adobe Analytics进行的Ajax调用进行比较,确保它们具有相同的值
但我不知道怎么做。任何帮助都将不胜感激 我找到了做这件事的方法 实际上,我从onResourceRequested回调中获取window.s_帐户的内容:
page.onResourceRequested = function(request) {
var obj = JSON.stringify(request, undefined, 4);
var needle = '2o7';
var url = request.url;
var result = false;
if (url.contains(needle)) {
var s_account = page.evaluate(function () {
return window.s_account;
});
result = true;
var calledUrl = decodeURI(decodeURIComponent(url));
console.log(s_account);
}
};
然而,这可能不是最优雅的方式,但它确实有效
"use strict";
if (!String.prototype.contains) {
String.prototype.contains = function (arg) {
return !!~this.indexOf(arg);
};
}
var page = require('webpage').create();
page.onResourceRequested = function(request) {
var obj = JSON.stringify(request, undefined, 4);
var needle = '2o7';
var url = request.url;
if (url.contains(needle)) {
var calledUrl = decodeURI(decodeURIComponent(url));
console.log(calledUrl);
}
};
page.onResourceReceived = function(response) {
var jsonResponse = JSON.stringify(response, undefined, 4);
};
page.open('http://www.domain.com/page.html', function() {
var s_account = page.evaluate(function () {
return window.s_account;
});
var s_events = page.evaluate(function () {
return window.s.events;
});
phantom.exit();
});
page.onResourceRequested = function(request) {
var obj = JSON.stringify(request, undefined, 4);
var needle = '2o7';
var url = request.url;
var result = false;
if (url.contains(needle)) {
var s_account = page.evaluate(function () {
return window.s_account;
});
result = true;
var calledUrl = decodeURI(decodeURIComponent(url));
console.log(s_account);
}
};