How to make if-else if-else statements work with specific objects

Hello.

I’m creating a test case for login checks, which will run several times. My test data will include valid and non-valid login IDs and PASS.
When LOGIN button is clicked, two possible outcomes:

  1. user logs in and do log out
  2. user is shown error message about incorrect credentials and test should click CLOSE button

So basically test should check if CLOSE button is shown, then click it or else if it’s not detected, click LOG OUT button.
I was trying to use if - else if - else statements by using objects(web elements) as variables(set in Input) field, but that does not work for me. Is it possible at all to detect element on website and act accordingly using IF statements?
I would appreciate any help.

I would do something like this, just replace “closebutton” with where your object path is in object repository.

‘determine if close button displays’
if (WebUI.verifyElementPresent(findTestObject(‘CloseButton’), FailureHandling.OPTIONAL))
{
‘click close button’
WebUI.click(findTestObject(‘CloseButton’))
}

what you are doing is setting up an if statement to determine if you see the close button, the failure handling optional means that if it’s not present it will continue and not fail the test, it will mark the step with a yellow icon. If the is there it will click it with the click step. If it’s not there it will just continue.

Make sense?

1 Like

Example:
if (WebUI.verifyElementNotPresent(findTestObject(‘Page_CuraAppointment/btn_BookAppointment’), 10) == true)
{
WebUI.click(findTestObject(‘Page_CuraAppointment/btn_Continue’))
}

Pasted from http://forum.katalon.com/discussion/1700/how-can-i-click-on-a-element-while-another-element-is-not-present

image.png

2 Likes

I am getting error message that :-
Failed to check for element ‘Object Repository/LMO_HomePg/LMO_HomePg_FailedToStartLockAppOKButton’ visible (Root cause: Element ‘Object Repository/LMO_HomePg/LMO_HomePg_FailedToStartLockAppOKButton’ not found)

Code:-

if (Mobile.verifyElementVisible(findTestObject(‘Object Repository/LMO_HomePg/LMO_HomePg_FailedToStartLockAppOKButton’), 4) == true )

{

Mobile.tap(findTestObject(‘Object Repository/LMO_HomePg/LMO_HomePg_FailedToStartLockAppOKButton’), 1)

}

1 Like

same here

that is why I have failure handling. so if it fails it doesn’t stop the test but goes on.

1 Like

