Jen Bandelin
Jen Bandelin

Reputation: 193

Hover over an element using WatiN

I work on a suite of automated tests that have been developed using WatiN and MBUnit. I've heard that it's possible to get WatiN to 'hover' over an element, but I can't seem to get it working using the methods I've used in the past. Is there another way to do this that I don't know about? I've tried using just FireEvent 'onmouseover', and using the FireEvent plus clicking on the link.

myDiv.HoverLink.FireEvent("onmouseover");
myDiv.HoverLink.Click();

Any suggestions? Thanks in advance!

Upvotes: 3

Views: 1119

Answers (2)

rahoolm
rahoolm

Reputation: 773

    /// <summary>
    /// Mouse Over on given <see cref="Element"/>
    /// </summary>
    /// <param name="element">element</param>
    /// <returns>Nothing</returns>
    public static void MouseOver(this Element element)
    {
        var jref = element.GetJavascriptElementReference();
        var dom = element.DomContainer;

        var evt = new JSEventCreator(jref, null);
        var evtProp = new NameValueCollection();
        evtProp.Add("windowObject", "window");

        var scriptCode = evt.CreateMouseEventCommand("mouseover", evtProp);
        Logger.LogDebug(scriptCode);
        scriptCode = scriptCode.ToString() + jref + ".dispatchEvent(event);";

        string result = dom.Eval(scriptCode);
        Logger.LogAction(result);
        dom.WaitForComplete();
        Thread.Sleep(TimeSpan.FromSeconds(2));
    }

This is what I did and it works both on IE 11 and FF.

Upvotes: 0

Waylon Flinn
Waylon Flinn

Reputation: 20267

Try using the MouseEnter method on the object you want to hover.

Here's an example:

hoverLink.MouseEnter();

Upvotes: 0

Related Questions