Appium 无法定位折叠的元素

Appium can't locate collapsed element

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

我不明白为什么它在树中可用却找不到。或者还有其他为什么要获得这种访问权限?

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.

非常感谢任何指点,
谢谢

我对 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 framework 应用程序相比,自动化同行回答查询的方式存在巨大差异。如果自动化对等点在屏幕外(控件的可见性已设置为折叠或隐藏),则不会回答询问,除非询问针对的是原始视图。所以我们必须强制查询目标使其工作。

        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"));
        }

有好几次我使用 Snoop 发现 Appium 可以访问更多 elements/attributes。

这就是我现在使用生成 XPath 的 Accessibility Insights for Windows. Of course you can use inspect.exe, but Accessibility Insights for Windows is the recommended from Microsoft. You can also try WinAppDriver UI Recorder 的原因。
如果您的元素未出现在 Windows 的 Accessibility Insights 中,Appium 将无法找到它。

根据文档:

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

折叠的元素未呈现且不在布局中。

为什么不使用 Visibility.Hidden 代替,或者 Visibility.Visible 高度为 0px。