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]
Thanks for reading you majestic sausage.

Author Signature for Posts

0