Katalon Studio

Get the xpath of an object in the Object Repository using findPropertyValue(‘xpath’)

In order to write a Custom Keyword to sum any column within a table, I need to know the number of rows in that table. I already have a Custom Keyword to count objects using the WebDriver, but is it possible to use the same method without having to define and pass the xpath separately?

The answer is, yes, you can get the xpath of an object that exists in the Object Repository using:

findTestObject and findPropertyValue('xpath')

This property can be read and then passed to a Custom Keyword such as countRowsPerPage. The String will look like:
"//div[@id='byMonth']/div/table/tbody/tr" as though you defined it manually.

To get things started, I have an object in the Object Repository called, Home/table-ytd-totals, which accepts Row and Column as variables to locate the cell in a table. This object will be used as part of the Test Case and is defined as:

String katalonObject="Home/table-ytd-totals"

Using that reference, I want to create an Object variable so I can get the properties, in this case the xpath, for that table. Since this object is a table, I pass the parameters of Row and Column to make it complete definition.

myPredefinedObject = findTestObject(katalonObject, [('row') : 1, ('column') : 1])

I can now use findPropertyValue('xpath') to get the properties.

log.logWarning("xpath= " + myPredefinedObject.findPropertyValue('xpath'))

And there it is, the full path of whatever has been defined for my table. For my case, the property would be returned as:

//div[@id='byMonth']/div/table/tbody/tr[1]/td[1]

In order to correctly count the rows for my table, I need to remove the trailing TD references and only keep the TR portion. That can be done with a ReplaceAll.

xpath=myPredefinedObject.findPropertyValue('xpath').toString().replaceAll('tr\\[1\\]/td\\[1\\]','tr')
* Note the escape characters to remove the brackets \\[ and \\]

I now have:

//div[@id='byMonth']/div/table/tbody/tr

Which can be passed to my countRowsPerPage method as:

int rowsInTable=CustomKeywords.'tools.commonCode.countRowsPerPage'(xpath)

The rowsInTable variable is then used as the counter in the loop, so I can sum the column. This is passed as a parameter, along with the name of the table from the Object Repository and the column I wish to sum:

int siteColumnTotal=sumColumnTotal(katalonObject, 3, rowsInTable)

I might a little overly excited by this! I can define a single object in the Object Repository and not redefine the xpath each time I need to use the WebDriver. With this in place, I can sum the column of any table on the site. This will eliminate a lot of repetitive code and make maintenance a whole lot easier. No more one-off xpath references inside the code.

The whole code block looks like this:

//Define variables that reference the table objects
String katalonObject="Home/table-ytd-totals"
myPredefinedObject = findTestObject(katalonObject, [('row') : 1, ('column') : 1])
xpath=myPredefinedObject.findPropertyValue('xpath').toString().replaceAll('tr\\[1\\]/td\\[1\\]','tr')

//Count the number of Rows in the table, then sum the column
int rowsInTable=CustomKeywords.'tools.commonCode.countRowsPerPage'(xpath)
int siteColumnTotal=sumColumnTotal(katalonObject, 3, rowsInTable)


def sumColumnTotal(String objectName, int columnToSum, int tableRows){
    /* Sum the column of a table
     * @param objectName - The Object Repository reference to the table
     * @columnToSum - The column to perform the sum on
     * @tableRows - The number of rows in a table
     * @return - the sum of the column
     */
    KeywordLogger log = new KeywordLogger()
    int columnTotal=0
    log.logWarning('Rows in the table: ' + tableRows)
    for (int loop = 1; loop <=tableRows; loop++) {
        int tempText=WebUI.getText(findTestObject(objectName, [('row') : loop, ('column') : columnToSum])).replaceAll("[^0-9-]","").toInteger()
        if (tempText==''){
            tempText=0
        }
        log.logWarning('Value from the table: ' + tempText)
        columnTotal=columnTotal+tempText
    }
    log.logWarning('Total from the site is: ' + columnTotal)
    return columnTotal
}

sumColumnTotal on Github

Other articles of interest:

A Custom Keyword to Verify a List of Product Categories

We recently implemented a change to the site that displays a different list of product categories based on the division a user is associated with. For example, if a User1 is associated with Division1, they see List1. User2 in Division 2 sees List2. User1 can’t see List 2 and vice versa. Since it’s important this list is correct for the user, it seemed a worthwhile to set it up as a test.

While putting the code together, I noted a couple of things:

– The table name may be different, but the list is always in a table
– The list is always 10 items
– The product names are always in the same order
– The list appears in 5 different places

Based on that, I decided to set up the code as a Custom Keyword so I could check the list regardless of where it showed up. Once the test was on the right page, a single call would check the list and then proceed with other tasks.

The basic structure of the Keyword needed the following:

– Read the list of products from a List
– Determine what division the user is in
– Compare the “division list” to the list read from the page
– Mark the test in error if the list doesn’t match

In order to make this Keyword as dynamic as possible, the object name would be passed as a String. The division the user is in would also be passed as a String.

The Keyword would then determine the correct list, read the 10 items from the table it was passed, then compare the two categories.

