0

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" class="self-center w-5 h-5" 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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you're answer is almost correct. To make it 100% correct can you please update your answer as following: Object obj = js.executeScript("return document.getElementsByName('includeDeletedPosts')[0].indeterminate"); document.getElementByName is not a js function and since getElementsByName is the valid function - it returns a list - and has to capture the first value from the list by .[0] -- if you can update your comment then I can accept it as the desired answer.
Oops, thank you for the correction. Just updated it!
0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.