Search code examples
texthtml-tablewatin

How to get table column header text in Watin?


I am using Watin for automation testing with IE browser. I am new to Watin. I got blocked in a task of getting the column header text for a table.HTML code for that table is something like below:

<table id="gvVoiceCallReport" style="width: 100%; border-collapse: collapse; height: 100%;" border="1" rules="all" cellSpacing="0" cellPadding="0">
       <tbody>
          <tr style="background-color: slategray; height: 20px; color: white;">
             <th scope="col">
             <a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Caller GCI')">
                Text - Caller GCIstyle
             <th scope="col">
              <a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Callee GCI')">
            Text - Callee GCI
            <th scope="col">
                <a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Originator-Default Bearer Apn')">
                Text - Originator-Default Bearer Apn

            <tr style="cursor: hand;" onmouseover="this.style.cursor='hand';">
            <td align="left" onclick="window.open('TestResult.aspx?testSuite=Job.139.1_1504100010110027884023126', 'TestResult', 'height=600,width=900,resizable=yes,scrollbars=yes');">
            Text - 310;410;FFFE;9210;B9F
            <td align="left" onclick="window.open('TestResult.aspx?testSuite=Job.139.1_1504100010110027884023126', 'TestResult', 'height=600,width=900,resizable=yes,scrollbars=yes');">
            Text - 310;410;FFFE;9210;B9F
        .....    
        ......
        </table>

Header text for that column is Caller GCI. I am able to get the text for values in that column by using something like that

string columnValueText = mytable.OwnTableRows[1].TableCells[1].Text;

When I am trying to get the column header text by making OwnTableRows[0] (Index to zero), It gives me exception: array out of bounds.

Anyone, Please help me to get the table column header text.


Solution

  •   var gridTableRows = page.Document.Frame(Find.ById("ctl00_MainContentPlaceHolder_ifrmReport")).Table("gvVoiceCallReport").TableRows[0];
      StringCollection abc = GetAllColumnDataFromRow(gridTableRows, true);
    
       private StringCollection GetAllColumnDataFromRow(TableRow tableRow, bool isTableHeaderRow)
        {
            StringCollection RowValues = new StringCollection();
    
            if (isTableHeaderRow)
            {
                foreach (Element e in tableRow.Elements)
                {
                    if (e.TagName == "TH")
                    {
                        RowValues.Add(e.Text);
                    }
                }
            }
    
            return RowValues;
        }