For the code below, two parameters are accepted,  the division the user is associated with and the Object Repository name.

Two lists are created representing the ‘Categories’ the user should see.

A blank ‘categoryList’ is created that will take on the category values of the users division.

A loop is created to read the category from the table using the name of the Object Repository item, which has Row and Column as parameters set up since it’s a table.

If the first row of the table equals the first item in the List, the next item is read and compared.

If row and List item don’t match, write out an error. The test could also be marked as Failed.

Within the Test Case itself, a variable is assigned the name of the Object Repository item using:

String katalonObject="productCategoryTableName"

The Keyword is called by the following command where the Division is the first parameter and the OR Object is the second.

verifyProductCategory('List1', katalonObject)

def verifyProductCategory(String divisionName, objectName){
    /* Confirm the category is correct for the user division
     * @param divisionName - The division to check, should be List1 or List2
     * @return - does not return a value
     */
    KeywordLogger log = new KeywordLogger()
    List division1Categories=['','Pink Hearts', 'Yellow Moon', 'Orange Stars', 'Green Clovers', 'Blue Diamonds', 'Purple Horseshoes', 'Red Balloons', 'Green Trees', 'Rainbows', 'Blue Moons']
    List division2Categories=['','One Fish', 'Two Fish', 'Red Fish', 'Blue Fish', 'Black Fish', 'Clever Fish', 'Old Fish', New Fish', 'Green Eggs and Ham', 'Fox in Sox', 
    List categoryList=[]
    if (divisionName=='List1'){
        categoryList=division1Categories
    } else {
        categoryList=division2Categories
    }
    for (int loop = 1; loop <=10; loop++) {
        String tempText=WebUI.getText(findTestObject(objectName, [('row') : loop, ('column') : 1]))
        if (tempText!=categoryList[loop]){
            log.logError('ERROR: Category is incorrect. The category should be ' + categoryList[loop])
        }
        log.logWarning('Category: ' + tempText)
    }
}

verifyProductCategory on Github

  • It should also be noted that both Lists start with a null,”, so that the row number and List item number will match. This makes row[1] equal List[1]

Other articles of interest:

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

Other articles of interest:

To markFailedAndStop or Not To markFailedAndStop?

Based on the criteria of the original tests I set up several months ago, if an object was missing, that was a critical error and needed to be flagged. Since we were making heavy adjustments to the code base, anything missing needed to be checked. Things have changed quite a bit, so if an object is missing, should the situation be handled within an IF statement  or does a missing object represent a much larger problem and the test has to be flagged as an Error?

This continues on from the idea of checking for the existence of an object without throwing an error. Using a Custom Keyword is was possible to suppress the error and not fill the log with False Positives.

Taking that a step further, it seems we could check if an object exists and continue on with the test if needed or exit out of the test completely.

Using the example from before, let’s say we want to count the number of Tasks a user has. If a user doesn’t have any Tasks to display, that’s not a critical error. They may not have made any. We could handle that within the IF statement and not try to count something that doesn’t exist.

Conversely, what if we tried to create a Task, but the New Task button was missing. Now we have a problem. We want to perform an action and piece of functionality is missing. And if the New Task button is missing, attempting to run code to fill in a series of input fields, then confirm it was saved won’t work and will lead to multiple errors. It would be better to mark the test as failed when we first notice the New Task button is missing.

We could handle both of those conditions within a single Custom Keyword. We can check for our object and if it’s a critical object, stop the test on failure. We can add a “critical” flag and handle both.

Our previous "verifyObjectPresent" method would be extended to create the following:

@Keyword
//Function to determine if an item exists on the page using WebUiCommonHelper.findWebElement
boolean verifyObjectPresent(String objectReference, boolean critical) {
    try {
        WebUiCommonHelper.findWebElement(findTestObject(objectReference),5)
        return true;
    } catch (Exception e) {
        if (critical==true){
            log.logWarning("The object with the name, " + objectReference + " was not found. Execution Halted.")
            KeywordUtil.markFailedAndStop("ERROR: The object with the name, " + objectReference + " was not found. Exiting Test.")
        } else {
            log.logWarning("The object with the name, " + objectReference + " was not found.")
            return false;
        }
    }
}

We are now accepting “boolean critical” as a parameter. If the object is missing and this is true, we note the missing object and execute, “markFailedAndStop.” If it’s not a critical object, the name of the object is noted, but execution continues. Control then returns to the Test Case to handle a non-critical missing object.

A call to the Custom Keyword would look like the following:

    boolean elementVisible=CustomKeywords.'commonCode.verifyObjects.verifyObjectPresent'('Blog/text-blogTitle', false)
    if (elementVisible==true){
        blogTitle = WebUI.getText(findTestObject('Blog/text-blogTitle'))
        log.logWarning('Blog Title: ' + blogTitle)
    }
    log.logWarning("Test Complete")

If the blog title exists, get the text of that object. If it doesn’t, take the appropriate action based on the critical parameter. If it’s a critical object, mark the tests as failed and exit.

I like this approach because it removes a lot of redundant check, so I plan to make adjustments to replace my standard “verify” based calls with this method. I can now decide how to handle a missing object within a single method and if it’s a real problem, stop the code.

https://github.com/DonPedroQA/qajamboree/blob/master/Katalon/verifyObjectPresent.groovy

Other articles of interest:

Checking for the presence of an object without throwing an error

One of my goals has to been to make my tests less noisy and not generate errors in the log unless it’s really necessary. For example, if an object is missing from the page, that may or may not be an error. If a tab is missing, the user may not meet the criteria to make it display.

When checking for the existence of an object using the WebUI.verifyElementVisible() keyword, a lot of additional logging that can be misleading. For example, this code block will execute as expected, but generates 2 entries for the Failed log.

    @Keyword
    //Function to determine if an item exists on the page
    boolean verifyObjectVisible(String objectReference) {
        try {
            WebUI.verifyElementPresent(findTestObject(objectReference),5)
            return true;
        } catch (Exception e) {
            log.logWarning("The object with the name, " + objectReference + " was not found. Exiting Test.")
            return false;
        }
    }

There is a cleaner way to check for an object using the WebUiCommonHelper.findWebElement command, which is part of the,

import com.kms.katalon.core.webui.common.WebUiCommonHelper library and performs the same function. I like it better because it allows for better handling of errors and logging.

Below is a Custom Keyword that wraps the use of the findWebElement statement within a Try/Catch block to determine if the object exists. The benefit is suppressing an error if the object doesn’t exit.

    @Keyword
    //Function to determine if an item exists on the page using WebUiCommonHelper.findWebElement
    //This does not throw an exception error to the log so it looks cleaner
    boolean verifyObjectPresent(String objectReference) {
        try {
            WebUiCommonHelper.findWebElement(findTestObject(objectReference),5)
            return true;
        } catch (Exception e) {
            return false;
        }
    }

A call to the Custom Keyword would then look like:

    //Check if there are existing notes on the page and count how many
    boolean elementVisible=CustomKeywords.'tools.commonCode.verifyObjectPresent'(katalonObject)
    if (elementVisible==true){
        originalNumberOfTasks=CustomKeywords.'tools.commonCode.countRowsPerPage'(xpath)
    } else {
        log.logWarning('Customer has no Tasks available at this time')
    }

This gives control within the code whether or not to display an error. In looking at the code above, if no Tasks are displayed on the page, a notification message is display, not an error. The test hasn’t failed and there is no problem with the site, a user may not have created an tasks which is why they don’t exit.

 

The verifyElementPresent or verifyElementVisible will perform the same task, but will log multiple errors if an element isn’t found, even when wrapped in a Try/Catch block.

Other articles of interest:

Recent Comments

  • How To Disable the Quicken Registration Prompt (26)
    • Denise Defoor: I have a Quicken SE Version 6 .. My computer crashed a few weeks ago and I finally got it up and running. Now Quicken is telling me I must registered it. What? I have had this program for 20 years. How can I disable the...
    • Greg: For me, holding the *LEFT* CTL + Shift then clicking Online, One Step Update worked. I originally tried holding the right CTL + Shift, and it didn’t work. I’m using Quicken 2006, so I don’t know if it will work...
    • Joe SR>: My monthly income is deposited into my Credit Union account. I use debit whenever possible. I write checks manually and mail them. I use Quicken 2012 off-line only. I have entered all my money and investment accounts. I...
    • Prtet: Never say never….every time I swear I will never use Quicken again, I discover that there are still no viable alternatives. Amazing that there is no decent personal finance software.
    • Joe D.: Holding CTRL + Shift keys and selecting On Line | One Step Update from the main menu worked for my Quicken 2004. I’m grateful that you wrote a synopsis (“Simply put, …”) just beneath the link to the blog...
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
  • Parsing Strings in Katalon – Split, Substring and Readlines (4)
    • Anjana: Hey thanks for the response It is quite strange but initializing variable for delimiter and it worked String splitFormat = ‘-‘ WebUI.println(var_CampaignDura tion.toString().split(splitFor mat)[0])...
    • Don Pedro: I’m not really sure what the difference is, but I don’t think the “-“ in the split is the same as the one in the date. String var_CampaignDuration = “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Anjana: Hi, I tried the same thing to split the string, but does not work for me, Could you plz point out whats wrong Variable var_CampaignDuration has value “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Ellen: Thanks for sharing!! I like your contributions to Katalon topics.
  • How to Block games by Title and Tag on Steam (2)
    • Marcus861: I made an acc just to say: Same bro
    • JACK: Thanks, same just wanted to block anime games in my discover
  • Create a new and random UUID for an API call (1)
    • Anonymous: Hi ! I found this blog few weeks ago and it’s really helping me out with my tests! Thanks a lot for the Katalon tips and tricks ! Keep up the good work 🙂
  • Working with Dates and Date Formatting in Katalon Studio (6)
    • Ajoo: Thank you for the details. How do i remove leading “0” from dates. i.e. while formatting i receive 04/21/2019, but i need 4/21/2019. (same applies for date)