guys, i wrote small user keyword that allow you to excute xpath functions eg:
(count(//someElement[ @attr=“xyz”])) > 0 - element exist and returns boolean value on which you can build decision
you can find it here:


hope it helps

I am not able excute this if condition can any one help me …

if(WebUI.verifyElementPresent(findTestObject(‘Object Repository/Obj_UserNme,Pswd,Login/error_Login Failed/p_Login failed Please ensure t’), FailureHandling.CONTINUE_ON_FAILURE)){

println("--------- failed login-------------------")

}else {

WebUI.click(findTestObject(‘Object Repository/Menu/a_menu-toggle’))

WebUI.click(findTestObject(‘Object Repository/Menu/a_Logout’))

println("--------- log out successfull-------------------")

}

1 Like

any error?

Andrej Podhajský said:

any error?

Test Cases/Data Files/Excell data with login FAILED because (of) groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling) values: [TestObject - ‘Object Repository/Obj_UserNme,Pswd,Login/error_Login Failed/p_Login failed Please ensure t’, …]

Possible solutions: verifyElementPresent(com.kms.katalon.core.testobject.TestObject, int), verifyElementPresent(com.kms.katalon.core.testobject.TestObject, int, com.kms.katalon.core.model.FailureHandling), verifyElementNotPresent(com.kms.katalon.core.testobject.TestObject, int), verifyElementNotPresent(com.kms.katalon.core.testobject.TestObject, int, com.kms.katalon.core.model.FailureHandling), verifyAlertPresent(int, com.kms.katalon.core.model.FailureHandling)

waiting time is missing, try:

if(WebUI.verifyElementPresent(findTestObject('Object Repository/Obj_UserNme,Pswd,Login/error_Login Failed/p_Login failed Please ensure t'), 5, FailureHandling.CONTINUE_ON_FAILURE))

if(WebUI.verifyElementPresent(findTestObject(‘Object Repository/Obj_UserNme,Pswd,Login/error_Login Failed/p_Login failed Please ensure t’), 5, FailureHandling.CONTINUE_ON_FAILURE)){

println(“--------- failed login-------------------”)

}else {

WebUI.delay(10)

WebUI.click(findTestObject(‘Object Repository/Menu/a_menu-toggle’))

WebUI.click(findTestObject(‘Object Repository/Menu/a_Logout’))

println(“--------- log out successfull-------------------”)

}

Wait is added still i am gettting the error

Test Cases/Data Files/Excell data with login FAILED because (of) com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Obj_UserNme,Pswd,Login/error_Login Failed/p_Login failed Please ensure t’ located by ‘By.xpath: id(“login”)/div[@class=“container”]/div[@class=“row”]/div[@class=“col-sm-12 text-center”]/p[@class=“lead text-danger”][count(. | //p[(text() = ‘Login failed! Please ensure the username and password are valid.’ or . = ‘Login failed! Please ensure the username and password are valid.’)]) = count(//p[(text() = ‘Login failed! Please ensure the username and password are valid.’ or . = ‘Login failed! Please ensure the username and password are valid.’)])]’ not found

but this is another one…

Andrej Podhajský said:

but this is another one…

I am not getting you …

Andrej Podhajský said:

but this is another one…

See this total code , in this i need compare the text present are not …

ExcelData data = findTestData(‘Excell_Data’)

WebUI.openBrowser(‘’)

WebUI.navigateToUrl(‘CURA Healthcare Service’)

for (int i = 0; i <= (data.getRowNumbers() - 1); i++) {

login(data.internallyGetValue(‘Username’, i), data.internallyGetValue(‘Password’, i))

WebUI.click(findTestObject(‘Page_CURA Healthcare Service/button_Login’))

WebUI.delay(5)

if(WebUI.verifyTextPresent(‘Login failed! Please ensure the username and password are valid.’, false, FailureHandling.CONTINUE_ON_FAILURE)){

println(“--------- failed login-------------------”)

}else {

WebUI.delay(5)

WebUI.click(findTestObject(‘Object Repository/Menu/a_menu-toggle’))

WebUI.click(findTestObject(‘Object Repository/Menu/a_Logout’))

println(“--------- log out successfull-------------------”)

}

}

WebUI.closeBrowser()

def login(def usr,def psd) {

WebUI.setText(findTestObject(‘Page_CURA Healthcare Service/input_username’), usr)

WebUI.setText(findTestObject(‘Page_CURA Healthcare Service/input_password’), psd)

}

On this code exicution i am getting this error …
Unable to click on object ‘Object Repository/Menu/a_Logout’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Menu/a_Logout’ located by ‘By.xpath: //a[@href = ‘authenticate.php?logout’ and (text() = ‘Logout’ or . = ‘Logout’)]’ not found)

and that’s exactly as you wanted to be … FailureHandling.CONTINUE_ON_FAILURE will ALLWAYS throws exception to console and TC will be failed.
if you want only warning use FailureHandling.OPTIONAL

look at my example: code:

if(WebUI.verifyElementPresent(findTestObject('Object Repository/__Sandbox/to'),5, FailureHandling.CONTINUE_ON_FAILURE)){	println '*** IF after exception ***'} else {	println '*** ELSE after exception ***'}

console:

07-26-2018 12:18:54 PM - [START]  - Start action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Object Repository/__Sandbox/to), 5, CONTINUE_ON_FAILURE))07-26-2018 12:18:54 PM - [INFO]   - Finding Test Object with id 'Object Repository/__Sandbox/to'07-26-2018 12:18:54 PM - [INFO]   - Checking object07-26-2018 12:18:54 PM - [INFO]   - Checking timeout07-26-2018 12:18:54 PM - [INFO]   - Finding web element with id: 'Object Repository/__Sandbox/to' located by 'By.xpath: //a[contains(@class, 'FullNameX')]' in '5' second(s)07-26-2018 12:19:01 PM - [FAILED] - com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/__Sandbox/to' located by 'By.xpath: //a[contains(@class, 'FullNameX')]' not found07-26-2018 12:19:01 PM - [END]    - End action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Object Repository/__Sandbox/to), 5, CONTINUE_ON_FAILURE))07-26-2018 12:19:01 PM - [FAILED] - Test Cases/__Sandbox/New Test Case 2 FAILED because (of) com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/__Sandbox/to' located by 'By.xpath: //a[contains(@class, 'FullNameX')]' not found07-26-2018 12:19:01 PM - [START]  - Start action : Statement - Else07-26-2018 12:19:01 PM - [START]  - Start action : Statement - println("*** ELSE after exception ***")*** ELSE after exception ***07-26-2018 12:19:01 PM - [END]    - End action : Statement - println("*** ELSE after exception ***")07-26-2018 12:19:01 PM - [END]    - End action : Statement - Else07-26-2018 12:19:01 PM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case 2

as you can see, execution continued in else closure

2 Likes

and this is 3rd error

On this code exicution i am getting this error …
Unable to click on object ‘Object Repository/Menu/a_Logout’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Menu/a_Logout’ located by ‘By.xpath: //a[@href = ‘authenticate.php?logout’ and (text() = ‘Logout’ or . = ‘Logout’)]’ not found)

are you sure element is there?

same. Do you already know the answer?

FailureHandling.OPTIONAL as per the comments above works for me for mobile projects in terms of if else statements. Thanks team

Do you already know the answer?