If statement does not continue - if false

Hi there,
I have an if statement that works when the error is caught and clicks the button to continue:

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false)== true)

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

but when there is no error - when the condition is false, it does not continue with the rest of the test case.

I have tried :

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false, FailureHandling.CONTINUE_ON_FAILURE)== true)

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

This then has the desired effect, but if the error is caught again - the test fails

How do do i click the button when the error is there and continue and continue if the is no error?

1 Like

You need to set FailureHandling.OPTIONAL so the test continues execution and if every other step passes, the test status will be “passed”.

Otherwise, if FailureHandling is CONTINUE_ON_FAILURE the test will continue but it will be marked as “failed”.

2 Likes

When i do use the CONTINUE_ON_FAILURE - if the text is “An error occurred and your order was not placed. Please try again” is present the button is not clicked the test then fails - where it should continue

Can you share more of your script?

This is my script and below the error i get in the console:

(In this case the error is on the page - but the code is not seeing it - so the test fails)

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

CustomKeywords.‘customLogin.Login.loginIntoApplicationWith’()

WebUI.callTestCase(findTestCase(‘AddItemToCart/Cart - AddItem’), [:], FailureHandling.STOP_ON_FAILURE)

WebUI.waitForElementClickable(findTestObject(‘Checkout/ProceedToCheckOutButton’), GlobalVariable.timeout)

WebUI.click(findTestObject(‘Checkout/ProceedToCheckOutButton’))

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘Checkout/DeliverMyOrderButton’))

WebUI.click(findTestObject(‘Checkout/SelectDeliveryAddress’))

WebUI.takeScreenshot()

WebUI.waitForElementPresent(findTestObject(‘Checkout/ContinueButton’), GlobalVariable.timeout)

WebUI.click(findTestObject(‘Checkout/ContinueButton’))

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘Checkout/ContinueButton’))

WebUI.waitForElementClickable(findTestObject(‘Checkout/Donat5Button’), GlobalVariable.timeout)

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘Checkout/Donat5Button’))

CustomKeywords.‘customPaymentSelector.PaymentSelector.navigatePaymentType’(‘eBucksSpend your eBucks’)

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘Checkout/PayWithButton’))

WebUI.waitForPageLoad(GlobalVariable.timeout)

//if (WebUI.verifyElementPresent(‘eBucks/ErrorOnCheckoutAlert’, GlobalVariable.timeout) == true)

//{

// WebUI.click(findTestObject(‘Checkout/PayWithButton’))

//}

//if (WebUI.verifyElementPresent(‘eBucks/ErrorOnCheckoutAlert’, GlobalVariable.timeout, FailureHandling.OPTIONAL)== true) {

// WebUI.click(findTestObject(‘Checkout/PayWithButton’))

//}

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false, FailureHandling.CONTINUE_ON_FAILURE)== true)

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

WebUI.setText(findTestObject(‘eBucks/RSAIDTextBox’), ‘6805550068084’)

WebUI.setText(findTestObject(‘eBucks/AccountPIN’), ‘2020’)

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘eBucks/Login’))

WebUI.verifyTextPresent(‘Pay with eBucks’, false)

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘eBucks/eBucksContinueButton’))

WebUI.takeScreenshot()

WebUI.click(findTestObject(‘eBucks/PayWithEbucks’))

titel = WebUI.getWindowTitle()

WebUI.verifyMatch(titel, ‘Checkout | eBucks’, false)

CustomKeywords.‘customLogout.Logout.LogoutAfterConfirmation’()

ERROR Log

1-12-2018 04:42:01 PM - [START] - Start action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyTextPresent(“An error occurred and your order was not placed. Please try again.”, false, CONTINUE_ON_FAILURE) == true)

11-12-2018 04:42:01 PM - [INFO] - Checking text present

11-12-2018 04:42:01 PM - [INFO] - Finding text ‘An error occurred and your order was not placed. Please try again.’ on page

11-12-2018 04:42:01 PM - [FAILED] - Text ‘An error occurred and your order was not placed. Please try again.’ is not present on page

11-12-2018 04:42:01 PM - [END] - End action : Statement - If (com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyTextPresent(“An error occurred and your order was not placed. Please try again.”, false, CONTINUE_ON_FAILURE) == true)

11-12-2018 04:42:01 PM - [START] - Start action : setText

11-12-2018 04:42:01 PM - [INFO] - Finding Test Object with id ‘Object Repository/eBucks/RSAIDTextBox’

11-12-2018 04:42:01 PM - [INFO] - Checking object

11-12-2018 04:42:01 PM - [INFO] - Checking text

11-12-2018 04:42:01 PM - [INFO] - Checking timeout

11-12-2018 04:42:01 PM - [INFO] - Finding web element with id: ‘Object Repository/eBucks/RSAIDTextBox’ located by ‘By.xpath: //input[@name=‘username’][contains(@id,‘username’)]’ in ‘30’ second(s)

11-12-2018 04:42:32 PM - [INFO] - Unable to find the element located by ‘By.xpath: //input[@name=‘username’][contains(@id,‘username’)]’. Please recheck the objects properties to make sure the desired element is located.

11-12-2018 04:42:32 PM - [INFO] - Begin finding web elements by trial and error method

11-12-2018 04:42:32 PM - [INFO] - Web element found by trial and error method is not used because we can’t guarantee it reflects the intended element. See detailed troubleshooting guide for common issues: https://docs.katalon.com/x/awXR

