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

}

Trying to check all check boxes on the page, but it doesn't check, even though test passes. Xpth is incorrect or I need 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>
Tetiana
  • 63
  • 6
  • Can you post a link to the page you are automating? – CEH Jan 21 '20 at 16:41
  • Unfortunately, I cannot. I just need to figure out how to create the right xPath I guess with HTML code above. – Tetiana Jan 21 '20 at 18:34
  • The 'right XPath' for the HTML code above is `//div[@class='c-checkbox']/input`. But I'm assuming that's not working, which is why you are here -- hence, the need to see full page HTML or link. It's impossible to know why your current choice of XPath is not working correctly without the larger context of what the entire page looks like. – CEH Jan 21 '20 at 19:24
  • https://stopandshop.com/coupons-weekly-circular/digital-coupons/#/available – Tetiana Jan 21 '20 at 20:08
  • Basically, I have to click on each Refine -> Filter option. – Tetiana Jan 21 '20 at 20:09

1 Answers1

0

Click on label element not on input.

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

Also with locator you use to get checked checkboxes is wrong, use:

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

Also change Assert to proper check if all checkboxes are selected:

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

Update, code with different assertions as example:

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);
Sers
  • 12,047
  • 2
  • 12
  • 31
  • If I use this xPath, it clicks on the first one, and after it fails. – Tetiana Jan 21 '20 at 20:37
  • `org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document`. I used ajax click method, the same - it clicks on the first option and after fails. – Tetiana Jan 21 '20 at 20:37
  • Because you have to wait until JavaScripts will completed. For that you can use `WebDriverWait`. Let me test – Sers Jan 21 '20 at 20:38
  • Thank you for your answer. I changed xPath and added Assert -Test passes, but I do not see that check boxes are checked. – Tetiana Jan 21 '20 at 21:31
  • Check answer update. You can use assertion you want, or split it in different test cases – Sers Jan 21 '20 at 21:36