Browse Tag: mousehover selenium

Mouse hover and other mouse events in WebDriver

This is widely known issue among web driver community, how to perform mouse hover.  I have many scenarios in my test where I needed to perform mouse hover to see calendar, email schedule etc. After many attempts, I found solution which is actually working very well for me but I am not sure if it’s going to work with your application of not.  Before going into coding we need to consider few limitations,

  • In Firefox, you have to maximize the webpage just before doing mouseover to get the focus on browser.
  • Works fine even in normal window size with other browser like IE and Chrome.



For mouse event, you  can call Action builder or even java script can do as well. I will not go in more theoretical detail but directly into coding. I am giving the same code I am using in my scripts, so please feel free to change or modify as per your need.

Using Action Builder:

public static bool ElementMouseOver(IWebElement targetElement)
        {
            Size currentWinSize = _driver.Manage().Window.Size;
            _driver.Manage().Window.Maximize();
            OpenQA.Selenium.Interactions.Actions builder = new OpenQA.Selenium.Interactions.Actions(_driver);
            try
            {
                builder.MoveToElement(targetElement).Build().Perform();
                Thread.Sleep(2000);//2 sec is just to for this blog.
                                   //I use custome method to wait element being appeared after mouse hover event.
                                   //You can use other variable wait time but make sure your give some pause
                                   //otherwise mouse hover will happen for fraction of seconds and then disappear.

                _driver.Manage().Window.Size = currentWinSize;
            }
            catch (Exception e)
            {
                loggerInfo.Instance.Message(e.Message);
                return false;
            }
            return true;
        }



Using JavaScript

public static void MouseHoverByJavaScript(IWebElement targetElement)
        {

            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);";
            IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
            js.ExecuteScript(javaScript, targetElement);
          }

You can use above java script to perform other  mouse events like click/double click. Please refer initMouseEvent which you can use in above code.
                          
I implemented drag and Drop event as well using Action builder itself.
Drag And Drop


            IWebElement source = FindElement("blah blah");
            IWebElement target = FindElement("blah blah");
            OpenQA.Selenium.Interactions.Actions builder = new OpenQA.Selenium.Interactions.Actions(_driver);
            try
            {
               builder.DragAndDrop(source, target).Build().Perform();
            }
            catch (Exception e)
            {
                //log Exception.I use NLog.
            }

Everyone uses own approach to handle such events. I mostly use Firefox/Chrome for testing as IE some time very flaky. And personally I hate IE. I tested above code in FF/Chrome and it was working fine. I would like to learn new way to handle such events If you are aware of any and please don’t forget to post in comment below or email me via contact us page.