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.