Sample XPath utility for Coded UI Test

Sample XPath utility for Coded UI Test

Rate This
  • Comments 6
Some important notes before I talk about the utility -
  • This posting, and the sample herewith, are provided "AS IS" 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.  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.

The attached sample utility adds support for XPath query to Coded UI Test for searching controls.  The utility uses built-in XPath parser and other classes of .NET and as such is conformant to .NET’s XPath Syntax.  The new utility assembly adds two extension methods to UITestControl class –

public static UITestControl FindFirstByXPath(this UITestControl controlToSearchUnder, string xPathQuery)
public static IEnumerable<UITestControl> FindAllByXPath(this UITestControl controlToSearchUnder, string xPathQuery)

To use these methods, unzip the attached sample and add reference in your Coded UI Test project to the UITestXPathUtility.dll.  (Note that you have manually ensure this dll is there on all machines on which the tests are run.)

Some examples of how to use these functions are –

// Get all the radio buttons in the calculatorvar
var radionButtons = calc.FindAllByXPath("//RadioButton");

// Get all the radio buttons which are either Selected or have name Radians.
// Note that Coded UI Test does not have built-in support for OR operator and
// neither does it allow using Selected property in search condition.var
var selectedButtons = calc.FindAllByXPath("//RadioButton[@Selected != 'False' or @Name = 'Radians']");

// Get the first control that has Selected property (irrespective of its value)var
var controlWithSelectedProperty = calc.FindFirstByXPath("//*[@Selected");

XPath is very powerful and expressive – I have not tested for all syntax but the way implementation is I believe all syntax would be supported.  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.

Before using the sample further, note the two limitations\issues here –

  1. Semantic difference – The Coded UI Test’s built in search does a breadth-first search whereas this utility (due to implementation of standard XPath) does depth-first search.  This means in certain cases, the result between the two could be different.
  2. Perf issue – 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.  The exact difference depends on the query and depth of the tree.  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.  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.

Because of the above two limitations\issues, this should not be used as a widespread replacement of the built-in search. You should use it with prudence at places where you really need ease of use and power of XPath.  I would suggest limiting the usage to places where control tree is not deep to overcome the performance penalty.

Gautam

Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post
  • Hi Gautam,

    Below is the html for my last query:

    <li class="yuimenubaritem first-of-type" id="yui-gen0" groupindex="1" index="0"><a href="seeker.dice.com/.../ProfMan class="yuimenubaritemlabel">Login</a></li>

  • @Prasanna - Which query?  I don't see any other comment on this.  This was a sample that I wrote long time back - see if you can debug it on your own.

  • Could you explain, please, how to find web elements? For example, my current object is Document and when I put a xPath query like  objDoc.FindFirstByXPath(@"./FORM" ) - it returns me null.

  • What are the supported browsers and browser versions for this Utility?

  • Hi!

    Am I missing something, or this project is provided only as a simple dll?

    No source code available?

    I have to implement exactly this at my company, but it's prohibited for us to include anything without sources - because they may contain viruses or backdoors.

    Thank you in advance and best regards,

    Kobor42

  • Hi, I am unable to use the method FindAllByXPath(), the error message is "could not load file or assembly 'microsoft.visualstudio.testtools.uitest.extension Version 10.0." .

    I am using VS 2012. Please help.

Page 1 of 1 (6 items)