Forum Discussion

Mia's avatar
Mia
Contributor
10 years ago

Working with WPF DataGrid - some objects cannot be found

Hi,



I am trying to record easy test with WPF DataGrid. Just to fill  a content of one cell. TC9 generated following script.



function DataGrid()

{

  TestedApps.TestComplete.Run();

  Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.ClickNewRowCellXY("Id", 104, 9);

  Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell.Keys("5");

  Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2.TextBox.SetText("50");

  Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2.TextBox.Keys("[Enter]");

}



It works until it comes to third row. I always get error message that it cannot find the TextBox. But when I use the Object Spy or Object browser, it is there. Also in the NameMapping tree veiw I have everything mapped, including the TextBox object. 



I tried to read some smartbear articles and some threads here on forum, but I hit the dead end and I just cannot figure it out.





The error message.


" An error occurred while accessing the "SetText" method or property of the "TextBox" object.

The object or one of its parent objects was not found.



Object Whose Method or Property Was Accessed














Alias:

Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2.TextBox

Name mapping item:

NameMapping.Sys.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2.TextBox


Object That Was Not Found














Alias:

Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2

Name mapping item:

NameMapping.Sys.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2


Click the link above to view and configure mapping settings for the missing object.



Learn about possible causes of the error "




2 Replies

  • roc_muto's avatar
    roc_muto
    New Contributor
    In your screenshot I don't see a third row? Does it exist when you get to the line:

    Aliases.TestComplete.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.DataGridCell2.TextBox.SetText("50");



    When we work WPF DataGrids we take either 2 approaches.



    1. We map just the grid object and work with the cells and rows using the diffent methods provided like ClickCell, FindRow etc. Here are the methods TestComplete provides when working with a standard Microsoft DataGrid http://support.smartbear.com/viewarticle/60989/


    2. The other approach we take would likely involve development to work with you and is a custom solution we did for some grids and likely wouldn't be relevent for you. We map the grid and map one row and all cells within using the AutomationID. Change the mapping to be a variable and then set the variable prior to doing any row or cell specific actions.


  • Mia's avatar
    Mia
    Contributor
    I guess the row exists... I assume, that if I work only with those two rows on the screenshot, TestComplete generates its adresses well.

    When I recorded the test, all I did was clicking into the first cell and I hit those keys: "50 + [Enter]". But TestComplete divided the number into 5 and 50. And since the "5" is inserted, the cell adress is changed somehow.   IMHO, that's why it does not work.



    If I modify it like this, it is working. But this approach seems to be very unreliable, because the adress to the particular cell or row is always different - depending on the number I insert.

    function GridTest()

    {

      TestedApps.TestComplete.Run();

      Aliases.TestComplete2.HwndSource_MainWindow.MainWindow.StackPanel.Grid.DataGridRow2.Datagridcell23.Keys("50");

    }





    Anyway, I treid to use the approach number 1. I like it more than working with particular cell/row and it would be really great if it worked.

    But both ways I tried are not working properly.



    1)This works until use e.g. ClickCell(1, "Id"); I always get and error, that the control item not found. And it is there. 



    function GridTest1()

    {

      var w;

      w = Sys.Process("TestComplete", 2).WPFObject("HwndSource: MainWindow", "MainWindow").WPFObject("MainWindow", "MainWindow", 1).WPFObject("StackPanel", "", 1).WPFObject("Grid");

      w.DblClickCell(0, "Id");

      //w.ClickCell(0, "Id");

      Sys.Keys('40'+'[Enter]');

    }



    2) This code is partially working, the value is inserted somewhere into the data source, but I cannot see it in the UI. To see it, I have to click e.g. on the headers, or make some another action in the grid. I tried to end the code with w.Refresh(), but it didn't help. And I couldn't find any other function for inserting value.



    function GridTest2()

    {

      var w;

      w = Sys.Process("TestComplete", 2).WPFObject("HwndSource: MainWindow", "MainWindow").WPFObject("MainWindow", "MainWindow", 1).WPFObject("StackPanel", "", 1).WPFObject("Grid");

      w.ClickCell(0, "Id");

      w.wValue(0, "ID") = "8888";    

    }