If and Else statement

I am new into Katalon and trying to understand if and else or any other conditional logic.

I am using sample katalon website CURA Healthcare Service
and trying to inspest element for invalid username and password and Valid username and password.
I am trying to do following

  1. Enter invalid username and password, Find element(error) is there.
  2. Enter valid username and password, Check if the element is there after log in

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

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

WebUI.click(findTestObject(‘Login Page of Demo Application/button_Login’))

if (WebUI.verifyElementPresent(findTestObject(‘Page_CURA Healthcare Service (3)/p_Login failed Please ensure t’)) == true) {

WebUI._verifyElementPresent_(_findTestObject_('Make Appointment Page/heading_Make Appointment'), 0)

} else {

WebUI._setText_(_findTestObject_('Page\_CURA Healthcare Service (2)/input\_username'), 'John Doe')



WebUI._setText_(_findTestObject_('Page\_CURA Healthcare Service (2)/input\_password'), 'ThisIsNotAPassword')

}

Any Help would be helpful

I think you don’t need any “if” and “else” in order to accomplish what you want to do.

// enter invalid username and passwordWebUI.setText(findTestObject('Page_CURA Healthcare Service (2)/input_username'),               demo_usn)
WebUI.setText(findTestObject('Page_CURA Healthcare Service (2)/input_password'),              demo_pwd)// try to login but...
WebUI.click(findTestObject('Login Page of Demo Application/button_Login'))// will fail
WebUI.verifyElementPresent(    findTestObject('Page_CURA Healthcare Service (3)/p_Login failed Please ensure t'))// still you can try againWebUI.verifyElementPresent(findTestObject('Make Appointment Page/heading_Make Appointment'), 0)
// type valid username and passwordWebUI.setText(findTestObject('Page_CURA Healthcare Service (2)/input_username'),             'John Doe')
WebUI.setText(findTestObject('Page_CURA Healthcare Service (2)/input_password'),             'ThisIsNotAPassword')// life goes on ...

Isn’t it sufficient?

Thank you.

That worked for me

In my condition always if part executed, never goes to else and so ;when if condition fails program still search for elements in if condition and then fails obviously it wont find those elements on page -
below is my code-
"
String string_MatchRecords = WebUI.getText(findTestObject(‘Object Repository/Approce Application_GOAMTCH/total_records’))

	println(string_MatchRecords)

	String match_record = string_MatchRecords.substring(string_MatchRecords.lastIndexOf("/")+1);
	println(match_record)
	if (match_record>1)

	{
		WebUI.delay(2)

		WebUI.click(findTestObject('Object Repository/Approce Application_GOAMTCH/create_new'))
		WebUI.click(findTestObject('Object Repository/Approce Application_GOAMTCH/generateID_okButton'))
	}
	else{
		WebUI.delay(3)
		WebUI.click(findTestObject('Object Repository/Approce Application_GOAMTCH/createAsNew_yes'))
		//WebUI.click(findTestObject('Object Repository/Approce Application_GOAMTCH/create_yes_button'))
		WebUI.delay(1)
		WebUI.click(findTestObject('Object Repository/Approce Application_GOAMTCH/generateID_okButton'))
	}

The problem is that you are comparing a String value to an int:

String match_record = string_MatchRecords.substring(string_MatchRecords.lastIndexOf("/")+1)
if(match_record>1) {
   ...
}

You need to convert your string into an int to make this work:

int match_record = Integer.parseInt(string_MatchRecords.substring(string_MatchRecords.lastIndexOf("/")+1))
if(match_record>1) {
    ...
}
1 Like