Iterate through a table

Ask general questions here.
macgowan
Posts: 14
Joined: Mon Jun 23, 2014 9:20 pm

Iterate through a table

Post by macgowan » Tue Jul 29, 2014 6:17 pm

Hi ...

Sorry for he repeat question - I have looked at some of the other posts related to table iteration and they don't seem to work for me ???

I have a table in an application that I have identified using the Ranorex Repository Track feature. The element is called 'HistoryViewerDataTable' The xpath is below:

/dom[@domain~'[a-z, A-Z]*.workbench.judicial.int.westgroup.com']//table[#'DataTables_Table_2']

The samples I've seem on this forum use the TrTag element to collect the rows in the table. I have attempted to use the following statement. The compile error suggests there is a cast I can use to return the TrTags in a IList collection ???

Code: Select all

IList<TrTag> directDataList = repo.JSA.HistoryViewerDataTable.Children; 


Cannot implicitly convert type 'System.Collections.Generic.IList<Ranorex.Unknown>' to 'System.Collections.Generic.IList<Ranorex.TrTag>'. An explicit conversion exists (are you missing a cast?) (CS0266) - C:\Workspace\SummaryRegression\CodeModules\Tests\History\DirectHistoryGeneral.cs:179,34

After I have the collection of rows - I would hope to iterate through the cells in each row with some code as follows ...

Code: Select all

foreach(TrTag trTag in directDataList)
{
    IList<TrTag> tdTags = trTag.Children;

    Report.Info("tdTags count: " + tdTags.Count);

    string cited_citing = (new TdTag(tdTags[0].Element)).InnerText;
    Report.Info("cited_citing: " + cited_citing);

    string serial_number = (new TdTag(tdTags[1].Element)).InnerText;
    Report.Info("serial_number: " + serial_number);
}
Thanks for your help,
Chris

macgowan
Posts: 14
Joined: Mon Jun 23, 2014 9:20 pm

Re: Iterate through a table

Post by macgowan » Wed Jul 30, 2014 3:44 pm

Hi ...

It looks like I was using the wrong method to get the collection of objects -
FindChildren()

Below is the code that is working

Code: Select all


/// <summary>
/// Performs the playback of actions in this module.
/// </summary>
/// <remarks>You should not call this method directly, instead pass the module
/// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
/// that will in turn invoke this method.</remarks>
void ITestModule.Run()
{
    Mouse.DefaultMoveTime = 300;
    Keyboard.DefaultKeyPressTime = 100;
    Delay.SpeedFactor = 1.0;

    Report.Info("Open the History Viewer widget"); 
    repo.JSA.HeaderBar.History.Click();

    Report.Info("Wait for the history viewer to open (wait 5 sec)");
    System.Threading.Thread.Sleep(5000);

    Report.Info("Loading test data");
    LoadValidationData(); 

    Report.Info("Validate Direct tab and related columns"); 
    ValidateDirectTabAndColumns();

}


/// <summary>
/// We will test for operation of the columns in the Direct Tab
/// </summary>
private void LoadValidationData()
{

    Report.Info("---------------------------------------");
    Report.Info("Loading test data");

    historyCells1 = new List<String>(); 
    historyCells1.Add("Prior"); 
    historyCells1.Add("2010271173");
    historyCells1.Add("2003 WL 25276984"); 
    historyCells1.Add("Monsanto Co. v. Ralph"); 
    historyCells1.Add("Affirming in Part, Vacating in Part Opinion"); 

    historyRows.Add(historyCells1); 

    historyCells2 = new List<String>(); 
    historyCells2.Add("Subsequent");
    historyCells2.Add("2006258465"); 
    historyCells2.Add("122 Fed.Appx. 510");
    historyCells2.Add("Monsanto Co. v. Ralph"); 
    historyCells2.Add("On Subsequent Appeal");

    historyRows.Add(historyCells2); 

    foreach(IList<String> cells in historyRows)
    {
        Report.Info("cells count: " + cells.Count);

        // the first row is a header - we will check for this 
        // and then move on

        foreach(String cell in cells)
        {
            Report.Info("cell: " + cell);
        } 
    }
}


/// <summary>
/// We will test for operation of the columns in the Direct Tab
/// </summary>
private void ValidateDirectTabAndColumns()
{

    // This code is from the test calss NoValidationErrors

    IList<Ranorex.TrTag> directDataList = repo.JSA.HistoryViewerDataTable.FindDescendants<Ranorex.TrTag>(); 

    Report.Info("Validate data in Direct Tab in the History Viewer");
    Report.Info("Data is tested agaist an artifact based mock dataset");

    Report.Info("Iterate the directDataList");
    Report.Debug("directDataList count: " + directDataList.Count);

    int rowIndex = 0;
    int cellIndex = 0; 

    foreach(Ranorex.TrTag row in directDataList)
    {

        IList<TdTag> tableCells = row.FindChildren<TdTag>() as List<TdTag>;
        Report.Debug("tableCells count: " + tableCells.Count);

        // the first row is a header - we will check for this 
        // and then move on

        if (tableCells.Count > 0) 
        { 

            // get the validation based on row index         
            IList<String> historyCells = historyRows[rowIndex]; 

            cellIndex = 0; 

            // Do the validation                
            foreach(TdTag cell in tableCells)
            {
                if (cell.InnerText.CompareTo(historyCells[cellIndex].ToString()) == 0)
                { 
                    Report.Success("Row: " + rowIndex + " Cell: " + cellIndex + " Data: " + cell.InnerText);
                } 
                else
                { 
                    Report.Failure("Cell data did not match data in mock dataset. Details below");
                    Report.Info("Cell Data: Row: " + rowIndex + " Cell: " + cellIndex + " Data: " + cell.InnerText);
                    Report.Info("Mock dataset: " + historyCells[cellIndex].ToString());
                } 

                cellIndex++; 
            } 

            rowIndex++; 
        }
    }
}