0

I have a table which has multiple rows. I want to verify that a specific string StringName is in the table. I used CodedUI to find the control, and the UI map is like this:

public class UIItemTable : WpfTable
{
    #region Fields
    private UIItemRow mUIItemRow;
    #endregion
}

public class UIItemRow : WpfRow
{
    #region Fields
    private UIBlahBlahBlahCell mUIBlahBlahBlahCell;
    #endregion
}

public class UIBlahBlahBlahCell : WpfCell
{
    #region Fields
    private WpfText mUIBlahBlahBlahText;
    #endregion
}

I want to find a WpfText that matches my StringName. So I added a function to UIItemTable:

public class UIItemTable : WpfTable
{
    public WpfText Find(string StringName)
    {
        WpfText StringNameWpfText = new WpfText(this);
        StringNameWpfText.SearchProperties[WpfText.PropertyNames.Name] = StringName;
        return StringNameWpfText;
    }

    #region Fields
    private UIItemRow mUIItemRow;
    #endregion
}

But CodedUI cannot find the WpfText control. The error I received is:

Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException. The playback failed to find the control with the given search properties. Additional Details: TechnologyName: 'UIA' ControlType: 'Text' Name: ...

I think this error may be due to the fact that the WpfCell I want to search is actually a grandchild of the table. But I thought CodedUI handles tree traversals right? How do I search for a grandchild?

CherryQu
  • 3,343
  • 9
  • 40
  • 65

2 Answers2

2
  1. You should move your code out of the partial UIMap.designer.cs class as it will be overwritten the next time you record a control/assert/etc. Move you code to the uimap.cs partial class instead.

  2. The text control may or may not be the next level down, if it is not on the child level of UIItemTable, your code will fail. You should try to use the test tool builder cross hair to identify the level it exists at.

  3. You can use the child enumerator and search through all child/grandchild/etc elements until you find your text item, an example below would be something like:

    public UIControl FindText(UIControl ui)
    { 
      UIControl returnControl = null;
    
      IEnumerator<UITestControl> UIItemTableChildEnumerator =
      ui.GetChildren().GetEnumerator();
    
      while(uiChildEnumerator.MoveNext())
      {
           if(uiChildEnumerator.Current.DisplayText.equals(StringName))
           {
               returnControl = uiChildEnumerator.Current  
               break;
           }
           else
           {
               returnControl = this.FindText(uiChildEnumerator.Current)
               if(returnControl != null)
               {
                   break;
               }
           }
      }
    
      return returnControl;
    }
    

4 . Another option would be to use the FindMatchingControls() function and parse through similar to the above statement. You might still need the proper direct parents though

    WpfText StringNameWpfText = new WpfText(this);

    IEnumerator<UITestControl> textItems = StringNameWpfText .FindMatchingControls().GetEnumerator();

        while (textItems.MoveNext())
        {
           //search
        }

Hope this helps

mr.X
  • 291
  • 1
  • 6
  • Item (1) is not clear. Both the `uimap.cs` and the `uimap.designer.cs` files have `partial class` near their tops. This point might be better saying: move your code out of the `uimap.designer.cs` file as it will be overwritten the next time you record a control/assert/etc. Move the code into the `uimap.cs` file as both files contribute to the same class. The other points look good. – AdrianHHH Aug 07 '14 at 11:43
0

I usually find cells first. I believe any content in a table would be in a cell. So we can search for the cells first and then dive in it.

One way to find cell whose value matches with the given string.

WpfCell cell = myWpfTable.FindFirstCellWithValue(string StringName);
if(cell != null)
    return cell.Value // returns WpfControl;
else 
    return null;

Another way to get the value of cell which contains a particular string

WpfCell myCell = myWpfTable.Cells
                           .FirstOrDefault(c => c.Value.Contains("StringName"));
var myTextControl = myCell != null ? myCell.Value : null;

If the text is nested deep in the cell, then you can do this. Just like what you were doing with the table

// Find cell which contains the particular string, let say it "myCell"
WpfText mytext = new WpfText(myCell);
mytext.SearchProperties.Add(WpfText.PropertyNames.Name, "StringName", PropertyExpressionOperator.Contains);
if(mytext.Exist)
    return myText;
else 
    retrun null;
SarkarG
  • 687
  • 1
  • 8
  • 30