{"id":5813,"date":"2011-12-28T05:16:16","date_gmt":"2011-12-28T05:16:16","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2011\/12\/28\/sample-xpath-utility-for-coded-ui-test\/"},"modified":"2019-02-14T18:00:25","modified_gmt":"2019-02-15T02:00:25","slug":"sample-xpath-utility-for-coded-ui-test","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/sample-xpath-utility-for-coded-ui-test\/","title":{"rendered":"Sample XPath utility for Coded UI Test"},"content":{"rendered":"<p>Some important notes before I talk about the utility &#8211;<\/p>\n<ul>\n<li>This posting, and the sample herewith, are provided <strong>&quot;AS IS&quot;<\/strong> with no warranties, and confers no rights. <\/li>\n<li>Use of included script samples are subject to the terms specified at <a href=\"http:\/\/www.microsoft.com\/info\/cpyright.htm\">http:\/\/www.microsoft.com\/info\/cpyright.htm<\/a> <\/li>\n<li>The sample is not supported by Microsoft or the author of this blog.&#160; If you have suggestion\\feedback\\issue with the sample, you may leave comment on this blog and the author will get back to you in his next free cycles. <\/li>\n<\/ul>\n<p>The <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/UITestXPathUtility.zip\" target=\"_blank\">attached sample utility<\/a> adds support for XPath query to Coded UI Test for searching controls.&#160; The utility uses built-in XPath parser and other classes of .NET and as such is conformant to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms256471.aspx\" target=\"_blank\">.NET\u2019s XPath Syntax<\/a>.&#160; The new utility assembly adds two extension methods to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.visualstudio.testtools.uitesting.uitestcontrol.aspx\" target=\"_blank\">UITestControl<\/a> class \u2013<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> UITestControl FindFirstByXPath(<span style=\"color: #0000ff\">this<\/span> UITestControl controlToSearchUnder, <span style=\"color: #0000ff\">string<\/span> xPathQuery)<br \/><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> IEnumerable&lt;UITestControl&gt; FindAllByXPath(<span style=\"color: #0000ff\">this<\/span> UITestControl controlToSearchUnder, <span style=\"color: #0000ff\">string<\/span> xPathQuery)<br \/><\/pre>\n<p><\/div>\n<p>To use these methods, unzip the attached sample and add reference in your Coded UI Test project to the UITestXPathUtility.dll.&#160; (Note that you have manually ensure this dll is there on all machines on which the tests are run.)<\/p>\n<p>Some examples of how to use these functions are \u2013<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\"><span style=\"color: #008000\">\/\/ Get all the radio buttons in the calculatorvar<\/span><br \/>var radionButtons = calc.FindAllByXPath(<span style=\"color: #006080\">&quot;\/\/RadioButton&quot;<\/span>);<br \/><br \/><span style=\"color: #008000\">\/\/ Get all the radio buttons which are either Selected or have name Radians.<\/span><br \/><span style=\"color: #008000\">\/\/ Note that Coded UI Test does not have built-in support for OR operator and<\/span><br \/><span style=\"color: #008000\">\/\/ neither does it allow using Selected property in search condition.var<\/span><br \/>var selectedButtons = calc.FindAllByXPath(<span style=\"color: #006080\">&quot;\/\/RadioButton[@Selected != 'False' or @Name = 'Radians']&quot;<\/span>);<br \/><br \/><span style=\"color: #008000\">\/\/ Get the first control that has Selected property (irrespective of its value)var<\/span><br \/>var controlWithSelectedProperty = calc.FindFirstByXPath(<span style=\"color: #006080\">&quot;\/\/*[@Selected&quot;<\/span>);<\/pre>\n<p><\/div>\n<p>XPath is very powerful and expressive \u2013 I have not tested for all syntax but the way implementation is I believe all syntax would be supported.&#160; In implementation, I am not parsing or doing anything specific to XPath grammar but rather providing navigation capability via XPathNavigator interface to standard .NET APIs to support XPath.<\/p>\n<p>Before using the sample further, note the two limitations\\issues here \u2013<\/p>\n<ol>\n<li><strong>Semantic difference<\/strong> \u2013 The Coded UI Test\u2019s built in search does a breadth-first search whereas this utility (due to implementation of standard XPath) does depth-first search.&#160; This means in certain cases, the result between the two could be different. <\/li>\n<li><strong>Perf issue<\/strong> \u2013 Because this is layer on top of UITestControl and does not uses any technology specific intelligence to speed up the search, the performance is slower than the built in search.&#160; The exact difference depends on the query and depth of the tree.&#160; For example, for if the control under which you are searching has deep control hierarchy within it, the search performance could even be 10x slower.&#160; On the other hand, say the control under which you are searching is just one or two level deep and query is simple enough, there will be hardly any impact. <\/li>\n<\/ol>\n<p><strong><span style=\"background-color: #ffff00\">Because of the above two limitations\\issues, this should not be used as a widespread replacement of the built-in search. <\/span><\/strong>You should use it with prudence at places where you really need ease of use and power of XPath.&#160; I would suggest limiting the usage to places where control tree is not deep to overcome the performance penalty.<\/p>\n<p>Gautam<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some important notes before I talk about the utility &#8211; This posting, and the sample herewith, are provided &quot;AS IS&quot; with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http:\/\/www.microsoft.com\/info\/cpyright.htm The sample is not supported by Microsoft or the author of this blog.&#160; If you [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,1,252],"tags":[],"class_list":["post-5813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-community","category-devops","category-testing"],"acf":[],"blog_post_summary":"<p>Some important notes before I talk about the utility &#8211; This posting, and the sample herewith, are provided &quot;AS IS&quot; with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http:\/\/www.microsoft.com\/info\/cpyright.htm The sample is not supported by Microsoft or the author of this blog.&#160; If you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=5813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/5813\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=5813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=5813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=5813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}