在编码的UI测试中使用XPath匹配元素

时间:2012-07-18 02:34:48

标签: coded-ui-tests

这是一个愚蠢的问题,但我开始使用MS编码的UI测试。我想知道是否有办法使用XPath查找页面元素,而不是默认的匹配机制?我想要做的是匹配父元素并以编程方式向下导航DOM树以获取我想要使用的元素。这可以通过Selenium轻松完成,但我不知道如何使用Coded UI Tests。

由于

1 个答案:

答案 0 :(得分:2)

您应该能够使用xpath管理导航UITestControlCollection。使用CodedUI的刻录机进入顶层控件,然后使用GetChildren导航。请记住,xpath更改是因为所有对象类型都相似,CodedUI的API无法区分。

示例:

HtmlDocument doc = this.UIYourWindowName.UIYourDocumentName; // mapped control
doc.Find();
UITestControl toline = new UITestControl(doc);
toline.SearchProperties["Id"] = "to_d"; // use the id of the top most control
UITestControlCollection toline1 = toline.GetChildren(); // get the child objects

toline1 = toline1[0].GetChildren(); // xpath: \\ctrl[@id='to_d']\item[0]
toline1 = toline1[0].GetChildren(); // ctrl[]\item[0]\item[0]
// and so on...