Automation

Create a Dynamic Object at Runtime

I'm not quite at the point to need to make an object outside of the Object Repository, but I've seen reference to it multiple times and wanted to put together a simple example because who knows when it might come up. There have been several comments about keeping the size of the Object Repository small so it's better to programmatically create a one-off object rather than commit it to the project.

Katalon Studio allows creating objects during runtime through the TestObject library.

import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.testobject.ConditionType

The object is created by giving it a name and associating a property to it. In the very simple example below, "xpath" is set with the location of a tab on the page.

TestObject is created with the name "dynamicObject"

The "dynamicObject" is given an xpath value that equals the contents of the xpath String

Once the object has been created, it can then be clicked.

The main work is done through this command:

TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', ConditionType.EQUALS, xpath, true)

xpathOfObject="//a[contains(text(),'Contacts')]"
TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', ConditionType.EQUALS, xpathOfObject, true)

WebUI.click(dynamicObject)

There is one important thing to note, since this object is not part of the project Object Repository, the call to manipulate it is slightly different. Note there is no, findTestObject, or the path of the object as part of the command. If the object were part of the project, the command would look like this:

WebUI.click(findTestObject('Project/Customer Profile/Tabs/tab-Contacts'))

The same would be true if this object were passed to a Custom Keyword:

WebUiCommonHelper.findWebElement(objectReference,5)
vs

WebUiCommonHelper.findWebElement(findTestObject(objectReference,5))

Again, I don't know that I have a use case for this scenario, but others have brought it up, and Katalon fully supports it. A dynamic object is quite an easy thing to create and the only real change is how the object is referenced by telling Katalon not to look in the Object Repository, but "locally" if you will.

Other articles of interest:

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:

Recent Comments

  • How To Disable the Quicken Registration Prompt (21)
    • 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...
    • Roslyn Chamberlain: Can I stop the countdown in quicken 2001 says only 8 sessions left. and what will happen after?
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting
  • Simple wildcard searches for pattern matching (2)
    • Don Pedro: For that scenario it seems .contains would be your choice. For example, variable.contains(‘amazo n.com’) to see if the url had amazon.com. In that case, www.amazon.com, forum.amazon.com, retail.amazon.com would all...
    • Jony: Hi, How can I use a wild card to assert a URL is the one I want. I just want to verify the domain ==expected but not anything after it. Tried * but not working and only works when I have full URL.
  • Create a Dynamic Object at Runtime (2)
    • Saish: How to add shadow root parent to this runtime object..
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !