On my test.html page, I've included the following code snippet:
<!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'm attempting to simulate a mouse over event on the 'test' span element using Selenium's JavascriptExecutor. Here's my current code:
@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);
}
Unfortunately, after running this code, the alert doesn't appear as expected. How can I make sure that the mouse over event is triggered successfully so that the alert pops up?