【问题标题】:Appium can't locate collapsed elementAppium 找不到折叠的元素
【发布时间】:2021-10-25 18:00:20
【问题描述】:

我正在尝试通过自动化测试访问自定义提示对话框的 Type 属性。所以类型的元素(文本框或文本块)被折叠了,因为没有人需要看到它,我只需要它在自动化方面进行逻辑处理。

我不明白为什么尽管它在树中可用,但无法找到它。或者还有其他为什么要获得这样的访问权限?

XAML:

 <controls:PromptDialog ...
                   AutomationProperties.AutomationId="PromptView"
                   d:DataContext="{d:DesignInstance Type=viewModels:PromptViewModel}">


<Grid Margin="{StaticResource MarginThickness}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="{StaticResource Gutter}" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <!--Prompt-->
    <Grid Grid.Row="0"
          Visibility="{Binding IsShowingPrompt, Converter={StaticResource BooleanToVisibilityConverter}}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="4" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="{StaticResource Gutter}" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!--Image-->
        <ContentControl Grid.Row="0"
                        Grid.Column="0"
                        Content="{Binding}">
            <ContentControl.Resources>
                <DataTemplate DataType="{x:Type viewModels:PromptViewModel}">
                    <Image Name="Image" />
                   //displaying image per type
            </ContentControl.Resources>
        </ContentControl>

        <ScrollViewer Grid.Row="0"
                      Grid.Column="2"
                      Focusable="False"
                      VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Disabled"
                      MaxHeight="400">
            <StackPanel>
                <TextBlock behaviors:TextBoxHyperlinkBehavior.Text="{Binding Text}"
                           TextWrapping="Wrap"
                           Focusable="False"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left"
                           FontFamily="{Binding Font.Name}"
                           Foreground="{Binding FontColor}"
                           AutomationProperties.AutomationId="PrompView_Text" />
                <TextBox Visibility="Collapsed"
                           Text="{Binding Type}"
                           AutomationProperties.AutomationId="PromptView_Type" />
            </StackPanel>
        </ScrollViewer>
        

    <!--Commands-->
    <UniformGrid Grid.Row="2" Rows="1" HorizontalAlignment="Right">
        // buttons
    </UniformGrid>
</Grid>

Appium:

public IVisualElement Type => _appiumSession.CreateVisualAppiumElement("PromptView_Type");

或者

public AppiumWebElement Type => _appiumSession.FindElementByAccessibilityId("PromptView_Type");

WPF 窥探:

错误:

OpenQA.Selenium.WebDriverTimeoutException : Timed out after 10 seconds
    ---- OpenQA.Selenium.WebDriverException : An element could not be located on the page using the given search parameters.

高度赞赏任何指针,
谢谢

【问题讨论】:

  • 折叠的 ui 片段不在可视化树中。它们没有被渲染。
  • wpf 中有逻辑树和可视树。折叠的 uielement 在逻辑树中但不在可视树中。
  • @Andy 有什么方法可以访问逻辑元素吗?
  • 您可以遍历逻辑树,但您应该首先了解它是什么。谷歌并仔细阅读 msdn 文档。事情是这样的。为什么不渲染需要测试的 uielement?这对我来说没有多大意义。
  • 该类型对最终用户没有用,因此无需显示它,信息以提示图像(错误、警告等)表示。但我需要它进行测试。我认为它会像 PHP 隐藏标签一样。我正在搜索和阅读与这个问题并行的文档,也许我可以节省一些时间。无论如何谢谢:)

标签: wpf testing automation appium


【解决方案1】:

我有好几次使用Snoop,它发现了更多Appium可以访问的元素/属性。

这就是我现在使用Accessibility Insights for Windows 的原因。当然你可以使用inspect.exe,但是微软推荐Accessibility Insights for Windows。您也可以尝试生成 XPath 的 WinAppDriver UI Recorder
如果您的元素不在 Accessibility Insights for Windows 中,Appium 将无法找到它。

【讨论】:

    【解决方案2】:

    根据文档:

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.visibility?view=net-5.0

    折叠的元素不渲染也不在布局中。

    为什么不改用 Visibility.Hidden,或者高度为 0px 的 Visibility.Visible。

    【讨论】:

      【解决方案3】:

      我对Appium一无所知,但是使用.net自动化api可以很容易地解决这个问题。

              // Must find top window of app first or could cause overflow easily.
              var topWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
              new PropertyCondition(AutomationElement.AutomationIdProperty, "Id of top window"));
      
              // Find out your target element if it's in visual tree, whatever it's collapsed or not.
              // You can also Find out another element in the middle first to reduce the search work.
              var target = topWindow.FindFirst(TreeScope.Descendants,
              new PropertyCondition(AutomationElement.AutomationIdProperty, "PromptView_Type"));
      
              // retrieve the value.
              var val = ((ValuePattern)target.GetCurrentPattern(ValuePattern.Pattern)).Current.Value;
      

      您必须添加对 UIAutomationClient.dll 和 UIAutomationType.dll 的引用才能使此代码正常工作。


      .net 核心应用更新

      对于 .net core(3.0, 3.1, 5.0),自动化同行回答查询的方式与 .net 框架应用程序存在巨大差异。如果它在屏幕外(控件的可见性已设置为折叠或隐藏),自动化对等将不会回答查询,除非查询针对的是原始视图。所以我们必须强制查询目标使其工作。

              var topWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Id of top window"));
              AutomationElement target = null;
              var cr = new CacheRequest()
              {
                  TreeFilter = Automation.RawViewCondition,
              };
      
              using (cr.Activate())
              {
                  target = topWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "PromptView_Type"));
              }
      

      【讨论】:

      • 感谢您提供的信息。我试过了,但target 仍然为空。我添加了一个中间元素来减少你的 cmets 中的搜索,但仍然为空
      • 您是否有任何有关查找目标元素的资源“无论它是否折叠”我在 Microsoft 文档中找不到任何内容。
      • @Katia 没有关于可见性如何影响文档搜索结果的信息,但您可以进行一些简单的测试来确认这一点。我很好奇的是,你能用我的代码找出目标的任何祖先吗,比如你在 WPF snoop 的屏幕截图中显示的 StackPanelGrid
      • 我确实测试了它,当元素可见时,您的代码可以工作,但如果它被折叠它是空的。我还在topWindow 和`target 之间添加了一个中间步骤来获取父提示元素并找到它。很难相信我是第一个面对这个问题的人,而且在没有相关文档的情况下我已经搜索了一段时间了。再次感谢您的意见
      • @Katia 这很奇怪,因为我可以找到任何被这些代码折叠或隐藏的元素。我的代码仅在元素不在可视树中时才起作用,例如在尚未导航到的 Page 中或未展开的 Expander 中。你能告诉我更多关于这个PromptView_Type的代码吗?
      猜你喜欢
      • 2019-09-24
      • 2020-09-15
      • 1970-01-01
      • 2015-02-09
      • 2023-03-18
      • 2020-10-01
      • 2018-09-21
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多