我正在尝试测试网站的一项新功能。这是迄今为止用 React 构建的唯一页面。当我尝试使用 PhantomJS 在 Selenium 中运行测试时,页面索引会加载,但完整页面加载永远不会触发。
页面正文是:
<body>
<main id="content"></main>
<script type="text/javascript">
function loadBundleJS( jsSource){
var bundleJSScriptTag=document.createElement('script')
bundleJSScriptTag.setAttribute("type","text/javascript")
bundleJSScriptTag.setAttribute("src", jsSource)
if (typeof bundleJSScriptTag != 'undefined'){
document.getElementsByTagName('head')[0].appendChild(bundleJSScriptTag);
}
}
var paramsArray = window.location.search.substring(1).split("&");
Object.keys(paramsArray).forEach(function(key){
var param = paramsArray[key];
if (param.indexOf("/")>-1){
param = param.substring(0, param.indexOf("/"))
}
})
loadBundleJS('js/bundle.0.0.2.js')
</script>
</body>
当网站在浏览器中运行时,内容将附加到主标记中。然而,在 PhantomJS 中,此内容永远不会被附加,并且 PhantomJS 会加载空白页面。
最佳答案
问题不在你的代码中,而是在 PhantomJS 运行的 WebKit 浏览器中。 PhantomJS 运行旧版本的 WebKit 引擎,该引擎使用旧版本的 ECMAScript。
ReactJS 使用 ECMAScript 5 中的 Function.bind 方法。
解决方案非常简单,如果 Function.prototype.bind 不存在,则需要在代码中定义它。
** 确保代码在之前加载,包括react.js。
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
代码取自:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind#Polyfill
关于javascript - Selenium 的 PhantomJS Webdriver 未在 ReactJS 中加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271237/