Simple wildcard searches for pattern matching

For a search test I was working on, I needed to verify what was returned for the name of a warehouse location. Normally the .contains command would be used, but for this test, I needed to check for multiple criteria. I need to check the warehouse is assigned to a certain coast and contains certain city values. The warehouse could be East or West with several city names listed afterward in any given order. For example, the warehouse location could look like:

String branchName="Warehouse East #585 Palm Beach, FL Charlottesville, VA Nampa, ID Charlottesville, VA"
String branchName="Warehouse West Los Angeles, CA, San Francisco, CA, San Diego, CA"

I want to verify "Warehouse East" is part of the text and I want to verify "Palm Beach" is in the text. Since these two strings are not next to each other and the city could appear anywhere on the line the .contains will not work in this instance.

However, the .matches command can be used which supports the wildcards, * and ?. The usage is the same as we see for filenames. For example *image* matches all filenames with the word image such as image, images, fileimage. The only difference is we have to "escape" the wildcard with the use of .*

With that in mind we can make the following command verify our chosen text strings exist in the returned result.

println(branchName.matches("Warehouse East .*Palm Beach.*"))

To use the single wildcard .? we could write the line as:

println(branchName.matches(".?arehouse .?ast .*Palm Beach.*"))

This ignores the case for the W and E, so Warehouse East is the same as warehouse east

It is also possible to use a more "regex" style and write the above line as:

println(branchName ==~/.?arehouse .?ast .*Plam Beach.*/)

This falls into the brute force category of pattern matching, but I have pretty simple needs and taking the time to write a custom parser isn't necessary and ins't in the cards.

Other articles of interest:

Some new tools to tackle the job in 2019

With 2019 practically upon us, I have some new tools to help with project management, task management, along with code and document organization.

Since there is plenty of manual testing to be done, I've brought in Pagico to help manage projects and 2Do, to help with task management and test management. I've got MWeb for note taking, DevonThink Office to organize and manage all my notes, Coderunner to help write code pieces and SnippetsLab to store code pieces I'm working on.

With Pagico, I can create lists, link documents, and group together the information I need for a given project. For example, I can enter due dates, store the links to Requirements and JIRA tickets, create reminders, track open tickets, and link to PDF files. In many respects it's my replacement for Freeter. I've tracked two projects so far with great success.

2Do is for task management, both for personal and project items. It's easy to create a list, set reminders, and even make a test plan without having to resort to Excel. To be honest, I've made one too many checklists using a spreadsheets and it's a terrible experience. 2Do is much easier to organize and structure.

DevonThink Office Pro is the command center of note taking and storage. It can create notes in a variety of formats as well as store documents like .doc, .xls, .pdf and pretty much everything else. Documents and topics can be tagged with keywords and linked together. Web pages can be imported and stored. To be honest, it's feature set is kind of massive. If you need to store information for a topic, this is the tool to handle it.

Coderunner 3 is a very nice tool for working on small code projects, whether they be Groovy, Java, Python or Shell Scripts. It's a very nice editor that keeps things simply and tidy. You can try out a new code idea in an IDE that takes a second to load rather than waiting for all the libraries and plugins to initialize for something like IntelliJ. It's very quick and nimble and I'm quite taken with it.

MWeb is great note taking and markdown tool. It runs very quickly, supports multiple themes as well as making your own, organizes documents into folders and allows multiple tabs. Documents can be exported in a variety of formats such as HTML, Rich Text, ePub, PDF, and Doc. DevonThink can also be used to track and index whatever notes you create.

You wouldn't ask a carpenter to make a fine table using nothing but a hacksaw and a screwdriver. I'm coming to the table with a variety of tools to get the job done and track what it took to complete.

Pagico
2Do
DevonThink Office Pro
Coderunner 3
SnippetsLab

Other articles of interest:

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:

Recent Comments

  • Create a Dynamic Object at Runtime (1)
    • 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 !
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...