Search code examples
selenium-webdrivergroovykatalon-studio

Count Of Hidden Rows in WebTable By Using Selenium


I have a hide button in the application that hides duplicate data entries on web table. I have been trying to capture the number of hidden rows. See html and my approaches below. Every attempt I have tried ended up with 0. However, the result should be 2.

HTML CODE:

<tbody>
<tr role = "row" class="odd">...<tr/>
<tr role = "row" class="even">...<tr/>
<tr role = "row" class="odd">...<tr/>
<tr role = "row" class="even">...<tr/>
<tr role = "row" class="odd">...<tr/>
<tr role = "row" class="odd duplicate" style="display: none;" >...<tr/>
<tr role = "row" class="even duplicate" style="display: none;" >...<tr/>
</tbody>

def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.cssSelector("[display=none]"));
    int rowSize = rows_table.size();
    return rowSize;
}

Here is my other attempt:

def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.tagName("tr[not(contains(@style,'display: none;'))]"));
    int rowSize = rows_table.size();
    return rowSize;
}

If I ran the xpath as //*[@id='DataTables_Table_0']/tbody/tr[not(contains(@style,'display: none;'))] , I can find the hidden rows on the browser.

I have also tried this:

def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.tagName("tr"));
    int rowSize = rows_table.size();
    for(WebElement row: rows_table)
    {
        if(row.isDisplayed()==false)
        {
            rowSize = rowSize -1; 
        }
    }
    return rowSize;
}

After @Hac's comment, I tried JQuery. I ran the jQuery on browser, it works with no problem. But I get a "NULL" value returned in my function. I double checked the jQuery string that prompts correct in the comment line.

@Keyword
def getTableRowCountAfterHiding()
{
    def jQuery='$'+'("#DataTables_Table_0 tbody tr:visible").length'
    WebUI.comment(jQuery);
    def visibleRowCounts = new utils.ExecuteJavaScript().executeJavaScript(jQuery);
    return visibleRowCounts;
}

I defined utils to run JS as it is in below:

public class ExecuteJavaScript {
    //This keyword is designed to execute JS.
    @Keyword
    def executeJavaScript(String javascript) {
        ((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript(javascript)
    }
}

Solution

  • This worked:

    def getTableRowCountAfterHiding()
    {   
        WebDriver driver = DriverFactory.getWebDriver()
        List<WebElement> table = driver.findElements(By.xpath("//*[@id='DataTables_Table_0']/tbody/tr[not(contains(@style,'display: none;'))]"))
        int rowSize = table.size();
        return rowSize;
    }