11-12-2018 04:42:33 PM - [FAILED] - Unable to set text ‘6809160068084’ of object ‘Object Repository/eBucks/RSAIDTextBox’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/eBucks/RSAIDTextBox’ located by ‘By.xpath: //input[@name=‘username’][contains(@id,‘username’)]’ not found)

11-12-2018 04:42:33 PM - [END] - End action : setText

11-12-2018 04:42:33 PM - [FAILED] - Test Cases/eBucksTestCases/eBucks - SuccessfullPayment FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Text ‘An error occurred and your order was not placed. Please try again.’ is not present on page

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:32)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:65)

at com.kms.katalon.core.webui.keyword.builtin.VerifyTextPresentKeyword$_verifyTextPresent_closure1.doCall(VerifyTextPresentKeyword.groovy:79)

at com.kms.katalon.core.webui.keyword.builtin.VerifyTextPresentKeyword$_verifyTextPresent_closure1.call(VerifyTextPresentKeyword.groovy)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:21)

at com.kms.katalon.core.webui.keyword.builtin.VerifyTextPresentKeyword.verifyTextPresent(VerifyTextPresentKeyword.groovy:83)

at com.kms.katalon.core.webui.keyword.builtin.VerifyTextPresentKeyword.execute(VerifyTextPresentKeyword.groovy:68)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyTextPresent(WebUiBuiltInKeywords.groovy:1677)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyTextPresent$9.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)

at Script1540996771538.run(Script1540996771538.groovy:63)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)

at TempTestCase1542033654562.run(TempTestCase1542033654562.groovy:22)

at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)

at groovy.lang.GroovyShell.run(GroovyShell.java:518)

at groovy.lang.GroovyShell.run(GroovyShell.java:507)

at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)

at groovy.ui.GroovyMain.run(GroovyMain.java:384)

at groovy.ui.GroovyMain.process(GroovyMain.java:370)

at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)

at groovy.ui.GroovyMain.main(GroovyMain.java:109)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)

at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)

The test continued after the “if” branch. This is the reason test failed:

11-12-2018 04:42:33 PM - [FAILED] - Unable to set text ‘6809160068084’ of object ‘Object Repository/eBucks/RSAIDTextBox’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/eBucks/RSAIDTextBox’ located by ‘By.xpath: //input[@name=‘username’][contains(@id,‘username’)]’ not found)

Are you sure the xpath to the ‘RSAIDTextBox’ is correct?

Yes because at this stage the error was on the page, but it did not click the button in the if statement, but went straight to the next statement.

My test is as follows:

login
add item to cart

checkout
click pay with button (sometimes in staging when you click this button there is an error on the page - i would just have to click the button again, hence the if statement)
*when i use the if statement WITHOUT failure handling - if the error is on the page it click the button and continues, however if there is not error on the page it fails to continue with the test
*when i use the if statement WITH failure handling - if the error is on the page it does not click the button, however if there is no error on the page it continues with the test

Do you think the better approach to be upon error close the browser?

try

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false, FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

take out the ==true and just verify it passes

another route to try would be:

if (WebUI.verifyElementText(findTestObject(‘your object path here’), ‘An error occurred and your order was not placed. Please try again.’, FailureHandling.CONTINUE_ON_FAILURE)) {

WebUI.click(findTestObject(‘Checkout/PayWithButton’))

}

B L said:

try

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false, FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

take out the ==true and just verify it passes

another route to try would be:

if (WebUI.verifyElementText(findTestObject(‘your object path here’), ‘An error occurred and your order was not placed. Please try again.’, FailureHandling.CONTINUE_ON_FAILURE)) {

WebUI.click(findTestObject(‘Checkout/PayWithButton’))

}

B L said:

try

if (WebUI.verifyTextPresent(‘An error occurred and your order was not placed. Please try again.’, false, FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI._click_(_findTestObject_('Checkout/PayWithButton'))

}

take out the ==true and just verify it passes

another route to try would be:

if (WebUI.verifyElementText(findTestObject(‘your object path here’), ‘An error occurred and your order was not placed. Please try again.’, FailureHandling.CONTINUE_ON_FAILURE)) {

WebUI.click(findTestObject(‘Checkout/PayWithButton’))

}

This is not working …

if (WebUI.VerifyElementChecked(findTestObject(‘Search_Enable/Page_nopCommerce administration/input_Enable Search_EnableSear’),FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI.comment('Already enabled')

} else {

WebUI.click(findTestObject('Search\_Enable/Page\_nopCommerce administration/input\_Enable Search\_EnableSear'))



WebUI.comment('Now enabled')

}

ARTI said:

This is not working …

if (WebUI.VerifyElementChecked(findTestObject(‘Search_Enable/Page_nopCommerce administration/input_Enable Search_EnableSear’),FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI.comment('Already enabled')

} else {

WebUI.click(findTestObject('Search\_Enable/Page\_nopCommerce administration/input\_Enable Search\_EnableSear'))



WebUI.comment('Now enabled')

}

What error are you getting? looking at your script you are missing timeout on the verify element checked. try this:

if (WebUI.VerifyElementChecked(findTestObject(‘Search_Enable/Page_nopCommerce administration/input_Enable Search_EnableSear’), 3, FailureHandling.CONTINUE_ON_FAILURE))

{

WebUI.comment('Already enabled')

} else {

WebUI.click(findTestObject('Search\_Enable/Page\_nopCommerce administration/input\_Enable Search\_EnableSear'))



WebUI.comment('Now enabled')

}