有没有办法在编码的UI中找到以前的兄弟姐妹

本文关键字:兄弟姐妹 UI 编码 有没有 | 更新日期: 2023-09-27 18:33:19

我有一个控件,它是父级的第一个子项,但在父级和控件中没有什么是唯一的。我可以找到 Scond 孩子的独特属性,所以我需要找到第二个孩子的前兄弟姐妹

有没有办法在编码的UI中找到以前的兄弟姐妹

这可以通过使用控件上的 SearchConfiguration 属性来实现。你要做的是搜索标签,然后实例化 WinEdit 控件并将其传递到标签搜索控件中。接下来,在 WinEdit 实例上设置 SearchConfiguration.Add(SearchConfiguration.NextSibling)现在,这将搜索文本标签的下一个同级。在代码示例中,如下所示:

 var app = ApplicationUnderTest.Launch(@"yourapplication.exe");
 var mainWindow = new WinWindow(app);
 mainWindow.WindowTitles.Add("Form1");
 WinText textLabel = new WinText(mainWindow);
 textLabel.SearchProperties.Add(WinControl.PropertyNames.Name, "Some Text Label");
 WinEdit siblingEditControl = new WinEdit(textLabel);
 siblingEditControl.SearchConfigurations.Add(SearchConfiguration.NextSibling);
 siblingEditControl.Text = "setting the text";

没有直接的方法可以得到兄弟姐妹。查找控件的同级的一种方法是查找其父级,然后查找该父级的所有子项。接下来搜索子控件以找到当前控件,然后获取前一个控件。该方法可能基于以下内容。它使用控件的Name字段进行比较,这对于一般情况可能是不正确的,我建议尝试其他值。

public UITestControl GetPreviousSibling(UITestControl uitc)
{
    UITestControlCollection siblings = uitc.GetParent().GetChildren();
    // Note that 'sibings[0]' has no predecessor
    for (int ii=1; ii<siblings.Count)
    {
        if (uitc.Name == siblings[ii].Name)
        {
            return siblings[ii - 1];
        }
    }
    return null;
}

"兄弟姐妹"的定义并不明确。此 MSDN 博客提供了兄弟姐妹的一些详细信息。