使用CodedUI在Web应用程序中测试树视图(Visual Studios 2013 C#)

时间:2014-07-08 14:36:45

标签: c# visual-studio-2013 treeview coded-ui-tests

如标题中所述,我正在为Web应用程序编写/记录基于C#的CodedUI测试。我记录/映射有问题的控件是TreeView控件。测试的问题是选择特定元素。

作为一个例子,我有一个树形视图,其中包含以下设置:

    • A1
    • A2
    • B1
    • B2
  • Ç
    • C1
    • C2
  • d
    • D1
    • D2
  • 电子
    • E1
    • E2

让我们说我想选择元素D1。我开始录音我按下" D"旁边的箭头打开D类; (Categoryname),然后继续选择D1。一旦我重播实例,而不改变任何代码,打开的类别是A,选择的元素是" D" (类别组)。所以我认为箭头没有指定它扩展的类别,这就是为什么它打开类别" A"。我不知道它为什么选择类别" D"。

我试图改变" SearchProperties"在代码中,但没有任何运气。我曾尝试使用TagInstance,但导致测试根本找不到任何控制。我试图设置其他SearchProperties(和FilterProperties),但没有任何运气。

是否有人遇到并解决了这个问题?如果是这样,你介意帮助我。谢谢!

PS。 我读了一些,有人说通过设置" PlaybackSettings.MatchExactHierarchy"可以解决问题。为真,但它没有用。

编辑;

我很遗憾无法访问生成控件的代码,但我可以发送控件的图像和生成的搜索属性。

Treeview control looks like this

以及为此类别定义的搜索属性" ServiceDesk":

this.mUIItemPane.SearchProperties[HtmlDiv.PropertyNames.Class] = "k-mid";
this.mUIItemPane.SearchProperties[HtmlSpan.PropertyNames.Class] = "k-icon k-plus";
this.mUIItemPane.SearchProperties[HtmlSpan.PropertyNames.TagName] = "SPAN";
this.mUIItemPane.SearchProperties[HtmlSpan.PropertyNames.ControlDefinition] = "class=\"k-icon k-plus\" role=\"presentation\"";

(请注意,这是类别名称旁边的箭头的SearchProperties" ServiceDesk"而不是文本本身)

并为用户" Farhad":

this.mUIFarhadPane.SearchProperties[HtmlDiv.PropertyNames.Id] = null;
this.mUIFarhadPane.SearchProperties[HtmlDiv.PropertyNames.Name] = null;
this.mUIFarhadPane.FilterProperties[HtmlDiv.PropertyNames.InnerText] = "Farhad";
this.mUIFarhadPane.FilterProperties[HtmlDiv.PropertyNames.Title] = null;
this.mUIFarhadPane.FilterProperties[HtmlDiv.PropertyNames.Class] = "k-in";
this.mUIFarhadPane.FilterProperties[HtmlDiv.PropertyNames.ControlDefinition] = "class=\"k-in\"";
this.mUIFarhadPane.FilterProperties[HtmlDiv.PropertyNames.TagInstance] = "337";

可能有用的另一件事是以下控制信息:

The arrow and user element in the treeview

1 个答案:

答案 0 :(得分:1)

我假设您正在使用通常的asp TreeView控件进行自动化。在网络上生成的结构是这样的。

<table>......
<tr> 
    <td> <a>+(Expand Icon)</a> </td>
    <td> <a>PARENT NODE NAME</a> </td>
</tr>....  
<table>
<div>
 <table>....<a>CHILD NODE 1</a>....</table>
 <table>....<a>CHILD NODE 2</a>....</table>
</div>

使用这种结构,我们可以轻松创建自己的方法来点击树视图上的东西。

    public void ClickTreeviewChildItem(string parent, string child)
    {
        // Suppose TreeView control Id is MyTreeView. 
        var treeView = new HtmlDiv(new TreeViewPageMap().UIHttplocalhost38842DeWindow); // Use your uimap window here
        treeView.SearchProperties.Add(HtmlDiv.PropertyNames.Id, "MyTreeView");
        treeView.Find();

        // click on parent
        var parentNode = new HtmlHyperlink(treeView);
        parentNode.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, parent);
        Mouse.Click(parentNode);

        // Get id of plus sign in front of node parent node - should be like "MyTreeViewn9"
        // <tr>
        //  <td><a> - </a> </td>
        //  <td><a> D </a> </td> 
        var expandCollapseIconLink = parentNode.GetParent().GetParent().GetChildren()[0].GetChildren()[0] as HtmlHyperlink;
        var linkId = expandCollapseIconLink.Id;

        // Get child div of clicked parent
        // Child div id will be nothing but suffix "Nodes" attached with the id of expand collapse which we got in previous step
        var childDiv = new HtmlDiv(treeView);
        childDiv.SearchProperties.Add(HtmlDiv.PropertyNames.Id, linkId + "Nodes");
        childDiv.Find();

        // find all links as child links in this div
        var allLink = new HtmlHyperlink(childDiv);
        var allLinks = allLink.FindMatchingControls();
        var linkToClick = allLinks.Cast<HtmlHyperlink>().FirstOrDefault(a => a.InnerText.Trim().Equals(child)); // Using System.Linq;
        Mouse.Click(linkToClick);
    }

即使树视图的结构不同,您也可以尝试使用相同的查找和单击节点的方法。在这个结构上测试了这个方法。

enter image description here