I have this polyfill in JS for my site:
if (!Date.prototype.toSQLDate) {
console.log('Adding my custom polyfill');
Date.prototype.toSQLDate = function () {
// relevant code snipped, but assume:
// return some string like '2000-01-01';
}
}
When I load the website manually, I see the console log and I can do:
var x = new Date(2000, 0, 1);
console.log(x.toSQLDate()) => '2000-01-01'
This works both in Chrome/Chromium(96) and Firefox(95+)
However, when I run my automated tests against this website, in Firefox(95) only (Chrome works fine) I get this when attempting to run the same code via webdriver.execute_script
from selenium import webdriver
wd = webdriver.Firefox()
wd.get(...myurl)
wd.execute_script('var x = new Date(2000, 0, 1); return x.toSQLDate()')
>>>>
selenium.common.exceptions.JavascriptException: Message: TypeError: x.toSQLDate is not a function
What? If I watch the automated test with the console open, I can see my console.log telling me that it successfully added the polyfill. I can interact with the browser in the console and do the exact same logic and get a valid string back. But when webdriver executes it, it's not in the same js context? And only in Firefox.
This behavior is present in both Python2.7/selenium3 and Python3.8/selenium4
What is going on here? Do I need to enable some sort of sandbox js escape in Firefox?
Additional Information:
It appears that there may be some "quirk" in the way firefox/geckodriver treats globals. Consider this simple script:
from selenium import webdriver
ff = webdriver.Firefox()
ff.get('http://www.google.com')
ff.execute_script('return Date === window.Date') => False
ff.execute_script('console.log(Date === window.Date)') => false
# But this same script shows true for chrome.