Dynamic object Identification using KATALON Studio.
When object location is changing every time then it is not good practice to update the reference of object in Object Repository. For this it is better to identify the object location dynamically by using method like modifyObjectProperty
Below is sample code which will give you idea how to identify the object runtime , how to set the xpath for the object to identify.
In this exmaple we are searching text “free society account software” and then we want to click on societyhive.com link if displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase import static com.kms.katalon.core.testdata.TestDataFactory.findTestData import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject import java.text.BreakIterator import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile import com.kms.katalon.core.model.FailureHandling as FailureHandling import com.kms.katalon.core.testcase.TestCase as TestCase import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory import com.kms.katalon.core.testdata.TestData as TestData import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository import com.kms.katalon.core.testobject.TestObject as TestObject import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords import com.machinepublishers.jbrowserdriver.Alert as Alert import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI import internal.GlobalVariable as GlobalVariable import javafx.scene.control.Alert.AlertType import org.openqa.selenium.Keys as Keys import org.openqa.selenium.WebElement as WebElement import org.openqa.selenium.server.htmlrunner.HTMLSuiteResult.HrefConverter import org.apache.jasper.tagplugins.jstl.core.If import org.apache.poi.hssf.record.PageBreakRecord.Break import org.openqa.selenium.By as By import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory WebUI.openBrowser(null) WebUI.maximizeWindow() WebUI.navigateToUrl('http://google.co.in') WebUI.setText(findTestObject('Object Repository/Page_Google/input_q'), 'free society account software') WebUI.sendKeys(findTestObject('Object Repository/Page_Google/input_q'), Keys.chord(Keys.RETURN)) 'In Object we have added one link now we are modifying that object' 'by setting URL = http://www.societyhive.com/' urlToSearch="http://www.societyhive.com/" l1: for(int j=1;j<=4;j++) { 'incrementing value by 1 in xpath format' 'for each displayed search result by google xpath format is same only the counter under div is changing (dont consider ad links)' String xpathval = "//*[@id="+'"rso"'+"]/div/div/div["+j+"]/div/div/h3/a" 'By using ModiyfyObjectPropery we are setting xpath runtime ' LinkToSearch = WebUI.modifyObjectProperty(findTestObject('Object Repository/resultPage/resultLink'), 'xpath','equals',xpathval, true) 'After setting xpath runtime we now then identifying href value for each link if it is equal to societyhive.com then we are clicking and coming out of the loop' 'by using getAttribute we are identifying the href value' LinkVal=WebUI.getAttribute(LinkToSearch, 'href', FailureHandling.STOP_ON_FAILURE) if(LinkVal==urlToSearch) { WebUI.click(LinkToSearch, FailureHandling.STOP_ON_FAILURE) Break l1 } } WebUI.closeBrowser(FailureHandling.STOP_ON_FAILURE) |
Let us know if more details are required.