3

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

3
  • You are storing only one value in xpath? or that is list? Commented Oct 20, 2015 at 10:28
  • @HelpingHands The above xpath gives me(highlights) all the rows when I search this xpath through firepath on my browser. I need to count those highlighted rows, but I dont know how to achieve that. Commented Oct 20, 2015 at 10:44
  • 1
    I think you should count no. of rows using List<WebElement> rows = driver.findElements(by.tagname("tr")); rows.size(); Commented Oct 20, 2015 at 10:50

5 Answers 5

12

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());
    }
Sign up to request clarification or add additional context in comments.

Comments

3

Use the following code to get the row size of the table.

WebElement webElement=project.login.driver.findElement(By.xpath("tbodyXpath"));
List<WebElement> rows=webElement.findElements(By.xpath("tablerowXpath"));         
System.out.println("Size -------"+rows.size());

1 Comment

Please format your code blocks. Also explain your answer.
1

Assuming your XPath works, you can just do this...

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

It will return you the number of rows (TRs) in that table.

Comments

-1

You could use the XPath:

count(//table[@class='table table-condensed table-hover event-list']//tr)

This will count all the tr elements that are descendants of the table element with @class="table table-condensed table-hover event-list"

3 Comments

Wouldn't that require count to be a function?
Count is an in-built XPath function - it computes the number of nodes/elements in a node set
Could you provide me some documentation regarding this in-built function. It would help me a lot.
-1

Resolve the table object through your xpath, then use that WebElement as your SearchContext and get all TR elements within. The size of the return list should tell you your visible row count.

int EXPECTED_ROW_COUNT = 3; 

/**
 * Test case for counting the number of visible rows in a table.
 * @see "http://stackoverflow.com/questions/33233883/getting-the-count-of-rows-from-a-web-table-selenium-webdriver-java"
 *  @author http://stackoverflow.com/users/5407189/jeremiah
 */
@Test
public void testCountRows() {
    WebDriver wd = new FirefoxDriver();
    wd.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table");
    WebDriverWait wait = new WebDriverWait(wd, 10);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframeResult"));//Specific to test location @ w3schools!
    WebElement parentTable = wd.findElement(By.xpath("html/body/table/tbody")); 
    //WebElement parentTable = wd.findElement(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody"));


    //Now use the parentTable as the root of the search for all 'tr' children.
    List<WebElement> rows = parentTable.findElements(By.xpath("./tr"));
    Assert.assertTrue(EXPECTED_ROW_COUNT == rows.size());
    }

   /**
     * Test that you can use to direct at your url and interact with rows.
     */
    @Test
    public void testYourRowCount() {
        WebDriver wd = new FirefoxDriver();
        //FIXME
        wd.get("yourURLHere");
        WebElement parentTable = wd.findElement(By.xpath("//table[@class='table table-condensed table-hover event-list']/tbody"));
        //Now use the parentTable as the root of the search for all 'tr' children.
        List<WebElement> rows = parentTable.findElements(By.xpath("./tr"));
        //FIXME:  Do stuff with the rows.        
    }

3 Comments

I'm not sure what question this code is answering. The OP provided an XPath and HTML, why are you using some other random table???
The xpath provided links to a page I do not have. The parentTable reference in the example links to a page which emulates the described format of the problem. By swapping out the URL with the url being used, and replacing the parent table currently used with the one that's commented out the problem is the same. I see that that was not completely evident so I've appended an additional case to facilitate and call out the changes to prove the usage. Thank you for pointing that out.
Going back over this again, I agree I could have presented the information in a more useful fashion. @JeffC I appreciate you bringing this up, and I will try to make it a point to be more direct in my intent in the future. Again, thank you for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.