Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the count of rows from a web table - Selenium WebDriver Java

I am working on selenium webdriver using java. I am facing a problem. I have a web table and I have test case for add event functionality, that successfully adds the event in the table. Now I want to assert/verify if I got the added event in my table or not (Because if the functionality is broken, then date will not add in the table, and I can fail that test deliberately). The number of events are unknown before the start of test case. My logic for this test case verification is as follows:

  1. Add the event in the table. The code to add event is :

    public void addIndividualEvent(String eventDate, String eventName) throws Exception
    {
        driver.findElement(individualEventDate).sendKeys(eventDate);
        driver.findElement(individualEventName).sendKeys(eventName);
        driver.findElement(addIndividualEventBtn).click();
    }
    

    Now, after this step, there may be multiple events already in the table. So my next step would be to.

  2. Find the number of rows in the table.

  3. Construct a for loop that goes through each row and get the text of the date field.
  4. Add the result of for loop to a list.
  5. Then Assert.assertEquals(List,eventDate); will tell me if my added event is in the table or not.

Now, below is the HTML code of the table in my website.

<table class="table table-condensed table-hover event-list">
    <tbody>
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
        <tr class="study-event-row ng-scope" ng-repeat="event in events | orderBy:'-date'">
    </tbody>
</table>

I was able to construct the xpath that highlights all my rows available in the table.

xpath = //table[@class='table table-condensed table-hover event-list']/tbody/tr

Now the issue I am facing is that I dont know how to use this xpath to get the count of rows from my table. I tried to use xpath.getSize(); but this returned the dimensions of the table and not the actual count.

Can anyone tell me how can I get the no of rows? Thanks

like image 895
Uziii Avatar asked Jan 28 '26 14:01

Uziii


1 Answers

To get the rows, do the following- Using CSS selector:

List<WebElement> rows = driver.findElement(By.cssSelector("[class='table table-condensed table-hover event-list'] tr"));

Or if you have to use XPATH, then do:

List<WebElement> rows = driver.findElements(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody/tr"));

This is returning a list of WebElements. You can get the count here:

int count = rows.size();
System.out.println("ROW COUNT : "+count);

Then you can get text for each element and assert if it's as expected. As you are saying the text should be same for all elements, then you can do something like this:

for(WebElement e : rows) {
        assertEquals("expected text", e.getText());
    }
like image 65
AGill Avatar answered Jan 31 '26 03:01

AGill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!