defining Objects using “Matches regex” to locate an id

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()