0

I'm trying to use UI Automation to click a simple button in a 3rd party application. I have initialized the IUIAutomation object and retrieved an IUIAutomationElement by using the ElementFromHandle() function with the handle of the 3rd party application window.

But I really can't figure out how to use this IUIAutomationElement to find the button and issue a leftbutton click on it..

This is my code so far:

void Control::clickButton()
{
    for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); i != mainDetector.getWindowList().end(); ++i)
{
    if (i->getTitle().find("PokerStars Lobby") != std::string::npos)
    {
        parentWindowHandle = (HWND)i->getHandle();
    }
}

InitializeUIAutomation(iUiAutomation);
(*iUiAutomation)->ElementFromHandle(parentWindowHandle, iUiAutomationElement);

}

Hope someone can help me how to continue from here to actually click the button

Zitrax
  • 19,036
  • 20
  • 88
  • 110
Julian G
  • 9
  • 6
  • [This thread](http://stackoverflow.com/questions/31813622/invoke-on-click-on-a-button-using-ui-automation-with-no-invokepattern-or-clickab) may be helpful. This is an example for InvokePattern usage though sometimes it may not work. – Vasily Ryabov Jan 20 '16 at 11:22
  • Possible duplicate of [Given an Automation Element how do i simulate a single left click on it](http://stackoverflow.com/questions/10105396/given-an-automation-element-how-do-i-simulate-a-single-left-click-on-it) – Vasily Ryabov Jan 20 '16 at 11:24

1 Answers1

0

You may find it simpler to use TestStack.White (also available from NuGet) to do this kind of thing. It wraps up a lot of UIAutomation to make it easier to write tests that drive Windows applications.

To find the target button you can use "UIVerify" or "inspect" from the Windows 10 SDK which lets you view the UI automation tree for all windows on the desktop. Once you locate the button of interest you can note its AutomationId or other properties to use as search parameters to one of TestStack.White's search commands or an IUIAutomationTreeWalker instance.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Another alternative is http://ldtp.freedesktop.org/ which in addition works on Mac and Linux. – Zitrax Jan 18 '16 at 10:55