Determine if a checkbox has been checked/selected

On the surface it seems like an easy test, check if a checkbox has been selected and perform an action. After trying it out, the process doesn't follow the usual pattern.

My first attempt looked like this:
checked=WebUI.verifyElementChecked(findTestObject('Object/checkbox-item'),10)

if(checked){
    True action
}else{
    False action
}

That works, but throws an exception when the condition is false. And that looks really bad in the log files.

The correct way to handle the situation is to add the FailureHandling option so you don't get the error message.

In the code below, the status of the checkbox is determined. If it's already checked, enter the search criteria into the input field. If the checkbox isn't selected, enter the search criteria AND check the checkbox.

For my situation, I need that box to be checked. It would be just as easy to add the code that unchecks it or does something else. But the main action happens within the IF statement and the FailureHandling.OPTIONAL. This allows the processing to continue and code flows normally.

if (WebUI.verifyElementChecked(findTestObject('Object Location/checkbox-My Branch Only'),10,FailureHandling.OPTIONAL)){
    WebUI.setText(findTestObject('Object Location/input-Inventory Search Field'), inventorySearch)

} else {
    WebUI.setText(findTestObject('Object Location/input-Inventory Search Field'), inventorySearch)
    WebUI.click(findTestObject('Object Location/checkbox-My Branch Only'))
}
Maybe I should've written that in a different font.

Author Signature for Posts

0