Setting up a repeatable Search Method in Katalon Studio

Another one of my project goals was to extend functionality within tests by allowing a single task to be repeated multiple times for different criteria. Search is a good example.

In the original incarnation, my search test would look for one item, confirm it was returned and that was the end. Getting a result back was good enough to say the functionality was working. But the actual search function could look for multiple criteria and I was only looking for one. Could that code be extended to be more dynamic without adding a slew of bloat? Yes it can.

For my scenario, I want to search for a item by the state it’s located in, by the city it’s located in, or by it’s numerical code. For example, I can locate Warehouse 13 by searching for it by Arizona, or by AZ or by 13. All 3 criteria can be handled in one method.

The code below gets the job done. First, it will search for the given criteria. It will then read the text of the returned result and determine if the search criteria is contained within that returned text.

As an example, if I search for “Tallahassee”, that word needs to be in the name of the Warehouse.

If I search for AZ, that needs to be listed in the location.

If I search for 333, that number needs to be in the Warehouse description.
Since I am searching for known commodities, if I don’t get my expected result back, something is wrong. There will always be a result for Tallahassee. There will always be a result for 333. And there will always be a result for AZ.

Since I know the outcome, I can pass my search string and my result as parameters to confirm those are the results. Anything else means something is wrong.

This hard coding could be reduced by using a file or sheet, but that’s for another iteration. This still produces 9 more search tests and is easy to manage. If these pass, I’m quite confident the search function is in a working state.

def searchForWarehouse(String warehouseSearchCriteria, String nameOfWarehouse){
    /* Enter a Warehouse and confirm the Warehouse exists
     * @param warehouseSearchCriteria, search criteria of the Warehouse to look for
     * @param nameOfWarehouse, the text that should be returned for Warehouse details
     * @return that the Warehouse was found, otherwise an error
     */
    KeywordLogger log = new KeywordLogger()
    //Enter search criteria
    WebUI.setText(findTestObject('Project/Search Warehouse/input-Search Warehouse'), warehouseSearchCriteria)
    WebUI.delay(2)
    //Confirm there are results
    int returnedResults=WebUI.getText(findTestObject('Project/Search Warehouse/text-Warehouse Search - Results Found')).replaceAll("[^0-9]","").toInteger()
    if (returnedResults==0) {
        log.logWarning('ERROR: No results were returned. No Warehouse matches the search criteria.')
        log.logWarning('ERROR: The Warehouse ' + warehouseSearchCriteria + ' is not valid')
    } else {
    //Does the Warehouse result contain the expected criteria?
        String WarehouseName=WebUI.getText(findTestObject('Project/Search Warehouse/link-Name of Returned Warehouse'))
        log.logWarning('Warehouse Name= ' + WarehouseName)
        if (WarehouseName.contains(nameOfWarehouse)==true){
            log.logWarning('SUCCESS: The expected Warehouse Name was returned')
        } else {
            log.logError('ERROR: The expected Warehouse Name was not returned')
            KeywordUtil.markFailed('ERROR: The expected Warehouse Name was not returned')
        }
    }
}


//Look for several Warehouse locations and verify results are returned
//Search by City Name
searchForWarehouse('ukiah', 'Project Warehouse #111 UKIAH, CA')
searchForWarehouse('tallahassee', 'Project Warehouse #222 TALLAHASSEE, FL')
searchForWarehouse('worcester', 'Project Warehouse #333 WORCESTER, MA')

//Search by Warehouse Number
searchForWarehouse('646', 'Project Warehouse #646 W MILWAUKEE, WI')
searchForWarehouse('997','Project Warehouse #997 BILOXI, MS')
searchForWarehouse('999','This Warehouse does not exist')

//Search by State
searchForWarehouse('NY','NY')
searchForWarehouse('AZ','AZ')
searchForWarehouse('TX','TX')

searchForWarehouse on Github

Thanks for reading you majestic sausage.

Author Signature for Posts

1 thought on “Setting up a repeatable Search Method in Katalon Studio

Leave a Reply to RJ Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.