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:
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.
Find the number of rows in the table.
- Construct a for loop that goes through each row and get the text of the date field.
- Add the result of for loop to a list.
- 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
List<WebElement> rows = driver.findElements(by.tagname("tr")); rows.size();