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

Then again, I could be wrong.

Author Signature for Posts

0