简体   繁体   中英

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

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());
    }

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 ( TR s) in that table.

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());

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"

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.        
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM