If Else Statement Not Working: Groovlang.MissingMethodException: No signature of method

I am trying to create a if else statement that if the label value is equals to 0 zero it will failed the step but my code has an error on that part.
Here’s my code:
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.testng.keyword.TestNGBuiltinKeywords as TestNGKW
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
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 java.nio.file.Path as Path
import java.nio.file.Paths as Paths
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import org.apache.commons.lang3.StringUtils as StringUtils
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil

string (Failure = WebUI.getText(findTestObject(‘Admin/WP-0010 Container with Failures count and Its Listings/Page_Dashboard/label_4’),
FailureHandling.CONTINUE_ON_FAILURE))

if (Failure == 0) {
KeywordUtil.markFailedAndStop(‘Test has unexpected value. Failure=’ + Failure)
} else {
Condition here
}

Is there wrong with my condition or if there is another import file should I get? Thank you for any help.

While the error outputs:
Test Cases/Cool Innovations/Zeph Portal/C-Users/C2 User/C2-WP-0010 Container with Failures count and Its Listings FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: Script1656466262114.string() is applicable for argument types: (java.lang.String) values: [0]
Possible solutions: toString(), toString(), print(java.lang.Object), print(java.lang.Object), sprintf(java.lang.String, java.lang.Object), print(java.io.PrintWriter)
at C2-WP-0010 Container with Failures count and Its Listings.run(C2-WP-0010 Container with Failures count and Its Listings:54)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:445)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:436)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:415)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:284)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1656556804452.run(TempTestCase1656556804452.groovy:25)

Zero as in 0, is an Integer. However, “0” or ‘0’ is zero as a String (note the quotes around the zero). You can also have, 0.toString().

So, your statement,
if (Failure == 0) {
is trying to compare a String to an Integer, which should always fail*. Choose one of the options that I gave and try again.

Your error message, “groovy.lang.MissingMethodException:” usually means that you do not have the correct data type for your parameters or the correct number of parameters for your method.

If I read correctly, you are trying to get an error number from your browser’s, “label_4”. Is that correct? Just remember that the “getText()” method returns a String, never an Integer, even if it looks like a number.

Edit: And, I noticed you have parenthesis around your expression:

string (Failure = WebUI.getText(findTestObject(‘Admin/WP-0010 Container with Failures count and Its Listings/Page_Dashboard/label_4’), FailureHandling.CONTINUE_ON_FAILURE))

Remove the parenthesis in the above statement, so it becomes:

String failure = WebUI.getText(findTestObject('Admin/WP-0010 Container with Failures count and Its Listings/Page_Dashboard/label_4'), FailureHandling.CONTINUE_ON_FAILURE)

This might be the cause of your “groovy.lang.MissingMethodException:”.

Groovy - Variables (tutorialspoint.com)

1 Like

This could work for you…

//You would first get the string value
//Then convert the string to an int
//Then use the if statement...

String Failure = '0'
//String Failure = WebUI.getText(findTestObject('Object'), FailureHandling.CONTINUE_ON_FAILURE)
int FailureValue = Failure.toInteger()
if (FailureValue == 0) {
	println('FailureValue == 0') 
} else  {
	println('FailureValue != 0')
}