Javascript 硒';s PhantomJS Web驱动程序未在reactjs中加载页面
我正在尝试测试一个网站的新功能。这是迄今为止唯一一个内置React的页面。当我尝试使用PhantomJS在Selenium中运行测试时,页面索引会加载,但不会触发完整页面加载 页面正文是:Javascript 硒';s PhantomJS Web驱动程序未在reactjs中加载页面,javascript,python-2.7,selenium,reactjs,phantomjs,Javascript,Python 2.7,Selenium,Reactjs,Phantomjs,我正在尝试测试一个网站的新功能。这是迄今为止唯一一个内置React的页面。当我尝试使用PhantomJS在Selenium中运行测试时,页面索引会加载,但不会触发完整页面加载 页面正文是: <body> <main id="content"></main> <script type="text/javascript"> function loadBundleJS( jsSource){ var
<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>
函数loadBundleJS(jsSource){
var bundlejscripttag=document.createElement('脚本')
bundlejscripttag.setAttribute(“类型”、“文本/javascript”)
bundlejscriptag.setAttribute(“src”,jsSource)
if(bundlejscripttag的类型!=“未定义”){
document.getElementsByTagName('head')[0].appendChild(bundlejscriptag);
}
}
var paramsArray=window.location.search.substring(1.split(“&”);
Object.keys(paramsArray).forEach(函数(键){
var param=paramsArray[key];
if(参数indexOf(“/”>-1){
param=param.substring(0,param.indexOf(“/”))
}
})
loadBundleJS('js/bundle.0.0.2.js')
当网站在浏览器中运行时,内容将附加到主标记。然而,在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;
};
}
代码取自: