Using VerifyElementChecked /VerifyElementNotChecked with if-Statement

Hello,
I have one problem this VerifyElementChecked /VerifyElementNotChecked.
I have pages this checkboxes, which I must activated / deactivated.
For example: I must deactivated a checkbox and I don’t know, it is active or not.
Code:
if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1) {
** WebUI.click(findTestObject(‘TestObject’))**
}

When the checkbox is active, the test runs without problem. When the checkbox is inactive, the test is failed and I receive the error message: Test Cases FAILED because (of) Unable to verify object ‘Object Repository/TestObject’ is checked (Root cause: Object ‘Object Repository/TestObject’ is not checked).
Is there a way to solve this Problem?

Thank you in advance!

Evgeniya Shirmanova said:

Hello,
I have one problem this VerifyElementChecked /VerifyElementNotChecked.
I have pages this checkboxes, which I must activated / deactivated.
For example: I must deactivated a checkbox and I don’t know, it is active or not.
Code:
if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1) {
** WebUI.click(findTestObject(‘TestObject’))**
}

When the checkbox is active, the test runs without problem. When the checkbox is inactive, the test is failed and I receive the error message: Test Cases FAILED because (of) Unable to verify object ‘Object Repository/TestObject’ is checked (Root cause: Object ‘Object Repository/TestObject’ is not checked).
Is there a way to solve this Problem?

Thank you in advance!

You need to add FailureHandling value into this keyword. By default, the value is ‘Stop On Failure’ which will display a failed message and stop you from there.

The suggested value in this case is ‘OPTIONAL’:
if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1, FailureHandling.OPTIONAL) {
** WebUI.click(findTestObject(‘TestObject’))**
}

Vinh Nguyen said:

Evgeniya Shirmanova said:

Hello,
I have one problem this VerifyElementChecked /VerifyElementNotChecked.
I have pages this checkboxes, which I must activated / deactivated.
For example: I must deactivated a checkbox and I don’t know, it is active or not.
Code:
if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1) {
** WebUI.click(findTestObject(‘TestObject’))**
}

When the checkbox is active, the test runs without problem. When the checkbox is inactive, the test is failed and I receive the error message: Test Cases FAILED because (of) Unable to verify object ‘Object Repository/TestObject’ is checked (Root cause: Object ‘Object Repository/TestObject’ is not checked).
Is there a way to solve this Problem?

Thank you in advance!

You need to add FailureHandling value into this keyword. By default, the value is ‘Stop On Failure’ which will display a failed message and stop you from there.

The suggested value in this case is ‘OPTIONAL’:
if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1, FailureHandling.OPTIONAL) {
** WebUI.click(findTestObject(‘TestObject’))**
}

Thanks you for quick response!
In the documentation is written that return type of verifyElementChecked / verifyElementNotChecked is boolean. If test is OK, it returns true and false otherwise.
But I don’t get false as return value, but an exception!

param.PNG

1 Like

if (WebUI.verifyElementChecked(findTestObject(‘TestObject’), 1, FailureHandling.OPTIONAL) {
** WebUI.click(findTestObject(‘TestObject’))**
**}

This works! Thanks**

I am facing the same problem even when I have set failure handling to option.
I am trying to verify if a checkbox is checked. If it is, it carries out further steps. If it is not checked, it first checks and then carries out those steps.
It works fine when the checkbox is not checked. However, when the checkbox is already checked, it unchecks it instead of directly carrying out the subsequent steps. Below is my code:

if (WebUI.verifyElementChecked(findTestObject(‘Page_CRM/Menu_Item_CRM’), 1, FailureHandling.OPTIONAL )) {
WebUI.getText(findTestObject(‘Page_CRM/Bcc_Email’))

WebUI.click(findTestObject('Page_Gmail/Email_Send_now_crm'))

WebUI.closeBrowser()

} else {
WebUI.check(findTestObject(‘Page_CRM/Menu_Item_CRM’))

WebUI.getText(findTestObject('Page_CRM/Bcc_Email'))

WebUI.click(findTestObject('Page_Gmail/Email_Send_now_crm'))

WebUI.closeBrowser()

}

Personally, it sounds like your if statement is failing, however, you say it unchecks the element. So, how about increasing the timeOut factor in your if statement to actually give it a chance to verify. Change the 1 second to 10 seconds, like below:

if (WebUI.verifyElementChecked(findTestObject('Page_CRM/Menu_Item_CRM'), 10, FailureHandling.OPTIONAL)) {

same issue even with increasing the timeOut factor.

Then how about confirming that the checkbox is not checked.

if (WebUI.verifyElementChecked(findTestObject('Page_CRM/Menu_Item_CRM'), 10, FailureHandling.OPTIONAL )) {
    WebUI.getText(findTestObject('Page_CRM/Bcc_Email'))

    WebUI.click(findTestObject('Page_Gmail/Email_Send_now_crm'))

} else {
    WebUI.verifyElementNotChecked(findTestObject('Page_CRM/Menu_Item_CRM'), 10, FailureHandling.OPTIONAL )
    
    WebUI.check(findTestObject('Page_CRM/Menu_Item_CRM'))

    WebUI.getText(findTestObject('Page_CRM/Bcc_Email'))

    WebUI.click(findTestObject('Page_Gmail/Email_Send_now_crm'))

}

WebUI.closeBrowser()