Using regular expressions to locate controls in Coded UI Test

When testing UI applications, you often want to locate a control on the app under test that has dynamic properties. For instance, when you login into a website, there may be a link/label on the first page that says “Welcome user, UserName1”. Now this username is going to change as per the user that logged in.

While writing a coded UI test to locate this label however, the search properties are set to the value that was found during recording. While playing back if you logged in as a new user, the search fails since it was looking for “UserName1” instead of your logged in “UserName2”. To address this, you can tweak the search properties of the control you are using:

UITestControl lblObj = new UITestControl();
lblObj.SearchProperties.Add("InnerText", "Welcome", PropertyExpressionOperator.Contains);

The above code uses “Contains” to ensure that it is searching for a string containing the substring “Welcome”. If you need a more accurate search, you might want to use regular expressions for matching the title. Unfortunately, we don’t have built in support to match the a search property against a given regular expression. However, you can write code to find the set of labels that are under a given hierarchy and match each of their inner text properties against a regular expression of your choice using the standard .NET APIs

Comments (5)

  1. hasan says:

    do you have any plan to support this in vs2010?

  2. Actually, we never heard back from any of our customers that they absolutely need this feature despite the Contains. So, this is currently lower pri to address in the release. Do you think different?

  3. SHANMUGAVEL.C says:

    SearchProperties.Add(WinRow.PropertyNames.Value, "welcome", PropertyExpressionOperator.Contains);

    So here i want to use "*" instead of putting "welcome" . ie. i want to click an object with any value praticularly not contains welcome…

    Give me idea…

    What about Regex function…..

    [Anu] we don't support this functionality using regexp unfortunately

    thx in advance…

  4. Keshav says:


    As u said if it is a sub string thats fine if part of a string is varying freequently like 10/1012010 19:10:29.if seconds are varying then can we use this regular expression feature, really i am facing this problem Please give the solution.

    [Anu] you can still use the contains on the date format then. write to the Coded UI MSDN forums for detailed code

    thanks in advance


  5. Thanks for this post.

    It really helped me out.

Skip to main content