Two ifs inside try catch

I am trying to cover some cases on the start up after downloading the app in simulator, if one scenario failed to logout then all scenario will failed . so to cover the following cases :

  1. passcode screen display and user isn’t logged in–> only enter the passcode
  2. passcode screen display and user logged in–> enter passcode then logout
  3. passcode did not display and user logged in → logout
  4. neither passcode display nor user logged in → do nothing

code :

'If pascode screen is displaying'
if (Mobile.verifyElementExist(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait)) {
	'Enter passcode'
	CustomKeywords.'custom.enterPhoneNumber'(GlobalVariable.passcode)
	'Wait for passcode screen to disappear'
	Mobile.waitForElementNotPresent(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait)
}
'If user logged in'
if (Mobile.verifyElementExist(findTestObject('002-In app/005-More/More'), GlobalVariable.avgWait)) {
	'Logout'
	WebUI.callTestCase(findTestCase('001-Common Test Cases/002-Tear down/002- Logout'), [:], FailureHandling.STOP_ON_FAILURE)
}

but if first test case failed then nothing will be performed

I think it would be easier for others to review if you format your code by putting three back ticks above and below your code, like ``` (found on the same key as the tilde)

'If pascode screen is displaying' 
if (Mobile.verifyElementExist(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait)) { 
    'Enter passcode' 
    CustomKeywords.'custom.enterPhoneNumber'(GlobalVariable.passcode) 
    'Wait for passcode screen to disappear' 
    Mobile.waitForElementNotPresent(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait) 
} 
'If user logged in' 
if (Mobile.verifyElementExist(findTestObject('002-In app/005-More/More'), GlobalVariable.avgWait)) { 
    'Logout'
    WebUI.callTestCase(findTestCase('001-Common Test Cases/002-Tear down/002- Logout'), [:], FailureHandling.STOP_ON_FAILURE) 
}

neither this worked :

if (Mobile.verifyElementExist(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait, FailureHandling.OPTIONAL)) {
    'Enter passcode'
    CustomKeywords.'custom.enterPhoneNumber'(GlobalVariable.passcode)

    'Wait for passcode screen to disappear'
    Mobile.waitForElementNotPresent(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait)
} else if (Mobile.verifyElementExist(findTestObject('002-In app/005-More/More'), GlobalVariable.avgWait ,  FailureHandling.OPTIONAL)) {
    'Logout'
    WebUI.callTestCase(findTestCase('001-Common Test Cases/002-Tear down/002- Logout'), [:])
} else (
	Mobile.waitForElementPresent(findTestObject('001-Onboarding/001-Startup page/001-Phone number fields/001-Phone number field/002-Phone number data/phoneNumberField'), 
    GlobalVariable.avgWait))```

worked on this way :

'If pascode screen is displaying'
if (Mobile.verifyElementExist(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait, FailureHandling.OPTIONAL)) {

	'Enter passcode'
	CustomKeywords.'custom.enterPhoneNumber'(GlobalVariable.passcode)

	'Wait for passcode screen to disappear'
	Mobile.waitForElementNotPresent(findTestObject('002-In app/006-Passcode screen/Please enter your passcode'), GlobalVariable.avgWait)

	Mobile.delay(GlobalVariable.avgWait)

	'If user logged in'
	if (Mobile.verifyElementExist(findTestObject('002-In app/005-More/More'), GlobalVariable.avgWait ,  FailureHandling.OPTIONAL)) {

		'Logout'
		WebUI.callTestCase(findTestCase('001-Common Test Cases/002-Tear down/002- Logout'), [:])

		'Wait for phone number field to present'
		Mobile.waitForElementPresent(findTestObject('001-Onboarding/001-Startup page/001-Phone number fields/001-Phone number field/002-Phone number data/phoneNumberField'),
				GlobalVariable.avgWait)

	}
} else if (Mobile.verifyElementExist(findTestObject('002-In app/005-More/More'), GlobalVariable.avgWait ,  FailureHandling.OPTIONAL)) {

	'Logout'
	WebUI.callTestCase(findTestCase('001-Common Test Cases/002-Tear down/002- Logout'), [:])

	'Wait for phone number field to display'
	Mobile.waitForElementPresent(findTestObject('001-Onboarding/001-Startup page/001-Phone number fields/001-Phone number field/002-Phone number data/phoneNumberField'),
			GlobalVariable.avgWait)
} else (
	Mobile.waitForElementPresent(findTestObject('001-Onboarding/001-Startup page/001-Phone number fields/001-Phone number field/002-Phone number data/phoneNumberField'),
	GlobalVariable.avgWait)
	)

Just a note that it looks like you have the last line with parentheses rather that curly braces.

1 Like