I have a test.html page, here's code for it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
</head>
<body>
<span id="test" onMouseover="alert('1')">this is new one</span>
</body>
</html>
I want to use Selenium JavascriptExecutor to simulate mouse over event on span in test page, so I wrote code like this:
@Test
public void testJSExecutor(){
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
webDriver = new FirefoxDriver();
webDriver.get("file:\\\\C:/test.html");
String script = "function test(){var t=document.getElementById('test');"
+ "if( document.createEvent ) {"
+ "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initEvent( 'mouseover', true, false );"
+ "elem.dispatchEvent(evObj);"
+"} else if( document.createEventObject ) {"
+ "elem.fireEvent('onmouseover');"
+"}} window.onload=test;";
jsExecutor = (JavascriptExecutor) webDriver;
jsExecutor.executeScript(script);
}
But after run this code, no alert prompted.
How could mouse over event be valid so alert could be prompted?