Home > front end >  How to handle intermediate state of a checkbox using Selenium in Java?
How to handle intermediate state of a checkbox using Selenium in Java?

Time:11-23

I'm automating a web page with selenium-java where there's a checkbox with tri-state. The HTML is this:

<input data-qa-anchor="field#includeDeletedPosts"  type="checkbox" name="includeDeletedPosts">

I want to create a method like this:

public void selectIncludeDeletedPosts(boolean isChecked) {
        if (isChecked) {
            if (!getDriver().findElement(INCLUDEDELETEDCHECKBOX).isSelected()) {
                    getDriver().findElement(INCLUDEDELETEDCHECKBOX).click();
            } else {
                getDriver().findElement(INCLUDEDELETEDCHECKBOX).click();
                getDriver().findElement(INCLUDEDELETEDCHECKBOX).click();
            }
        }
    }

Since it's a tri-state checkbox - there's an intermediate state where the checkbox is neither selected or unchecked. Hence, I can't use the following as isSelected can be used mainly for the regular bi-state checkboxes:

getDriver().findElement(By.name("includeDeletedPosts").isSelected

Basically I want to create one method that can handle all the states of the checkbox.

From here: https://css-tricks.com/indeterminate-checkboxes/ - I found out that "You can’t make a checkbox indeterminate through HTML. There is no indeterminate attribute. It is a property of checkboxes though, which you can change via JavaScript."

Hence, I was thinking if there's any way we can use javascript executer here.

Any help here would be highly appreciated.

CodePudding user response:

This is how you use the JavascriptExecutor to check the indeterminate attribute.

JavascriptExecutor js = (JavascriptExecutor)getDriver();
Object obj = js.executeScript("return document.getElementsByName('includeDeletedPosts')[0].indeterminate");

Object obj is a boolean type.

CodePudding user response:

My Final Method was this to handle all the checkbox states:

public QueryPostPage setIncludeDeletedPost(CheckboxStatus checkboxStatus) {
        JavascriptExecutor js = (JavascriptExecutor) getDriver();
        Object isIndeterminate = js.executeScript("return document.getElementsByName('includeDeletedPosts')[0].indeterminate");
        switch (checkboxStatus) {
            case CHECKED:
                if (isIndeterminate.equals(true)) {
                    selectIncludeDeletedPost();
                    break;
                } else if (!waitAndReturnElementIfDisplayed(INCLUDEDELETEDCHECKBOX).isSelected()) {
                    clickOnElementRepeatedly(INCLUDEDELETEDCHECKBOX,2);
                    break;
                }
            case UNCHECKED:
                if (isIndeterminate.equals(true)) {
                    clickOnElementRepeatedly(INCLUDEDELETEDCHECKBOX,2);
                    break;
                } else if (waitAndReturnElementIfDisplayed(INCLUDEDELETEDCHECKBOX).isSelected()) {
                    selectIncludeDeletedPost();
                    break;
                }
            case UNDEFINED:
                if (waitAndReturnElementIfDisplayed(INCLUDEDELETEDCHECKBOX).isSelected()) {
                    clickOnElementRepeatedly(INCLUDEDELETEDCHECKBOX,2);
                } else {
                    selectIncludeDeletedPost();
                }
                break;
        }
        return this;
    }
  • Related