我在UIStore应用程序的UI自动化方面苦苦挣扎。我实现IValueProvider后立即得到UITestControlNotAvailableException。如果我删除它的实现它。
StackTrace看起来像这样:
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.MapAndThrowException(Exception e, IUITechnologyElement element, Boolean useRethrowException)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.GetAutomationPropertyValue[T](AutomationElement element, AutomationProperty property)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.GetAutomationPropertyValue[T](AutomationProperty automationProperty)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_NativeWindowHandle()
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_WindowHandle()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_IsRefetchRequired()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.EnsureValid(Boolean waitForReady, Boolean refetch)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValuePrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyPrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyOfType[T](String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Name()
at UITest.Framework.Windows.TestGround.GetElementString(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.PrintTree(Modes mode, XamlWindow window) in ...
在应用程序端,我使用此代码获取有关控件的信息:
public class CanvasAP : FrameworkElementAutomationPeer, IValueProvider
{
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
{
return this;
}
return base.GetPattern(patternInterface);
}
#region Implementation of IValueProvider
bool IValueProvider.IsReadOnly => true;
string IValueProvider.Value
{
get
{
var str = "Test";
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
foreach (var child in owner.Children)
{
str += $"{child.GetType()}";
}
return str;
}
}
void IValueProvider.SetValue(string value)
{
}
#endregion
}
在UI自动化客户端,我使用此代码获取有关控件的信息:
private static string GetElementString(UITestControl element, Int32 level = 0)
{
var xaml = element as XamlControl;
var str = "";
for (var i = 0; i < level; i++)
str += " ";
str += $"{element.ControlType} {element.ClassName} {element.Name} {xaml?.AutomationId ?? ""}\n";
return str;
}
答案 0 :(得分:0)
发现我犯过的错误 - 它是在调用base.GetPattern
而不是base.GetPatternCore
。
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
return this;
return base.GetPatternCore(patternInterface);
}