locked
How can I read the value of a WpfCell using Coded UI? RRS feed

  • Question

  • I'm attempting to write a method to get the list of values in a particular column of a WPF data grid. Below is the code I've written so far:

    public static List<string> GetColumnDataFromGrid(WpfWindow mainWindow, string gridAutomationId, string columnHeaderText)
    {
        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Waiting for custom control to be ready.");
        WpfCustom custom = new WpfCustom(mainWindow);
        custom.SearchProperties.Add(WpfCustom.PropertyNames.AutomationId, "userControlHeaderListView");
        custom.SearchProperties.Add(WpfCustom.PropertyNames.ClassName, "Uia.VoucherHeaderListView");
        custom.WaitForControlReady();
        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Custom control is ready.");
    
        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Waiting for table [" + gridAutomationId + "] to be ready.");
        WpfTable table = new WpfTable(custom);
        table.SearchProperties.Add(WpfTable.PropertyNames.AutomationId, gridAutomationId);
        table.WaitForControlReady();
        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Table [" + gridAutomationId + "] is ready.");
    
        List<string> cellDataList = new List<string>();
    
        for (int i = 0; i < table.RowCount; i++)
        {
            Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Getting data from row [" + i + 1 + "].");
            WpfCell cell = new WpfCell(table);
            cell.SearchProperties.Add(WpfCell.PropertyNames.ColumnHeader, columnHeaderText);
            cell.SearchProperties.Add(WpfCell.PropertyNames.RowIndex, i.ToString());
    
            Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Waiting for cell to be ready.");
            if (cell.WaitForControlReady(15000))
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Cell is ready.");
                cellDataList.Add(new WpfText(cell).DisplayText);
            }
        }
    
        return cellDataList;
    }

    However when it get to cell.WaitForControlReady(15000), an exception is thrown:

    Test Name:	SetScStatusToBackToImportMouseTest (Data Row 0)
    Test Outcome:	Failed
    Result Message:	
    Test method CodedUI.ApplicationFeatureTest threw exception: 
    Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: Automation Playback Engine was not able to find the custom control userControlHeaderListView in - custom control userControl. Additional Details: 
    TechnologyName:  'UIA'
    FrameworkId:  'WPF'
    ControlType:  'Custom'
    ClassName:  'Uia.VoucherHeaderListView'
    AutomationId:  'userControlHeaderListView'
     ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
    Result StandardOutput:	
    2016/09/06 - 17:13:04 | Waiting for custom control to be ready.
    2016/09/06 - 17:13:13 | Custom control is ready.
    2016/09/06 - 17:13:13 | Waiting for table [MainDatagridView] to be ready.
    2016/09/06 - 17:13:14 | Table [MainDatagridView] is ready.
    2016/09/06 - 17:13:14 | Getting data from row [01].
    2016/09/06 - 17:13:14 | Waiting for cell to be ready.

    I attempted to verify the value of a single cell using the coded ui test builder tool and it worked. I created a path to the row following the same search properties which was used by the test builder tool.

    I'd appreciate it greatly if someone could point out why this is happening or even suggest an alternative.

    Tuesday, September 6, 2016 12:09 PM

Answers

  • Hi asifrr,

    According to the sample DataGrid, I'm afraid that this DataGrid is a custom control. Since if I try use CodedUI Builder to capture this DataGrid, I could not capture it as a entire control. I just could capture the cells one by one.

    And when I debug your Coded UI script, I found that if I add watch for the WpfTable, there has a lot of errors for properties of WpfTable. And if I get all children for MainWindow, I don't find a control named table or DataGrid in its children.

    So I suggest you write your own Coded UI plugin to support this custom control based on the link I provided in my first reply.

    https://blogs.msdn.microsoft.com/gautamg/2010/03/23/writing-extension-for-wpf-custom-control/

    If you think create custom CodedUI plugin need a long time, I think CodedUI Builder also a good choice. You could use CodedUI Builder to record the action that you needed, which is more easier.

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by asifrr Wednesday, October 26, 2016 8:01 AM
    Monday, October 24, 2016 6:24 AM

