internal BrowserCommandResult OpenSubGridRecord()

in Microsoft.Dynamics365.UIAutomation.Api.UCI/WebClient.cs [3697:3756]


        internal BrowserCommandResult<bool> OpenSubGridRecord(string subgridName, int index = 0)
        {
            return this.Execute(GetOptions($"Open Subgrid record for subgrid {subgridName}"), driver =>
            {
                // Find the SubGrid
                var subGrid = driver.FindElement(By.XPath(AppElements.Xpath[AppReference.Entity.SubGridContents].Replace("[NAME]", subgridName)));

                if (subGrid.HasElement(By.CssSelector(@"div.Grid\.ReadOnlyGrid")))
                {
                    // Read-only subgrid
                    var subGridTable = subGrid.FindElement(By.XPath(AppElements.Xpath[AppReference.Entity.SubGridListCells]));
                    var rowCount = subGridTable.GetAttribute<int>("data-row-count");

                    if (rowCount == 0)
                    {
                        throw new NoSuchElementException($"No records were found for subgrid {subgridName}");
                    }
                    else if (index + 1 > rowCount)
                    {
                        throw new IndexOutOfRangeException($"Subgrid {subgridName} record count: {rowCount}. Expected: {index + 1}");
                    }

                    var row = subGridTable.FindElements(By.XPath(AppElements.Xpath[AppReference.Entity.SubGridRows])).ElementAt(index + 1);
                    var cell = row.FindElements(By.XPath(AppElements.Xpath[AppReference.Entity.SubGridCells])).ElementAt(1);

                    new Actions(driver).DoubleClick(cell).Perform();
                    driver.WaitForTransaction();
                }
                else if (subGrid.TryFindElement(By.XPath(AppElements.Xpath[AppReference.Entity.EditableSubGridList].Replace("[NAME]", subgridName)), out var subGridRecordList))
                {
                    // Editable subgrid
                    var editableGridListCells = subGridRecordList.FindElement(By.XPath(AppElements.Xpath[AppReference.Entity.EditableSubGridListCells]));
                    var editableGridCellRows = editableGridListCells.FindElements(By.XPath(AppElements.Xpath[AppReference.Entity.EditableSubGridListCellRows]));
                    var editableGridCellRow = editableGridCellRows[index + 1].FindElements(By.XPath("./div"));

                    new Actions(driver).DoubleClick(editableGridCellRow[0]).Perform();
                    driver.WaitForTransaction();
                }
                else
                {
                    // Check for special 'Related' grid form control
                    // This opens a limited form view in-line on the grid

                    //Get the GridName
                    string subGridName = subGrid.GetAttribute("data-id").Replace("dataSetRoot_", string.Empty);

                    //cell-0 is the checkbox for each record
                    var checkBox = driver.FindElement(
                        By.XPath(
                            AppElements.Xpath[AppReference.Entity.SubGridRecordCheckbox]
                            .Replace("[INDEX]", index.ToString())
                            .Replace("[NAME]", subGridName)));

                    driver.DoubleClick(checkBox);
                    driver.WaitForTransaction();
                }

                return true;
            });
        }