使用 Cucumber Java 验证 HTML 表与数据表

Validating HTML Table with the datatable using Cucumber Java

提问人:Preksha 提问时间:10/13/2023 最后编辑:Malik BilalPreksha 更新时间:10/14/2023 访问量:24

问:

我正在使用 Cucumber Java 使用数据表验证 HTML 表,并编写了以下方法。

    public void verifyHtmlTableData(DataTable dataTable) {
        WebElement htmlTableElement = webDriver.findElement(By.xpath("//table[@class='tablename']"));
        List<WebElement> rowElements = htmlTableElement.findElements(By.tagName("tr"));
        List<List<String>> dataTableRows = dataTable.asLists(); //outer List<> is rows, inner List<> is cells

        // Check number of rows in Datatable and html table are equal
       Assert.assertEquals("Datatable rows should match the HtmlTable rows", dataTableRows.size(), rowElements.size());

        //Verify table header
        List<WebElement> headerElements = rowElements.get(0).findElements(By.xpath(".//th")); //get all the headers from the row WebElement
        compareTableData(headerElements, dataTableRows.get(0),0 );

        rowElements.remove(0);
        for (List<String> row : dataTableRows) { //loop through every row in the DataTable input
            int rowIdx = dataTableRows.indexOf(row);
            WebElement rowElem = rowElements.get(rowIdx); //get the row WebElement based on the index of the current row in the DataTable
            List<WebElement> cellElements = rowElem.findElements(By.xpath(".//td")); //get all the cells from the row WebElement
            compareTableData(cellElements, row, rowIdx);
        }
    }

    public void compareTableData(List<WebElement> cellElements, List<String> row, int rowIdx){
        for (String expectedCell : row) { //loop through every cell in the current DataTable row
            int cellIdx = row.indexOf(expectedCell);
            String actualCell = cellElements.get(cellIdx).getText();

            if (expectedCell == null) {
                expectedCell = "";
            }

            log.info("DataTable row " + rowIdx + ", cell " + cellIdx + ": " + expectedCell);
            log.info("Actual value on the page: " + actualCell);

            Assert.assertEquals("Expected value of cell should match actual value of cell", expectedCell, actualCell);
        }

    }

我无法从数据表中删除标题表,因此这不起作用。谁能帮忙?

我尝试了dataTableRows.remove(0);但它只删除了第一个元素

java selenium-webdriver 测试 数据表 cucumber

评论

0赞 M.P. Korstanje 10/21/2023
你是想从中删除吗?dataTableRows

答: 暂无答案