Tuesday 8 September 2015

Mouse Hover functionality with selenium



Handling Mouse hover is one of the complex events for Selenium Users

We can not handle Mouse Hover directly by any command; we have to use some javascript to help us in mouse hover.

In this post I have created a method which will help selenium users to handle mouse hover event.

You can easily call that method into your script logic.

 public void MouseHover(String locatorvalue ) throws InterruptedException  
 {  
      Actions action = new Actions(driver);  
      WebElement element = driver.findElement(By.id(locatorvalue));  
      action.moveToElement(element).build().perform();  
      Thread.sleep(5000);  
             
             
      String javaScript = "var evObj = document.createEvent('MouseEvents');" +  
      "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false,        
  false, false, 0, null);" +  
      "arguments[0].dispatchEvent(evObj);";  
   
      WebElement element1 = driver.findElement(By.id(locatorvalue));  
      ((JavascriptExecutor)driver).executeScript(javaScript, element1);  
             
      Thread.sleep(5000);  
 }  
   

You can call this method as  
 MouseHover(locator value );  

No comments:

Post a Comment