0

We are using Microsoft's UIAutomation stuff to implement some automated UI tests, and that's working reasonably well, but now we're running into a limitation that we can't figure out how to get around.

We've subclassed TextBox to add an "IsValid" property, which indicates whether the value the user has entered is valid or not (duh). It would, of course, be great if the UI tests could check if the TextBox believes the entered value is valid or not.

The workaround we've come up with, looks something like this (I've left out some of the non-pertinent details):

internal class ValidatingTextBox : TextBox
{
   internal bool IsValid;

   internal ValidatingTextBox()
   {
   }
   protected override AutomationPeer OnCreateAutomationPeer()
   {
      return new ValidatingTextBoxAutomationPeer(this);
   }
}
internal class ValidatingTextBoxAutomationPeer : TextBoxAutomationPeer, IToggleProvider
{
   ValidatingTextBox TextBox;

   internal ValidatingTextBoxAutomationPeer(ValidatingTextBox textBox)
   {
      TextBox = textBox;
   }
   public override object GetPattern(PatternInterface patternInterface)
   {
      if (patternInterface == PatternInterface.Toggle)
         return this;
      else
         return base.GetPattern(patternInterface);
   }
   public ToggleState ToggleState
   {
      get
      {
         if (Owner is not ValidatingTextBox box)
            return ToggleState.Indeterminate;
         else
            return box.IsValid ? ToggleState.On : ToggleState.Off;
      }
   }
}

That works, it's just a bit clunky as the UI tests have to be heavily commented saying that Toggle_On means valid and Toggle_Off means invalid.

We noticed that when we implemented UI automation for a custom grid control, the IGridProvider.GetItem() function returns a class that implements IRawElementProviderSimple and that's got a function GetPropertyValue() that would probably work for us, except that we can't figure out where the IRawElementProviderSimple is for TextBoxes. There is a question on StackOverflow about creating a custom property but it never got answered.

We also wondered about implementing a custom pattern, and there's a question on StackOverflow about that, but the GitHub repo it links to seems insanely complicated.

What's the best/easiest/fastest/simplest way to get the "IsValid" value back to the UI test harness?

EDIT

Trying to implement a custom pattern, and ran into trouble there. Created a separate question for that.

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68
  • Implementing custom properties, events or patterns is indeed complicated. Here is the official documentation (in native code but it's the same idea in .NET) https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-regcustompropseventpatterns But if you only need a property like IsValid, you can use for example UIA_IsDataValidForFormPropertyId https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-automation-element-propids – Simon Mourier Jun 11 '22 at 06:34
  • @SimonMourier The IsValid property is great, but the only interface that supports GetPropertyValue() is IRawElementProviderSimple and I don't see any way to have my ValidatingTextBox implement that. Is there some other interface that ValidatingTextBox can impement, that my client will be able to talk to? – Betty Crokker Jun 13 '22 at 13:58
  • What framework are you using? WPF? Else? – Simon Mourier Jun 13 '22 at 17:45
  • 1
    WPF WPF WPF WPF WPF (to reach the StackOverflow 15 character minimum) – Betty Crokker Jun 13 '22 at 17:56
  • The problem is WPF is now pretty much frozen since 15+ years or so with a hardcoded list of properties: https://referencesource.microsoft.com/#UIAutomationTypes/System/Windows/Automation/AutomationElementIdentifiers.cs,37 – Simon Mourier Jun 13 '22 at 19:07

0 Answers0