UIACOMWRAPPER未能返回以前崩溃的WPF控制的子节点



我正在尝试在WPF应用程序上创建编码的UI测试。我正在使用Visual Studio 2019创建/运行测试。

我正在遇到一种奇怪的行为,在启动时崩溃的wpf按钮(但后来使可见/启用(没有使用任何与该按钮关联的自动化元对象可用的Findxxx方法显示任何子节点。其他未崩溃的按钮似乎没有这个问题。我应该注意,我期望此WPF按钮的子节点的原因是,在XAML中,它的定义类似于以下内容:

<Button x:Name="ButtonStop" Grid.Row="0" Grid.Column="2" Command="{Binding TheVm.StopCommand}">
  <Button.Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyleA}">
      <Setter Property="Visibility" Value="Collapsed"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding TheVm.DisplayButton}" Value="False">
          <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
  <StackPanel Style="{StaticResource ControlsStackPanelStyle}">
    <Image Source="pack://application:,,,/Assets/icon1.png" Style="{StaticResource ControlsButtonImageStyle}"/>
    <ContentPresenter Content="Stop" Style="{StaticResource ControlsButtonTextStyle}"/>
  </StackPanel>
</Button>

使用Inspect.exe应用程序,我可以正确看到此按钮的子节点,但是当我穿越自动化元素时,我可以访问它们。

我用来检查可读文本的测试代码是:

// Wait for 'Stop' button to become enabled, and verify correct text
uIButtonStopButton.WaitForControlEnabled();         
var displayText = (!uIButtonStopButton.DisplayText.Equals(""))
                   ? uIButtonStopButton.DisplayText 
                   : GetFirstNodeText(uIButtonStopButton.NativeElement as AutomationElement;
Assert.AreEqual("Stop", displayText, "Stop button doesn't have correct text.");

getFirstNodeText方法如下:

private static string GetFirstNodeText(AutomationElement automationElement)
{
  if (automationElement != null)
  {
    // Get first AutomationElement node that is a 'text' control-type
    var textEl = automationElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "text"));
    if (textEl != null) return textEl.Current.Name;
  }
  return "";
}

另外(有趣的(信息:我使用Appium/WinappDriver尝试了类似的测试,并且几乎具有相同的经验 - 以前倒塌的按钮上没有孩子节点。

什么可能导致这一点,您对此有任何建议吗?

要验证您正在使用最新的AutomationElement对象,请务必检查有关刷新控制的问题。

但是,由于您提到使用WinAppDriver遇到了几乎相同的问题,因此我宁愿在测试的应用程序中出现问题。如果您可以访问从事该代码的源代码/开发人员,请仔细查看涉及此按钮及其子女的代码/XAML。这个问题很可能会在那里找到。

最新更新