Verify text from a range of acceptable values

I have a field in a mobile app where I’m expecting one of two or three values (i.e. the test should pass if the value of this field is “One”, “Two” or “Three”, but fail if it’s anything else.

How could I go about this please? Any help would be greatly appreciated.

@Kevin_McAndrew I would try to go with a CASE block first to see if that works, like the below link:

In your case, the Case switches would be “One”, “Two” or “Three” and perhaps have the “default” switch have a failure statement or something. Just a note that you do not need the switch variable to be a GlobalVariable like in the example. Just have your object reference (or a variable) be the switch indicator.

If you are not familiar with Case blocks, you do need a break; statement to end each block except the “default” one (it’s at the end anyways).

switch (refItem) {
    case 'One':
        println('One');
        break;
    case 'Two':
        println('Two');
        break;
    case 'Three':
        println('Three');
        break;

    default:
        println('Oh no!');
        break;
}

How about using Verify Match keyword.

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

def fieldText = Mobile.getText(findTestObject(...), 10) 

'Use Mobile keyword'
Mobile.verifyMatch(fieldText, '.*(One|Two|Three).*', true, FailureHandling.STOP_ON_FAILURE)

You can also use the in syntax in an if/else:

myList = ["One", "Two", "Three"]

item = "something"

println item

if (item in myList) {
	println "test passed"
} else {
	println "test failed"
}

running with two different values:

2020-12-22 13:30:36.204 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2020-12-22 13:30:36.208 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2020-12-22 13:30:36.501 INFO  c.k.katalon.core.main.TestCaseExecutor   - (Default) variable = []
2020-12-22 13:30:36.607 DEBUG testcase.New Test Case                   - 1: myList = ["One", "Two", "Three"]
2020-12-22 13:30:36.614 DEBUG testcase.New Test Case                   - 2: item = "something"
2020-12-22 13:30:36.621 DEBUG testcase.New Test Case                   - 3: println(item)
something
2020-12-22 13:30:36.634 DEBUG testcase.New Test Case                   - 4: if (item in myList)
2020-12-22 13:30:36.641 DEBUG testcase.New Test Case                   - 5: else
2020-12-22 13:30:36.648 DEBUG testcase.New Test Case                   - 1: println("test failed")
test failed
2020-12-22 13:30:36.653 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case


2020-12-22 13:32:51.337 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2020-12-22 13:32:51.342 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2020-12-22 13:32:51.644 INFO  c.k.katalon.core.main.TestCaseExecutor   - (Default) variable = []
2020-12-22 13:32:51.741 DEBUG testcase.New Test Case                   - 1: myList = ["One", "Two", "Three"]
2020-12-22 13:32:51.747 DEBUG testcase.New Test Case                   - 2: item = "Two"
2020-12-22 13:32:51.753 DEBUG testcase.New Test Case                   - 3: println(item)
Two
2020-12-22 13:32:51.763 DEBUG testcase.New Test Case                   - 4: if (item in myList)
2020-12-22 13:32:51.771 DEBUG testcase.New Test Case                   - 1: println("test passed")
test passed
2020-12-22 13:32:51.777 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case

or do an 'up-side-down` validation by using contains() method:

if (myList.contains(item)) {
	println "test passed"
} else {
	println "test failed"
}

Thanks all for your responses - I do appreciate the time and effort spent here.

I went with your suggestion @kazurayam as it keeps the test cases small and straightforward, and it worked exactly as I’d hoped - thank you.

@grylion54, @bionel thanks for your suggestions, I will look at those in more detail when I have more time as they look useful for other things too.

Cheers :slight_smile: