-->

How to manipulate a control without any pattern im

2019-05-27 02:04发布

问题:

I'm trying to implement the automation test via UIAutomation for our project. But lots of the controls are not standrad, and proper patterns are also not implemented for that controls. How should I to manipulate the controls via UIAutomation framework in this case?

For example, a button in our product is implemented via a Pane, and the invoked pattern is not implemented as well. How should I click the button? (To avoid installing VS on the test machine, I don't want to use Mouse.Click() in Microsoft.VisiualStudio.TestTools.UITesting namespace) Is there a way to do that only using UIAutomation framework or something else embedded in .net framework? Thanks in advance! (If the proper pattern is implemented, Below code will work. And as a new user, I cannot post the screenshot for your reference, sorry!)

object temp = null;
if (btnTest.TryGetCurrentPattern(InvokePattern.Pattern, out temp))
{
    InvokePattern btnTestPattern = temp as InvokePattern;
    btnTestPattern.Invoke();
}

回答1:

The only way to interact when Control Patterns are not implemented is to go clicking around stuff. I would suggest try following to avoid maximum errors.

  1. Before sending the click, make sure the parent of button(pane or window is set to foreground)
  2. Instead of sending the click to corner of the AutomationElement, try sending it in midpoint, of the element,
  3. Also, try hovering over the element first, the wait like 200ms, and then send click, So that you are sure to see execution.[Trust me, this helps debugging a lot and avoids many issues.]


回答2:

The best thing would be, if those guys who implement the system would implement server-side UIA provider to their UI Elements!

But often that's not possible..., I used the following workaround (at least for clicking/toggling):

AutomationElement yourAE = ...// some code to find the right AutomationElement (AE)
clickablePoint = yourAE.GetClickablePoint();

also BoundingRectangleProperty could be of help

If you receive that clickable point you can use

System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)clickablePoint.X, (int)clickablePoint.Y);

to move to the location, and than click it via InputSimulator or some win32 (user32.dll) commands. (note: of course you can also use InputSimulator or win32 to move the mouse - but I had some problems with the InputSimulator when it came to several screens with different locations or resolutions - so Cursor.Position was the easiest approach, which is also very reliable)