There has been a lot of discussion surrounding the absence of a Function.prototype.bind method in PhantomJS, and many generous individuals have created shims/polyfills or directed others to those resources. I am currently integrating PhantomJS via Selenium Webdriver with Python bindings. Despite attempting various methods to utilize this polyfill, I have not been successful. Here is the code snippet I am using in my tester class that inherits from Webdriver:
bindShim = """var script = document.createElement('script');
script.src = '/home/dunkin/scripts/es5-shim.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
"""
self.execute_script(bindShim)
I run this code every time I navigate to a new page. While this approach worked for ensuring jQuery variables were recognized by PhantomJS, I still encounter the following error in my PhantomJS driver log:
[ERROR - 2015-02-10T17:43:44.068Z] Session [fd37e5c0-b14b-11e4-b9e3-5bbebfaf3f9d] - page.onError - msg: TypeError: 'undefined' is not a function (evaluating 'arguments.callee.bind(this,e)')
[ERROR - 2015-02-10T17:43:44.069Z] Session [fd37e5c0-b14b-11e4-b9e3-5bbebfaf3f9d] - page.onError - stack:
(anonymous function) (https://jsta.sh/media/all.js?1459:16)
t (https://jsta.sh/media/all.js?1459:16)
(anonymous function) (https://jsta.sh/media/all.js?1459:17)
(anonymous function) (https://jsta.sh/media/all.js?1459:8)
(anonymous function) (https://jsta.sh/media/all.js?1459:8)
(anonymous function) (https://jsta.sh/media/all.js?1459:8)
I (https://jsta.sh/media/all.js?1459:2)
and so on.
Although my question may seem related to other inquiries about the .bind() issue, I believe it can be valuable to those looking to enhance their default Selenium PhantomJS setup. Ideally, I would prefer to modify the JavaScript library used by my Ghostdriver-PhantomJS-Selenium stack rather than directly injecting the es5 shim into each page I visit. However, I am uncertain how to achieve this or if it is possible. I am starting to think that such tasks might have been simpler if I had built this tester directly on bare PhantomJS instead of incorporating it through another framework.
Current specifications:
- Selenium version 1.43
- PhantomJS 1.98
- Python 2.7
- Ubuntu 14.04 LTS (GNU/Linux 3.17.1-elastic x86_64)
*** When utilizing the es5-shim in the PhantomJS console, I get the following promising outcome:
phantomjs> console.log(Object.keys)
function keys() {
[native code]
}
undefined
phantomjs> var shim = require("/home/dunkin/scripts/es5-shim.js")
undefined
phantomjs> console.log(Object.keys)
function keys(object) {
if (isArguments(object)) {
return originalKeys(ArrayPrototype.slice.call(object));
} else {
return originalKeys(object);
}
}
undefined
phantomjs>