2012-07-18 63 views
0

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

感谢

+1

http://blogs.msdn.com/b/visualstudioalm/archive/2011/12/28/sample-xpath-utility-for-coded-ui-test.aspx有虽然CodedUI上的自定义工具可以做到这一点,但没有内置支持。 – 2012-07-30 08:21:57

回答

2

你应该能够管理使用UITestControlCollection导航的xpath。使用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...