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.