Selenium's PhantomJS webdriver在reactjs中不加载页面
Selenium's PhantomJS webdriver not loading page in reactjs
我正在测试一个网站的新功能。这是目前为止唯一一个用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加载一个空白页面。
问题不在你的代码中,是在WebKit浏览器中,PhantomJS运行。PhantomJS运行的是旧版本的WebKit引擎,它使用的是旧版本的ECMAScript。
ReactJS使用函数。从ECMAScript中绑定方法
解决方案非常简单,如果不存在的话,您需要在代码中定义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
相关文章:
- 如何在ReactJs中渲染子组件时重新加载子组件的数据
- 在reactjs组件中预加载数据
- ReactJS-在加载时渲染顶级组件
- 如何在reactjs中动态加载组件
- 外部js文件中的Reactjs组件有时不会加载
- 使用ReactJS动态加载组件
- 当我在 reactjs 中单击按钮时如何加载函数
- 使用 reactjs 加载 JSON
- ReactJS 在服务器响应加载之前将 props 发送到子级错误
- Reactjs:基于响应加载视图
- 如何使用ReactJS实现延迟加载功能(append child不适用于React)
- 使用react路由器浏览两次加载ReactJS
- webpack reactjs应用程序加载失败:Uncaught ReferenceError:未定义snapapp
- 在ReactJS中传递道具,它们不会加载,同时接收到目标容器错误
- 动态加载