Check if suggested words contain String

Hello everyone,
I just started a new job and here we are working with katalon studio so I am a bit new to this.

I am going to a web and write into a search bar a destination and then wait for what the search bar is going to suggest me. Then I am getting text of the suggested words and I want to check if the suggested words contain string which is the searched destination.

ex: I go to travel agency website and write “Thailand” to the search bar, then check the pop-up with the suggested words and get the text of it - then I want to check if the text that I got contains “Thailand”.
Code:

WebUI.openBrowser(’’)

WebUI.maximizeWindow()

WebUI.navigateToUrl(GV.URL)

WebUI.setText(findTestObject(‘Search/SearchInput’), GV.Zeme)

String suggestWords = WebUI.getText(findTestObject(‘Search/SuggestedWords’))

print(suggestWords)

WS.containsString(suggestWords, GV.Zeme , false)

where GV.Zeme is the global variable definied as a String in profile.

E: suggestWords looks like this “Thajsko OD 24 290 KČ
Thajsko > Phuket a okolí OD 27 990 KČ
Thajsko > Koh Chang a okolí OD 34 290 KČ”

Console log:

2020-09-25 10:09:37.281 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/SearchFunction/CheckSuggestedWord FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.containsString() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.Boolean) values: [GV.Zeme, suggestWords, false]
Possible solutions: containsString(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, boolean), containsString(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, boolean, com.kms.katalon.core.model.FailureHandling)
at CheckSuggestedWord.run(CheckSuggestedWord:31)
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:339)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1601021364097.run(TempTestCase1601021364097.groovy:25)

I tried to do similar thing before but could not work this out and eventually just did if else workaround but I want to be able to use the functions that katalon provides.

I hope it is not too confusing. Thanks in advance guys!

Hello,

check this, there are needed as parameters return object and String

1 Like

@KDK The error message, MissingMethodException, means you do not have the statement, WS.containsString, with the correct parameters, either because of Data Type (an Integer, or Object instead of a String or vice versa) or number of parameters (like missing that darn timeout, again) :frowning:

You can take your mouse and hover over each of the parameters and check their data type. As Timo has given the proper data types of the Contains String, I don’t think you can use it to compare the String to another String, but are supposed to compare against a TestObject as the first parameter.

Also, another suggestion I am going to give is adding, toString() to your GlobalVariable to ensure it is a String rather than an Object.

WS.containsString(findTestObject(‘Search/SuggestedWords’), GV.Zeme.toString(), false)

If you copy and paste from this forum, make sure all smart quotes, which have a curve or curl to them, are changed to straight quotes–just redo all quotes within KS.

1 Like

Hi,
thanks for trying to help me but I went through this doc before and I still do not understand where is my mistake or what I am doing wrong.

Change your statement to be like above.

1 Like

Hi,
thanks for the explanation now I get it how it is supposed to work. Also the first reply was to the first response I just need to wait to get my post approved by mods since I am not verified.

I tried your line and it was not working sadly even though it feels like it should.
Error:

Test Cases/SearchFunction/CheckSuggestedWord FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.containsString() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.String, java.lang.Boolean) values: [TestObject - ‘Object Repository/Search/SuggestedWords’, Řecko, …]
Possible solutions: containsString(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, boolean), containsString(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, boolean, com.kms.katalon.core.model.FailureHandling)
at CheckSuggestedWord.run(CheckSuggestedWord:35)
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:339)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1601366913578.run(TempTestCase1601366913578.groovy:25)

Not really sure what is happening. I also tried to define suggestWords as TestObject but that did not work out. Thanks so far for trying to help though!

E: snippet of the code now

WebUI.openBrowser(’’)

WebUI.maximizeWindow()

WebUI.navigateToUrl(GV.URL)

WebUI.setText(findTestObject(‘Search/SearchInput’), GV.Zeme)

WS.containsString(findTestObject(“Search/SuggestedWords”), GV.Zeme, false)

Reading more into the WS.containsString() method, I see it wants the first parameter to be a “ResponseObject”, not a TestObject. Are you familiar with creating a new ResponseObject from a String or casting a TestObject to a ResponseObject (don’t know if we can)?

https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/ResponseObject.html

If not, then how about try the Groovy method contains (you may have to also you the split method to create an array of strings).

https://www.tutorialspoint.com/groovy/groovy_contains.htm

1 Like

If you want to match against more that one word via OR
if ((suggestedWords == ‘word1’) || (suggestedWord == ‘word2’))
{
AssertResult = true
}

match one to one

assert suggestWords == varMyValidationWord

can also add a variable by the variable tab on test case instead of the var.

If you want to match against more that one word via AND
if ((suggestedWords == ‘word1’) && (suggestedWord == ‘word2’))
{
AssertResult = true
}

1 Like

Thanks guys for replying. Sadly I do not know too much about converting String into TestObject and considering it is actually way harder for me to use a simple keyword in the app than coding it self I just went with the scripting route and did this :

WebUI.openBrowser(’’)

WebUI.maximizeWindow()

WebUI.navigateToUrl(GV.URL)

WebUI.setText(findTestObject(‘Search/SearchInput’), GV.Zeme)

String suggestWords = WebUI.getText(findTestObject(‘Search/SuggestedWords’))

if (suggestWords.contains(GV.Zeme)) {
println('Suggested words contain ’ + GV.Zeme)
KeywordUtil.markPassed()
} else {
KeywordUtil.markFailed()
}

I wanted to use the default function of katalon but after such issues is prolly just better to do it this way. Anyway thank you guys for helping me out !

@KDK strings in Groovy are seen as an ordered sequence, so a simple assert should work, e.g:

assert suggestWords.contains(GV.Zeme)

you can add also a failure message:

assert suggestWords.contains(GV.Zeme) : "Test failed because blah blah"
1 Like

you can also add useful messages to the marked passed or failed util.

My example:

if ((this == this) == true) {
KeywordUtil.markPassed(’*** here is my unique passed message to console’)
(if you want to see comparrison strings–>) println((((This + System.lineSeparator()) + This) + System.lineSeparator()) + ‘Verification Strings’)
} else {
KeywordUtil.markFailed(’***Here is my unique fail message’)

}

my println would look like this:

This
This
Verifications Strings

Your Example:

if (suggestWords.contains(GV.Zeme)) {
println('Suggested words contain ’ + GV.Zeme)
KeywordUtil.markPassed(‘Unique Pass Message’)
} else {
KeywordUtil.markFailed(‘Unique Fail Message’)
}

you did a nice job figuring this out :+1:

1 Like