Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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();
share|improve this answer

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

share|improve this answer
    
I see the issue whether or not I run the test in Debug mode or as normal – squeemish Jul 16 '12 at 18:43

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;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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