defining Objects using “Matches regex” to locate an id

Im am trying to utilize the ‘matches regex’ condition to locate dynamically named objects in my project such as an id ‘checkboxfield-1475-displayEl’

If I use a definition like ‘checkboxfield-\d\d\d\d-displayEl’ ( a full match on https://regex101.com/). The object is not found.

What am I missing here?

 

Thanks,

Mark

I used your example and I am finding objects, however the final filtering within the for loop (matching to a regular expression) fails as all of the objects found have the value of ‘id’ expressed in what appears to be Hexadecimal: {aba5d53e-17f1-41d2-bd85-0fed7673aa79}… is there some further filtering or conditions that must be configured?

Hi Mark,

From duyluong’s solution, the object’s properties must be changed before Katalon Studio start to find it on the web application, so you don’t need to care the EXISTING properties.

To properly apply duyluong’s solution, you need to build a TestObject first. Just assign it to some random object which you have defined in Object Repository:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebElement
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

'Open browser and navigate to Appointment page'
WebUI.openBrowser(GlobalVariable.G_SiteURL)

'Build your TestObject using Contains match condition'
TestObject to = findTestObject('Object Repository/genericObject')
to.addProperty("id", ConditionType.CONTAINS, 'checkboxfield-displayE')

'Find WebElements based on above match condition'
ArrayList<WebElement> wes = WebUiCommonHelper.findWebElements(to , 30)
for(WebElement we: wes){
'Compare with regex id pattern'
if (we.getAttribute('id').matches('checkboxfield-\\d\\d\\d\\d-displayE'))
{
'Do something on found WebElement here'
}
}

WebUI.closeBrowser()

You can simplify building TestObject step by using ONE of below solutions, and then use it in other steps:

1. Edit object’s property to use ‘contains’ or ‘starts-with’ or ‘ends-with’: http://prnt.sc/endp3n
2. Use ‘Modify Object Property’ keyword:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

'Open browser and navigate to Appointment page'
WebUI.openBrowser(GlobalVariable.G_SiteURL)

'Modify object property of the test object in Object Repository'
TestObject to = WebUI.modifyObjectProperty(findTestObject('Object Repository/genericObject'), 'id',
'contains', 'checkboxfield-display', true)

'Find WebElements based on above match condition'
ArrayList<WebElement> wes = WebUiCommonHelper.findWebElements(to , 30)
for(WebElement we: wes){
'Compare with regex id pattern'
if (we.getAttribute('id').matches('checkboxfield-\\d\\d\\d\\d-displayE'))
{
'Do something on found WebElement here'
}
}

WebUI.closeBrowser()

Hello Duyluong,

My issue is that I cannot obtain the object. This object (and everything else in the application apparently ) is I believe, being created using Angular.js so all the objects have dynamically assigned id’s, with the number in the id description changing. I cannot do as you suggest as I never get the ‘Tested Object’, because I do not know its name until after it is created. That is why I was looking to utilize regex to find the correct object id, and get a handle on the object.

Thanks,

Mark

1 Like