gpt4 book ai didi

java - 从 selenium 验证表列顺序

转载 作者:行者123 更新时间:2023-12-01 09:35:55 26 4
gpt4 key购买 nike

要求是根据列标题名称验证下表的列顺序是否正确。

因此,我写了一个方法来掩盖我的要求,它接受按所需顺序排列的表头名称的数组列表 [String]。

但它显然没有通过软断言满足我的要求,因为“isDisplayed()”方法总是返回“false”。

有人可以建议我进一步修改并修复它吗?

表的 HTLM 代码:

<table id="examMarkEntryExamList" class="display table table-bordered table-striped dynamic-table display_header_class">
<thead>
<tr>
<th class="text-center sortable sorted order1">Academic Year</th>
<th class="text-center sortable sorted order1">Curriculum</th>
<th class="text-center sortable sorted order1">Grade</th>
<th class="text-center sortable sorted order1">Semester/Term</th>
<th class="text-center sortable sorted order1">
<a class="pagination-cuser-point">Exam Code</a>
</th>
<th class="text-center sortable sorted order1">Actions</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2016-2016</td>
<td>LOCAL</td>
<td>GRADE11</td>
<td>2nd Term</td>
<td>G11SecondTerm</td>
<td>
</tr>
</tbody>
</table>

Selenium [Java] 验证列顺序的方法

public void verifyColumnOrder(WebDriver driver, String tableId, ArrayList<String> columnHeaderList) {

SoftAssert softassert = new SoftAssert();
String relativeXpath = "//table[contains(@id,'"+tableId+"')]/";
for (String columnHeader : columnHeaderList) {

relativeXpath = relativeXpath + "/following-sibling::th[contains(.,'" + columnHeader + "')]";
softassert.assertTrue(driver.findElement(By.xpath(relativeXpath)).isDisplayed());
}
softassert.assertAll();
}

最佳答案

另一种解决方案是使用 getText() 直接获取 thead 的文本。它将返回所有可见标题的串联,这将更容易比较:

public void verifyColumnOrder(WebDriver driver, String tableId, ArrayList<String> columnHeaderList) {

String expectedHeaders = String.join(" ", columnHeaderList);
String visibleHeaders = driver.findElement(By.cssSelector("[id='" + tableId + "'] thead")).getText();

Assert.assertEquals(visibleHeaders, expectedHeaders);
}

关于java - 从 selenium 验证表列顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com