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:

Leave a Reply

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

Recent Comments
  • Custom Keywords for Custom Functions (1)
    • Sarah: Thank you for this. It helped me understand these better.
  • 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...
  • 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)