gpt4 book ai didi

java - 如何检查所有复选框?尝试选中所有复选框,测试通过,但从视觉上我看不到它们已被选中

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:26 27 4
gpt4 key购买 nike

@Test
public void checkAllFilters() throws InterruptedException {
couponSearchPage.navigateToAvailableCoupons();
couponSearchPage.refineBarClick();
driver.findElement(By.name("group1")).isDisplayed();
WebElement checkBoxList = driver.findElement(By.name("group1"));
System.out.println("Is checkbox displayed? " + checkBoxList.isDisplayed());

List<WebElement> checkBoxFiltersList = driver.findElements(By.xpath("//div[@class='c -checkbox']//following:: input[@type='checkbox']"));
for(WebElement checkBox : checkBoxFiltersList){
Thread.sleep(2000);
checkBox.click();
Thread.sleep(2000);
//mainPage.ajaxClick(checkBox);
}
// Assert if any checkbox left checked
List<WebElement> allCheckboxList = driver.findElements(By.xpath("//div[@class='c-checkbox']//following:: input[@type='checkbox']"));
if(!allCheckboxList.isEmpty()){
Assert.fail();
}

}

尝试选中页面上的所有复选框,但即使测试通过,它也不会选中。 Xpth 不正确或者我需要 ajaxClcick?

<div class="c-checkbox">
<input type="checkbox" id="Baby &amp; ChildCare" class="c-settings-form__filter-checkbox c-checkbox__input js-update-filter" name="filter-Baby &amp; ChildCare" value="Baby &amp; ChildCare">
<label for="Baby &amp; ChildCare" class="c-checkbox__label"><span></span>Baby &amp; ChildCare</label>
</div>

最佳答案

点击label元素而不是input

List<WebElement> checkBoxFiltersList = driver.findElements(By.xpath("//div[@class='c-checkbox']//label"));

此外,您用来获取选中复选框的定位器是错误的,请使用:

  • xpath: //div[@class='c-checkbox']//input[@checked='true']
  • css:.c-checkbox input[checked='true']

同时将断言更改为正确检查是否选中了所有复选框:

Assert.assertEquals(driver.findElements(By.cssSelector(".c-checkbox input[checked='true']")).size(), 
checkBoxFiltersList.size());
<小时/>

更新,以不同断言的代码为例:

WebDriverWait wait = new WebDriverWait(driver, 10);

driver.findElement(By.cssSelector(".c-sort-filter")).click();

List<WebElement> checkboxes = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(".c-checkbox label")));
List<String> labels = checkboxes.stream().map(WebElement::getText).collect(Collectors.toList());

for (String label: labels) {
driver.findElement(By.cssSelector(String.format("label[for='%s']", label))).click();
// wait until loading disappear
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".c-spinner")));

// check if input is selected
wait.until(ExpectedConditions.elementToBeSelected(By.cssSelector(String.format("input[id='%s']", label))));
}

// check if input is selected by compare tags with checkboxes
List<String> selectedFilters = driver.findElements(By.cssSelector(".divider-view-filters-list .c-xbubble")).stream().map(WebElement::getText).collect(Collectors.toList());
Assert.assertEquals(selectedFilters, labels);

关于java - 如何检查所有复选框?尝试选中所有复选框,测试通过,但从视觉上我看不到它们已被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845186/

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