There has been a lot of discussion here and elsewhere about PhantomJS's lack of a Function.prototype.bind method, and a lot of helpful philanthropists have written shims/polyfills or pointed others to those resources. I'm implementing PhantomJS via Selenium Webdriver with Python bindings. I've tried a few ways to make use of this polyfill, to no avail. Currently I'm using the following code in my webdriver-inheriting tester class:
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 execute this code every time I visit a new page. In fact, this method worked for ensuring that jQuery variables were understood by PhantomJS. However, I'm still seeing the following 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)
etc.
My hope is that in spite of being closely related to other questions about this .bind() issue, my question is useful to those who generally want to add functionality to their out-of-the box Selenium PhantomJS implementation. I would love it if I could amend the JavaScript library implemented by my Ghostdriver-PhantomJS-Selenium stack, rather than directly add the es5 shim to every page I visit, but I'm not sure how I can do that or if. Beginning to feel that such things would be simpler had I just built this tester on bare PhantomJS instead of filtering it through another framework.
My specs:
- Selenium version 1.43
- PhantomJS 1.98
- Python 2.7
- Ubuntu 14.04 LTS (GNU/Linux 3.17.1-elastic x86_64)
*** the es5-shim produces the following promising result when i use it in the phantomjs console:
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>