views:

397

answers:

3

There are some buttons and menus in the application I am testing that require actual clicks to send all the information through and FireEvent style clicking does not seem to cut it. Both TestPartner and QTP can use actual clicks. TestPartner does this always (slow) and QTP does this by changing the 'ReplayType' to 2 from 1.

Setting.WebPackage("ReplayType") = 2

So my question is. Is there anyway to get real clicks in WatiN or Selenium? I have not been able to find that option.

Thank You, Josh

+1  A: 

When ReplayType is set to device (2), QTP actually moves the mouse and performs a click (you can see the mouse cursor move). I'm not very familiar with Selenium but from what I know replay is done in pure JavaScript therefore it can fire DOM events (as QTP does in event ReplayMode) but it can't simulate a real click the way QTP does.

Motti
+3  A: 

Selenium 2 can fire real clicks instead of trying to synthesize the clicks and keystrokes like Selenium 1 does. It does a lot of interaction with the OS to do these things natively which over comes a lot of the Selenium 1 issues like is it firing a click or mouseup or mousedown?

Selenium 2 is in its 2nd alpha phase and is quite stable at the moment. You can get Selenium 2.0a2 from http://code.google.com/p/selenium/downloads/list.

I have been working on the .NET bindings and am really happy with the way they are going.

AutomatedTester
Selenium 2 does look like a good possible solution. I will have to give it a shot. Thanks AutomatedTester.
Josh Harris
I have a couple tutorials on my site http://www.theautomatedtester.co.uk/selenium_training.htm if you wanna have a look
AutomatedTester
Selenium 2 gets closer to what I need. Just need to figure out how to get around the hover() issues with my mouseover menus.
Josh Harris
+1  A: 

With WatiN, it is possible to get to the "native" interface of elements in IE windows. Using that you can get the screen coordinates of any element.

using mshtml; //Add a reference to Microsoft.mshtml that comes with WatiN.
using WatiN.Core.Native.InternetExplorer;

public static System.Drawing.Point GetScreenPoint(IEElement element)
{
    IHTMLElement nativeElement = element.AsHtmlElement;
    IHTMLElement2 offsetElement = (IHTMLElement2)nativeElement;
    IHTMLRect clientRect = offsetElement.getBoundingClientRect();
    IHTMLDocument2 doc = (IHTMLDocument2)nativeElement.document;
    IHTMLWindow3 window = (IHTMLWindow3)doc.parentWindow;

    int windowLeft = window.screenLeft;
    int windowTop = window.screenTop;
    int elementLeft = clientRect.left;
    int elementTop = clientRect.top;
    int width = nativeElement.offsetWidth;
    int height = nativeElement.offsetHeight;

    int clickX = windowLeft + elementLeft + (width / 2);
    int clickY = windowTop + elementTop + (height / 2);

    return new System.Drawing.Point(clickX, clickY);
}

To use it:

Button myButton = browser.Button(Find.ById("myButton"));
System.Drawing.Point clickPoint = GetScreenPoint((IEElement)myButton.NativeElement);
MyClick(clickPoint);

Obviously the element has to be visible. I don't know how to check for that offhand, but I think you can change the scrollTop and scrollLeft attributes of the body to scroll around.

You'll also need a method to actually do the click, but there's probably an answer for that already on stackoverflow.

JamesH
Thank you JamesH. This does look like it could come in handy, but finding the object was not the problem it was the "real" click that seemed to fire some events that a normal "onclick" or "onmousedown/up" did not fire.
Josh Harris
Maybe I could have had more detail in the explanation. My idea was to find the screen coordinates to click on and then use a system function to do the click. Something like SendInput: http://pinvoke.net/default.aspx/user32/SendInput.html. Something lower level like that should be darn close to simulating a real mouse click.
JamesH