I am trying to inject Javascript code into Selenium webdriver using external javascript source. So basically, I inject the DOM with this:
public class Testing extends BaseInitialization{
public static void main(String args[]) {
new Testing();
}
public Testing() {
driver.get("http://www.google.com/");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse
.executeScript("var s=window.document.createElement('script');" +
" s.type = 'text/javascript'; s.src='elementController.js';" +
" window.document.head.appendChild(s);");
jse.executeScript("test();");
}
}
And here is my js file:
document.ourFunction = function(){
alert("Hey");
}
function test() {
alert(1);
}
And this is a snippet from my project structure (js file is at the same level as Testing class is):
But for some reason, the external javascript is not loaded at all. Injection itself seems to work because when I replace the source file name with direct javascript (alert for example), then it works (Browser opens, it goes to google and alerts), but when I try to take javascript from js file, then it fails. What am I doing wrong?