搜索儿童控制

时间:2014-09-24 15:10:41

标签: c# wpf coded-ui-tests

我在Windows商店应用程序中使用编码ui测试。

我的控件层次结构是:

UIPearsonPOCCommonViewFlipViewItem(XAMLFlipViewItem - > UIWebViewPane(XAMLWebViewPane) - >其余内容。

对于其余的子控件,没有特定的自动化ID或唯一的名称,它们看起来像html控件,例如参考附加的图像。

我想迭代UIWebViewPane的子节点并访问具有innerText的子DIV。

我对编码ui测试相对比较新。我无法迭代UIWebViewPane(XAMLWebViewPane)的子代

enter image description here

1 个答案:

答案 0 :(得分:3)

如果子控件的内部文本是唯一的,您始终可以使用定义中的父控件对其进行搜索。例如:

public HtmlControl child()
{
    HtmlControl parent = new HtmlControl(browser);
    parent.SearchProperties["id"] = "[my id]";

    HtmlControl child = new HtmlControl(parent);
    child.SearchProperties["innerText"] = "[the inner text]";
    return child;
}

如果你真的想要迭代,那么你必须使用UITestControl类的.GetParent()和.GetChildren()方法来抓取结构。

public HtmlControl child()
{
    //First, we create an empty HtmlControl to return.
    HtmlControl result = new HtmlControl()

    //Specify the parent and get a collection of the children (this only goes one level, 
    //   so if you have to go deeper, you'll have to nest your foreach loops and get
    //   children of the children, etc.
    HtmlControl parent = new HtmlControl(browser);
    parent.SearchProperties["id"] = "[my id]";
    UITestControlCollection children = parent.GetChildren();

    foreach (UITestControl child in children)
    {
        // If the child has the text you're looking for, then assign it to the result
        // object and break the loop.
        if (child.GetProperty("InnerText").ToString().Equals(searchTerm))
        {
            result = (HtmlControl)child;
            break;
        }
    }

    return result;
}

就个人而言,我尝试第一种选择。不过,最好的办法是(礼貌地)要求开发人员在HTML中添加一些独特的静态标记。