All replies

  • Hi asifrr,

    According to your sample code and the error message "not able to find the custom control userControlHeaderListView ", the WPF control is a custom control which contains a WpfTable in it, is it right?

    Please use control.DrawHightlight() method to check whether you have find the correct custom control first. If it could not find the VoucherHeaderListView, I suggest you to use Coded UI Test Builder tool to verify whether the search properties for this control are correct.

    And if the search properties are not work for your custom control, I'm afraid that you need to write your own custom control extension for your control. Following blog a sample for custom WPF control, hope it can help you.

    https://blogs.msdn.microsoft.com/gautamg/2010/03/23/writing-extension-for-wpf-custom-control/

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 7, 2016 5:24 AM
  • Hi Weiwei,

    I added a DrawHighlight for each of the controls I'm searching for (i.e the custom control, the grid, the row and the cell) and everything gets highlighted except for the cell control. I even replaced 

    cell.SearchProperties.Add(WpfCell.PropertyNames.ColumnHeader, columnHeaderText);

    with

    cell.SearchProperties.Add(WpfCell.PropertyNames.ColumnIndex, "5");

    but the cell still does not get highlighted. 

    The test builder tool gets the text within the cell using:

    WpfText uIItem1000053Text = this.UIMainWindowWindow.UIUserControlHeaderLisCustom.UIMainDatagridViewTable.UIItemRow.UIItemCompelloModelDefCell.UIItem1000053Text;

    and the cell is identified using:

    [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
    public class UIItemCompelloModelDefCell : WpfCell
    {
        
        public UIItemCompelloModelDefCell(UITestControl searchLimitContainer) : 
                base(searchLimitContainer)
        {
            #region Search Criteria
            this.SearchProperties[WpfCell.PropertyNames.ColumnHeader] = "Archive no.";
            this.WindowTitles.Add("MainWindow");
            #endregion
        }
        
        #region Properties
        public WpfText UIItem1000053Text
        {
            get
            {
                if ((this.mUIItem1000053Text == null))
                {
                    this.mUIItem1000053Text = new WpfText(this);
                    #region Search Criteria
                    this.mUIItem1000053Text.SearchProperties[WpfText.PropertyNames.Name] = "1000053";
                    this.mUIItem1000053Text.WindowTitles.Add("MainWindow");
                    #endregion
                }
                return this.mUIItem1000053Text;
            }
        }
        #endregion
        
        #region Fields
        private WpfText mUIItem1000053Text;
        #endregion
    }

    Have I made a mistake in my approach to locate the cell in comparison to the code generated by test builder?

    Friday, September 9, 2016 9:38 AM
  • Hi asifrr,

    Since the exception occurs on the line code of "if (cell.WaitForControlReady(15000))". I suggest you comment the for loop code and then using the search properties code to find a specific cell. It can help us confirm whether this issue caused by the for loop code.

    According to the code you handed code and generated by Coded UI Builder, I don't find any mistake in your code. I suggest you provide a sample project which contains this WPF control and your Coded UI script code, I will debug your test script in my side to check whether there has any issues.

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 12, 2016 6:19 AM
  • Hi Weiwei,

    I tried the following but sadly it didn't work

    table.GetRow(0).DrawHighlight();
    Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Getting data from row [" + 1 + "].");
    WpfCell cell = new WpfCell(table);
    cell.SearchProperties.Add(WpfCell.PropertyNames.ColumnHeader, "Archive no.");
    cell.SearchProperties.Add(WpfCell.PropertyNames.RowIndex, 0.ToString());
    cell.DrawHighlight();
    Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Waiting for cell to be ready.");
    if (cell.WaitForControlReady(15000))
    {
        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss") + " | Cell is ready.");
        archiveNosList.Add(new WpfText(cell).DisplayText);
    }
    As for getting a copy of control, I'll speak to the developers and see if they could create something similar.

    Wednesday, September 14, 2016 12:21 PM
  • Hi asifrr,

    Do you have contact the developers for this issue? Whether this control a custom control?

    Please provide a sample for this control which can help me reproduce this issue. Thanks for your understanding.

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 21, 2016 6:02 AM
  • Hi Weiwei,

    I've managed to get the requested sample. Please find the links below:

    1. Sample solution for the console
    2. Coded UI Test solution sample

    Apologies for the delay.

    Best regards,

    asifrr


    • Edited by asifrr Tuesday, October 18, 2016 10:07 AM
    Tuesday, October 18, 2016 10:07 AM
  • Hi asifrr,

    According to the sample DataGrid, I'm afraid that this DataGrid is a custom control. Since if I try use CodedUI Builder to capture this DataGrid, I could not capture it as a entire control. I just could capture the cells one by one.

    And when I debug your Coded UI script, I found that if I add watch for the WpfTable, there has a lot of errors for properties of WpfTable. And if I get all children for MainWindow, I don't find a control named table or DataGrid in its children.

    So I suggest you write your own Coded UI plugin to support this custom control based on the link I provided in my first reply.

    https://blogs.msdn.microsoft.com/gautamg/2010/03/23/writing-extension-for-wpf-custom-control/

    If you think create custom CodedUI plugin need a long time, I think CodedUI Builder also a good choice. You could use CodedUI Builder to record the action that you needed, which is more easier.

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by asifrr Wednesday, October 26, 2016 8:01 AM
    Monday, October 24, 2016 6:24 AM
  • Thanks for your help, Weiwei
    Wednesday, October 26, 2016 8:01 AM