How to extract second word from text


I’m struggling with extracting text via GetText Keyword. After using GetText I have 5 words extracted from Object, and I need to get only second one. I cannot use substring option as this word have different length each time.

At the beginning I’m declaring new variable:
String selectedChannelName = WebUI.getText(findTestObject(randomChannel))

after that some part of test is going on, and after opening new menu I need to verify that text which contains name is the same one as stored in selectedChannelName variable. So I’m getting name of new element in other menu:

String displayedChannelname = WebUI.getText(findTestObject('Object Repository/xxxx/xxx/xxx/xxx/xxx/div_channelAddedToPackage'))

and then I need to use previously obtained name from 1st menu:

WebUI.verifyMatch(displayedChannelname, selectedChannelName, false)

But in logs I have stack that name does not match as I have much more text extracted in selectedChannelName.

xxx/xxxx/xxx/xxx/xxx/Add a new service group FAILED.
com.kms.katalon.core.exception.StepFailedException: Actual text ‘Channel 3’ and expected text ‘SD
Channel 3
2 models
No’ are not matched

I need to extract “Channel 3” name (it may have different length each time)? Maybe it is possible to extract it to some kind of a list?

You will likely get better results using string.split

String partName = selectedChannelName.split(/\s/)[1] // 0 == first, 1 == second, etc
WebUI.verifyMatch(displayedChannelname, partName, false)
1 Like

Thanks! It worked well :wink: But in the mean time, I have found my mistake. Xpath was a little bit inaccurate and it was taking text from whole row and not one element in it. But Split will be helpful in the future!

Thanks again!


multiple way to do that
etc etc