5

If I automate mouse actions using Selenium2/Webdriver in combination with Firefox 12, I can easily get the "mouse" to move to and hover over a context menu so the sub-menus are seen. However, if I have the real mouse resting outside of the active window running the test, there's a 99% chance when the hover action occurs, the context menu will flash quickly and disappear as if the mouse had moved. This causes the next step in the test to fail since the web element is no longer visible.

Once in a blue moon the test will run fine if the mouse is outside of the window. That's definitely a rare instance. If the real mouse is resting anywhere inside of the active window running the test, the hover action and test will run without issue 100% of the time.

Has anyone experienced this behavior or is it a known issue of Selenium2/Webdriver and Firefox? If you have experienced this, although it doesn't seem likely there could be a workaround, have you found a way to counteract this behavior?

It's a pain to have to wait for a test to run if it contains a hover command instead of letting it go in the background while I continue working on something else.

3 Answers 3

3

I edit the firefox profile to disable native events. Also maximize window and it works fine

FirefoxProfile p = new FirefoxProfile();
p.setEnableNativeEvents(false);
WebDriver driver = new FirefoxDriver(p);    
driver.get("url");
driver.manage().window().maximize();
0

I did notice this behavior, but only while debugging. When I executed the tests normally I never saw the issue. Have you tried that?

1
  • I see the issue whether or not I run the test in Debug mode or as normal
    – squeemish
    Jul 16, 2012 at 18:43
0

Here is a workaround. Use Javascript to hover the mouse.

public static void mouseHoverJScript(WebElement HoverElement) {
            try {
                if (isElementPresent(HoverElement)) {

                    String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
                    ((JavascriptExecutor) Selenium.webDriver).executeScript(mouseOverScript,HoverElement);

            } else {
                System.out.println("Element was not visible to hover " + "\n");

            }
        } catch (StaleElementReferenceException e) {
            System.out.println("Element with " + HoverElement
                    + "is not attached to the page document"
                    + e.getStackTrace());
        } catch (NoSuchElementException e) {
            System.out.println("Element " + HoverElement + " was not found in DOM"
                    + e.getStackTrace());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error occurred while hovering"
                    + e.getStackTrace());
        }
    }

    public static boolean isElementPresent(WebElement element) {
        boolean flag = false;
        try {
            if (element.isDisplayed()
                    || element.isEnabled())
                flag = true;
        } catch (NoSuchElementException e) {
            flag = false;
        } catch (StaleElementReferenceException e) {
            flag = false;
        }
        return flag;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.