I'm a newcomer to selenium and javascript and looking to incorporate the VisualEvent javascript into pages opened in a selenium-controlled browser. My goal is to access its variables from selenium in java. I've successfully completed the first phase using the following code:
driver = new FirefoxDriver();
driver.get("http://stackoverflow.com/");
String script = //Minified version of the script below
//////////////
(function() {
var protocol = window.location.protocol === 'file:' ? 'http:' : '';
var url = protocol + '//www.sprymedia.co.uk/VisualEvent/VisualEvent_Loader.js';
if (typeof VisualEvent != 'undefined') {
if (VisualEvent.instance !== null) {
VisualEvent.close();
} else {
new VisualEvent();
}
} else {
var n = document.createElement('script');
n.setAttribute('language', 'JavaScript');
n.setAttribute('src', url + '?rand=' + new Date().getTime());
document.body.appendChild(n);
}
})();
///////////////////////
Object[] a = { null, null, null };
driver.executeScript(script, a);
However, when attempting to access window.VisualEvent
:
script = "return window.VisualEvent.instance;";
Object b = driver.executeScript(script, a);
An exception is thrown:
Exception in thread "main" org.openqa.selenium.JavascriptException: TypeError: window.VisualEvent is undefined
Interestingly, executing the same code in the browser console returns the reference. Additionally, I can access the window.document
object from selenium. Any suggestions?