I want to look for a test object and if it cannot find it, report test case successful, instead of failing. Also acceptable to change for loop end index to something that can detect the correct number of loops.
Test Case
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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
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 com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
WebUI.openBrowser('https://dg.coupons.com/signin/?done=%2Fcoupons%2F', FailureHandling.STOP_ON_FAILURE)
WebUI.setText(findTestObject('Page_Sign In/input_EMAIL OR PHONE_username'), Username, FailureHandling.STOP_ON_FAILURE)
WebUI.setText(findTestObject('Page_Sign In/input_Password_password'), Password, FailureHandling.STOP_ON_FAILURE)
WebUI.click(findTestObject('Object Repository/Page_Sign In/input_Sign in_btn-sign-in'), FailureHandling.STOP_ON_FAILURE)
for (def index : (1..40)) {
WebUI.scrollToPosition(0, 99999, FailureHandling.STOP_ON_FAILURE)
WebUI.delay(0.5, FailureHandling.STOP_ON_FAILURE)
}
WebUI.scrollToPosition(0, 0, FailureHandling.STOP_ON_FAILURE)
WebUI.delay(0.5, FailureHandling.STOP_ON_FAILURE)
WebUI.click(findTestObject('Page_Coupons Gallery/span_Add', [('Slot') : 1]), FailureHandling.STOP_ON_FAILURE)
WebUI.delay(1, FailureHandling.STOP_ON_FAILURE)
WebUI.click(findTestObject('Page_Coupons Gallery/label_Ok I understand Close window'), FailureHandling.STOP_ON_FAILURE)
for (def index : (1..99999)) {
WebUI.click(findTestObject('Page_Coupons Gallery/span_Add', [('Slot') : (index * 2) + 1]), FailureHandling.STOP_ON_FAILURE)
}
Test Object:
xpath=(.//*[normalize-space(text()) and normalize-space(.)='â'])[${Slot}]/following::span[1]
I have. I canât seem to get the if statement to work. It always returns true even when it shouldnât.
Additionally, I still donât know what to do after that. How do I get the script to end immediately when true and report a successful test case?
See #2 above
Edit: I just realized why it was always returning true. Iâll get that fixed in a moment. I still need to know about getting the script to end immediately when true and report a successful test case.
xpath=(.//*[normalize-space(text()) and normalize-space(.)='â'])[${Slot}]/following::span[1]
which ${Slot} needed a default value of 1. This should be working, but it isnât. It doesnât find something that is there. It should be returning false in the current case. Project files below.
It would help if I could see the variables update during debugging, but no variables ever show up and I canât find a way to watch them
Are you expecting that the Slot variable within the test object would be defaulted to 1 ? Youâre not passing the variable Slot from your test case into it. I suspect, if you change verifyElementNotPresent to verifyElementPresent, you would find that the test case fails because the locator of span_Add would be
xpath=(.//*[normalize-space(text()) and normalize-space(.)='â'])[${Slot}]/following::span[1]
which since itâs not a valid locator (containing ${ }) the element cannot be found.
I see that, but are you confusing Test Case variable with Test Object variable ?
Unless you pass the Slot variable intofindTestObject('id', ['slot': slot]) The Slot variable within the test object will not be aware of the existence of the Slot test case variable.
@BetaLeaf Answering your many questions tidily would take me too long to construct the post so Iâm just going to dump some stuff here. Try these suggestions and cherry-pick what you need.
I think part of your problem is not waiting until the webpage is ready. I see you use a wait time of 2 seconds but that might not begin or end at appropriate times. A better strategy would be to wait for all objects that should be there to be proven to be there before you look for things that should not be there. Make sense? I think thatâs why youâre seeing some of your not-present attempts always return true. You can do that with a single method right there in your test case. Let me know what you think.
Any time you want to break out of a loop just do that:
for(...) {
//stuff
if(some_condition) {
break
}
}
If the test has issues it will report them. If it doesnât it will pass. You donât need code to say that specifically. All you need is to get the logic and flow correct.