Selenium's PhantomJS webdriver在reactjs中不加载页面

Selenium's PhantomJS webdriver not loading page in reactjs

本文关键字:reactjs 加载 webdriver PhantomJS Selenium      更新时间:2023-09-26

我正在测试一个网站的新功能。这是目前为止唯一一个用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

相关文章: