Click WPF button from another application - c#

Was thinking it would be an easy task, but it proved to be far from that.
Spy++ doesn't generate control ID's (Only shows main window), each control has a name. Example(Button->"AddButton")
I've tried using invoked methods such as SendMessage, and i get the main window handle just fine, but always get a 0 when trying to get control handles.
I heard about UI Automation library, but didn't find any clear examples of usage for my specific task.
My Goal:Being able to retrieve data (Such as data grid cells texts), and click buttons on a WPF application, remotely from another application (I'll be using C#)
Thanks in advance!
EDIT: I've tried Ranorex Spy and was able to click the control just fine:
Press button from Ranorex
How can i simulate it?
EDIT 2:
I managed to do it after further reading some documentations, hopefully it will help other people:
string mainTitle = "";
string controlName = "";
AutomationElement prog = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, mainTitle));
AutomationElement btn = prog.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, controlName));
InvokePattern clickBtn = (InvokePattern) btn.GetCurrentPattern(InvokePattern.Pattern);
clickBtn.Invoke();

You can indeed use UI Automation which was (kinda) designed to do this kind of stuff. There is also a NuGet-Package which does some work for you already, called TestStack.White. Originally it is designed for automated UI testing, but I don't see why you couldn't use it for doing stuff like that. You can find out more about the Package here, and more about UI Automation here.

Related

How to set Word Application.Selection focused

I am building a Word VSTO Add-in. There I have a side drawer with some buttons on it. Now when I click on a button it inserts a predefined text in a document selection position. When it inserts the text I would like to bring back focus to the document itself.
How to do that?
My best try so far:
Word.Selection currentSelection = Application.Selection;
currentSelection.Text = ((Button)sender).Tag.ToString();
Application.ActiveWindow.SetFocus();
Application.ActiveWindow.Activate();
It makes sense to call the Activate method prior calling the SetFocus one:
Word.Selection currentSelection = Application.Selection;
currentSelection.Text = ((Button)sender).Tag.ToString();
Application.ActiveWindow.Activate();
Application.ActiveWindow.SetFocus();
Also you may find Windows API functions helpful for such scenarios. For example, the SetForegroundWindow function which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. You may find the Win32: Bring a window to top thread helpful.
Try Application.ActiveDocument.Activate.

How to simulate keystroke in WPF, but outside the application?

Currently I used this snip code as a result from googling.
var eventArgs = new TextCompositionEventArgs(Keyboard.PrimaryDevice,
new TextComposition(InputManager.Current, Keyboard.FocusedElement, "A"));
eventArgs.RoutedEvent = TextInputEvent;
var flag = InputManager.Current.ProcessInput(eventArgs);
It was working if I used Keyboard.Focus(TxtBox); and the TxtBox will be filled with the Keystroke.
But what I want really achieved is:
1.Drawing a box (for example, I draw box on one of the excel cell)
2.Click on the box coordinate (to change Keyboard Focus)
3.Send Keystroke to clicked excel cell
I have done step 1 and 2.
But I can't find a way to do the third step.
Somehow, the click event (using mouse event) maybe not changing Keyboard Focus automatically.
So, how do I change Keyboard focus, if possible using coordinate ?
Or maybe can I get IInputElement from a coordinate ? and then set keyboard focus to it.
Of course, all of it outside from the main application window of the WPF.
Found it !
At:
Installed InputSimulator via NuGet, no members accessible
It is working in most cases.
I said in most cases, because it is able to type in other window like excel application, but on other custom app window. There might be a case it won't work.
Hope it help for other people, looking for the same thing.

How to Send sellect all command by ui automation c#

We are working on UI Automation for the third party software. And we are firing button by the invoke method.
AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
AutomationElement Addbutton = GetElementByNameProperty(appElement, "Add");
if (Addbutton != null)
{
ClickElement(Addbutton);
}
Now i want to fire select all or (ctrl+A) command for selecting the element of the third party software. How to possible by UI automation?
or
Can I fire command of some menu? for example (Edit> Select All). Can I fire select all command by the UI automation?
Please help me.
thanks
If you are just trying to automate sending the command Ctrl+A, you may want to check out the question below. One of the suggestions is to use SendKeys and it works for me.
How to send keys Control + A + B? (keep Control modifier "pressed")

Invoke on click on a button using UI Automation with no InvokePattern or clickablePoint

i'm try to send click message to (or invoke) a button in another application .
i used UISpy.exe and could find the element which i need.
but it has no id,no clickablePoint and no Invoke pattern.
i tried following code:
var processStartInfo = new ProcessStartInfo(#"tdesktop\Program.exe");
var proc = Process.Start(processStartInfo);
Thread.Sleep(3000);
AutomationElement mainWin = AutomationElement.RootElement.FindChildByProcessId(proc.Id);
List<AutomationElement> elmList= GetChildren(mainWin);
//MessageBox.Show(elmList.Count.ToString());
if (elmList.Count == 7)
{
List<AutomationElement> menubar= GetChildren(elmList[6]);
AutomationElement elementNode = menubar[1];
double x = elementNode.GetClickablePoint().X;
double y = elementNode.GetClickablePoint().Y;
win32 w = new win32();
w.move_left_click((UInt32)x, (UInt32)y);
}
it throws an exception in elementNode.GetClickablePoint().X that the Autumation Element has no clickable point.
i tried also TryGetInvokePattern() but still throws execption it has no InvokePattern.
i use VS2012 and .net 4.5
is there any way to make this?
As was already suggested, I'd strongly recommend pointing the Inspect SDK tool to the UI you're interested in. The tool (inspect.exe) be found in places like "C:\Program Files (x86)\Windows Kits\8.1\bin\x64".
By using the tool, you can see how the UI element of interest is being exposed programmatically. For example, is it exposed as a single UIA element, or it is part of a larger UIA element which represents a set of UI elements shown visually, and what UIA patterns does it support? As a test, I just pointed Inspect to an arrow shape in Paint. The results are shown below.
So I can tell that the arrow is exposed programmatically as a single UIA element, and that it supports the Invoke pattern. This means I can programmatically invoke the button through UIA. (And I can call the pattern methods on the element from inside Inspect’s Action menu, which is pretty handy.) If the UIA element didn't support any patterns that would allow me to programmatically control it, I can find its BoundingRectangle property through UIA, and simulate a mouse click in the middle of that to invoke it. (And I'm assuming the button's not obscured when I simulate the mouse click.)
But if I look at another group of elements shown visually on the screen, using Inspect I can learn that the whole set is exposed through UIA as a single UIA element. So in the image of Inspect shown below, I can learn that I'm not able to programmatically invoke a specific color in that group.
So in that case, I'd probably have to assume I know the size and layout of the UI elements shown visually in that group, and simulate a mouse click at some point which I think is appropriate based on the color I want to invoke.
By using Inspect, I can get a good understanding of what my options are. Ideally a single element shown visually on the screen will be exposed through UIA as a single element that I can control through whatever patterns are relevant, (for example, Invoke, Toggle, SelectionItem etc). But if useful patterns aren't supported, then I could consider simulating mouse clicks instead, based on whatever ClickablePoint or BoundingRectangle data's exposed.
Thanks,
Guy
A menu bar doesn't expose the InvokePattern (see UI Automation Support for the MenuBar Control Type). However, a menu item can be Invoked (see UI Automation Support for the MenuItem Control Type).
The following code illustrates how to generate a list of menu items:
AutomationElementCollection items = menubar.FindAll(
TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem));

Off screen element teststack white

I need to automate 3rd party WPF application. I use
TestStack/White. This application has menu bar that is presented by images. After some actions menu is changing. There presents new images. When I want to click on new image:
Window mainWindow = application.GetWindow("Main window", InitializeOption.NoCache);
Image newTask = mainWindow.Get<Image>(SearchCriteria.ByControlType(ControlType.Image).AndIndex(2));
newTask.Click();
I get exception:
TestStack.White.AutomationException: Cannot perform action on Image.
AutomationId:, Name:, ControlType:image, FrameworkId:WPF, element is
offscreen.
I use Microsoft Inspect for research elements.
When I start tests, Inspect show me that image is offscreen. But if I do these actions manually, it works perfectly and in Inspect this image is not offscreen.
How can I refresh these elements or clear cache of window?
There are ReInitialize and ReloadIfCached methods on Window object. Try those to see if something changes.
Are you sure AndIndex(2) is correct element in that particular situation?
Try using GetMultiple and iterate the collection to see what images you actually have and which are not Offscreen.
WPF automation with White is pretty hard. Try Telerik Testing Framework and White could be supporting framework. It is much more easier that way.
It can be a problem with focus, try to use this before getting image:
mainWindow.Focus(DisplayState.Maximized);
Not an exact answer but the final solution I've got after all these TestStack.White not found elements, table rows, and so on. I started to move it to FlaUI. If something does not work or unstable with White then most likely I can get more stable and fast-executable FlaUI solution.
Fortunatly, such migration can be done with little steps. For example I already have TestStack.White.Application app, then I replace White portion of code with FlaUI like this:
var flApp = FlaUI.Core.Application.Attach(app.Process.Id);
using (var automation = new UIA3Automation())
{
// .. new UI element processing
}
I don’t think that caching is the problem here. You are getting the mainWindow with InitializeOption.NoCache. In Non-cache mode the controls are found on demand. So I presume that the cache is refreshed automatically. (https://github.com/TestStack/White/blob/master/src/TestStack.White/Factory/InitializeOption.cs)
Perhaps the index of the element you want to click is not 2.
Have you tried adding an explicit wait? It sounds like you have only tried adding an implicit wait.(https://github.com/TestStack/TestStack.docs/blob/master/_source/White/Advanced%20Topics/Waiting.md)

Categories

Resources