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:

Leave a Reply

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

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 !