Freigeben über


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

  • Anonymous
    December 18, 2009
    do you have any plan to support this in vs2010?
  • Anonymous
    December 19, 2009
    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?
  • Anonymous
    September 21, 2010
    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 unfortunatelythx in advance...
  • Anonymous
    October 09, 2010
    Hi,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 codethanks in advanceKeshav.M
  • Anonymous
    May 07, 2013
    Thanks for this post.It really helped me out.