Aishu
Aishu

Reputation: 1320

Handling tables in c# selenium

<table cellpadding="0" cellspacing="0" onclick="" style="width: 1345px;">

  <tbody>

    <tr id="item_tcm:222-382904-131104" title="2. Publish to WIP (tcm:222-382904-131104)" class="item even" c:drawn="true">
      <td class="col0 icon odd" value="T131104L0P0">
        <div class="icon" style="background-image: url(&quot;/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&amp;size=16&quot;);"></div>
      </td>
      <td class="col1 even">
        <div class="text">2.&nbsp;Publish&nbsp;to&nbsp;WIP</div>
      </td>
      <td class="col2 odd">
        <div class="text">JH&nbsp;Anchor&nbsp;link&nbsp;2</div>
      </td>
      <td class="col3 even">
        <div class="text">S070&nbsp;Public&nbsp;Site&nbsp;US&nbsp;English</div>
      </td>
      <td class="col4 odd" value="2015-12-23T14:41:04">
        <div class="text">12/23/2015 2:41 PM</div>
      </td>
      <td class="col5 even">
        <div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
      </td>
      <td class="col6 odd" value="">
        <div class="text">
          <span style="color: #f00"></span>
        </div>
      </td>
      <td class="col7 even" value="16">
        <div class="text">Suspended</div>
      </td>
      <td class="col8 odd">
        <div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
      </td>
      <td class="col9 even">
        <div class="text">Publishing&nbsp;Failed</div>
      </td>
    </tr>
    <tr id="item_tcm:222-382901-131104" title="2. Publish to WIP (tcm:222-382901-131104)" class="item even" c:drawn="true">
      <td class="col0 icon odd" value="T131104L0P0">
        <div class="icon" style="background-image: url(&quot;/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&amp;size=16&quot;);"></div>
      </td>
      <td class="col1 even">
        <div class="text">2.&nbsp;Publish&nbsp;to&nbsp;WIP</div>
      </td>
      <td class="col2 odd">
        <div class="text">JH_anchor&nbsp;link</div>
      </td>
      <td class="col3 even">
        <div class="text">S070&nbsp;Public&nbsp;Site&nbsp;US&nbsp;English</div>
      </td>
      <td class="col4 odd" value="2015-12-23T14:17:51">
        <div class="text">12/23/2015 2:17 PM</div>
      </td>
      <td class="col5 even">
        <div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
      </td>
      <td class="col6 odd" value="">
        <div class="text">
          <span style="color: #f00"></span>
        </div>
      </td>
      <td class="col7 even" value="16">
        <div class="text">Suspended</div>
      </td>
      <td class="col8 odd">
        <div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
      </td>
      <td class="col9 even">
        <div class="text">Publishing&nbsp;Failed</div>
      </td>
    </tr>
    .....
  </tbody>
</table>

I have collection of rows. Inside each row i have 10 columns(td). I want to iterate to each row. For each row I want to get the 8th and 10 th column. Note :- The test case will get Fail if the 8th column value is "Suspended" and 10th column value is "Publishing Failed" or else the test case would get Pass

I tried the below logic

IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
foreach (var item in tableRow)
{

}

I'm not sure how to proceed further. Could anyone help me? Thanks in advance

Upvotes: 6

Views: 24278

Answers (3)

Leon Barkan
Leon Barkan

Reputation: 2703

Your logic is good:

IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
   rowTD = row.FindElements(By.TagName("td"));

   if(rowTD.Count > 9)
   {
      if(rowTD[8].Text.Equals("Suspended") && rowTD[10].Text.Equals("Publishing Failed");
      //test failed
   }
}

Upvotes: 17

Buaban
Buaban

Reputation: 5137

Try this:

foreach (var item in tableRow)
{
    IWebElement column7 = item.FindElement(By.CssSelector("[class*='col7']"));
    IWebElement column9 = item.FindElement(By.CssSelector("[class*='col9']"));

    if (column7.Text.Equals("Suspended") && column9.Text.Equals("Publishing Failed"))
        Assert.Fail("Failed because column8 is 'Suspended' and column10 is 'Publishing Failed'");
    else
        Assert.Pass();
}

Please note that this code will stop testing when it has found the "Suspended" and "Publishing Failed". If you want to continue testing until the final row in table, you have to use multiple assertions. NUnit, is it possible to continue executing test after Assert fails?

Upvotes: 0

alecxe
alecxe

Reputation: 474021

What if you would just try to find the rows having the 8th column value "Suspended" and 10th column value "Publishing Failed":

IList<IWebElement> rows = tableElement.FindElements(By.TagName("//table//tr[td[8]/div = 'Suspended' and td[10]/div = 'Publishing Failed']"));

Then, you can fail the test if rows list is not empty.

Upvotes: 0

Related Questions