sam0673
sam0673

Reputation: 201

Webdriver - Webdriver MoveToElement does not work in IE and Firefox to get a text tooltip

Have tried calling MoveToElement so that I can get the tooltip of a particular element. This works in Chrome fine. However, am trying to do the same thing on IE10 and Firefox 26.0 and it does hover over - but only for a split second hence not giving me enough time to get the tooltip. Putting in a sleep does not help and besides I trying to avoid thread.sleep as much as possible. My question: Is there an alternative way to hover over a field or some other expectedConditions that can be used to see if the tooltip comes up and remains there for Firefox and IE?

Code snippet:

    /// <summary>
    /// Check to see that the hover over option for the 'Defined' column
    /// exists and also to return the text for that hover over option.
    /// </summary>
    /// <returns></returns>
    public Tuple<bool, string[]> HoverOverDefinedColumn(bool javascriptWorkaround = false)
    {
        Thread.Sleep(1000);
        var wait = WebDriverWaitObject();
        var action = new Actions(driver);
        wait.Until(d => HoverOverDefinedRow);

        action.MoveToElement(HoverOverDefinedRow).MoveByOffset(5, 0);
        action.Build().Perform();
        var isThereAnHoverOption = HoverOverOptionExists(wait);
        var textDefinedForHoverOption = TextDefined(HoverOptionText);
        return new Tuple<bool, string[]>(isThereAnHoverOption, textDefinedForHoverOption);
    }

    /// <summary>
    /// Checks to see specifically if the hover over option exists.
    /// </summary>
    /// <param name="wait"></param>
    /// <returns></returns>
    private bool HoverOverOptionExists(WebDriverWait wait)
    {
         var hoverOverElement =
                wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#TTipTDnetst.hintsClass")));

        return IsElementPresent(hoverOverElement);
    }

    /// <summary>
    /// Gets the text for the hover over option.
    /// </summary>
    /// <param name="element"></param>
    /// <returns></returns>
    private string[] TextDefined(IWebElement element)
    {
        var path = (element.Text.Split(new string[] { " » " }, StringSplitOptions.None));
        return path;
    }

Upvotes: 2

Views: 2698

Answers (1)

sam0673
sam0673

Reputation: 201

There are 3 different browsers being tested: (1) Firefox (2) Chrome (3) Internet Explorer

(1) Firefox (v 26.0) I still have the 'MoveToElement' problem using the Actions class. Instead, I perform a try/catch handler so that in case the actions do not work then the exception would run a Javascript method to get the tool tip as presented below:

    /// <summary>
    /// This is the workaround for the hover over functionality
    /// for the 'Defined' column. This (for the moment) specifically
    /// applies to Firefox and its inability to get the tool tip.
    /// </summary>
    /// <param name="elemement"></param>
    private void HoverOverWorkAround(IWebElement elemement)
    {
        var code = "var fireOnThis = arguments[0];"
            + "var evObj = document.createEvent('MouseEvents');"
            + "evObj.initEvent( 'mouseover', true, true );"
            + "fireOnThis.dispatchEvent(evObj);";
        ((IJavaScriptExecutor)driver).ExecuteScript(code, elemement);
    }

(2) Chrome (v 32.017) Actions class for hover over works fine as is with the latest ChromeDriver.

(3) Internet Explorer 10 This works with the Actions class as long as I have a driver with the following options:

var options = new InternetExplorerOptions {RequireWindowFocus = true, EnablePersistentHover = false}; instance = new InternetExplorerDriver(ApplicationSettings.DriverLocation, options);

NOTE:

In the case of IE, 'NativeEvents' option is set to true by default (for Windows) and I leave that alone.

Always use 'Native Events' meaning (in this case) use the 'Actions' class to get at the hovering functionality. If this does not work then catch the exception method and run the Javascript (as for Firefox above).

The reasoning for the above approaches comes from ideas taken from the following discussion:

http://code.google.com/p/selenium/issues/detail?id=2067

Upvotes: 1

Related Questions