Katalon Multiple Dynamic Test Objects

Hello
I have a scenario where I need to send values to the test objects dynamically through a DataFile and based on the values it will select those buttons.

I have iterated through the DataFile using a for loop and I was trying to click on each button based on the dynamic Object’s value which I get from the DataFile.

But the problem is it is clicking on the same button every time, even though it receives the values present in the DataFile in each iteration.

total_rows = findTestData('Element1_QuestionIDS').getRowNumbers()
System.out.println(total_rows)
if (total_rows > 0) {
for (def Rownumb = 1; Rownumb <= total_rows; Rownumb++) {
    String dynamicId = findTestData('Element1_QuestionIDS').getValue('Question_ids', Rownumb)

    String xpath = ('//div["@id=question_id_' + dynamicId) + '"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]'

    KeywordUtil.logInfo(xpath)

    TestObject obj = new TestObject('objectName')

    obj.addProperty('xpath', ConditionType.EQUALS, xpath)

    WebUI.click(obj)
	
}
}

And the data file is Screenshot (487)

This looks odd to me (but I’m not an xpath guru)

Hi guy,

There is a shorten way to use

  1. Create one template test object
  2. Define its xpath like //a[@id =’${yourVariable}’] in the object xpath. (Edit it to match your xpath)
  3. Binding yourVariable with your test data by creating another variable in your TC

Hope it works!

FYI

Hii @Russ_Thomas
Thanks for ur response

Here I was replacing the dynamicID in the XPath with the QuestionID value from the data file.

Here in place of dynamicID the value from the data file gets replaced., and it looks like this
//*[@id=“question_id_8121951820”]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]

The syntax looks wrong. See @loc.nguyen’s response.

What you have is overlapping parentheses:

( ... [ ... ) ... ]

Hii @loc.nguyen

I have tried this way… For the first iteration of for loop it is clicking on the correct button… But from the next iteration onwards even though it is getting the correct id from the DataFile it is clicking on the first button itself.

I have defined a TestObject with an Xpath as you said and did bind the variable with my test data.

cnt = lst.size()

for (def k = 0; k < cnt; k++) {
    WebUI.click(findTestObject('MasterApplication/TestCase_Specific_Objects/Dynamic_objects_spike/Checklist_YES', [('anoop') : lst[k]]))

    WebUI.delay(2)

    System.out.println(lst[k])
}

I am also attaching the logs from the console which show the correct ids of the objects but it is not clicking on those buttons.

2021-08-24 20:15:54.902 DEBUG testcase.Operator_Permessions_dynamicID  - 120: cnt = lst.size()
2021-08-24 20:15:54.906 DEBUG testcase.Operator_Permessions_dynamicID  - 121: for ([k = 0, k < cnt, (k++)])
2021-08-24 20:15:54.908 DEBUG testcase.Operator_Permessions_dynamicID  - 1: click(findTestObject("MasterApplication/TestCase_Specific_Objects/Dynamic_objects_spike/Checklist_YES", ["anoop":lst[k]]))
2021-08-24 20:15:55.889 DEBUG testcase.Operator_Permessions_dynamicID  - 2: delay(2)
2021-08-24 20:15:57.908 DEBUG testcase.Operator_Permessions_dynamicID  - 3: out.println(lst[k])
//div["@id=question_id_8121951820"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]
2021-08-24 20:15:57.911 DEBUG testcase.Operator_Permessions_dynamicID  - 1: click(findTestObject("MasterApplication/TestCase_Specific_Objects/Dynamic_objects_spike/Checklist_YES", ["anoop":lst[k]]))
2021-08-24 20:15:58.998 DEBUG testcase.Operator_Permessions_dynamicID  - 2: delay(2)
2021-08-24 20:16:01.017 DEBUG testcase.Operator_Permessions_dynamicID  - 3: out.println(lst[k])
//div["@id=question_id_5998220928"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]
2021-08-24 20:16:01.018 DEBUG testcase.Operator_Permessions_dynamicID  - 1: click(findTestObject("MasterApplication/TestCase_Specific_Objects/Dynamic_objects_spike/Checklist_YES", ["anoop":lst[k]]))
2021-08-24 20:16:01.805 DEBUG testcase.Operator_Permessions_dynamicID  - 2: delay(2)
2021-08-24 20:16:03.819 DEBUG testcase.Operator_Permessions_dynamicID  - 3: out.println(lst[k])
//div["@id=question_id_2788011304"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]

Here everytime only the first button is getting clicked (//div["@id=question_id_8121951820"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]) and for the other buttons too it is clicking on this button itself.

Hi,

cnt = lst.size()

How many items we have, print it please.

In addition, please help to try to allocate different memory block each time of loop. Try with it first, maybe it cached data

  • declare a variable outer loop type TestObject
  • Set this variable by finding object with your solution
  • Call click on this variable

If still not work:

  1. Check object captured correct or not
  2. Using another keyword, like executeJSScript to simulate click on your captured object
  3. Use JQuery on Chrome Developer tool to test click with your object

Question: I saw click inner the loop, if 1st click successfully, it navigates to other pages or keep at current page, DOM tree change or not?

Hii @loc.nguyen

I have tried to declare the variable and then clearing its value once after its execution, But it is still the same… It is clicking on the first button itself. I have verified the objects which I’m getting and they were correct.

The code i have used is

cnt = lst.size()
System.out.println(cnt)

for(def a=0; a < cnt; a++) {
	String obj_value = lst[a]
	if (obj_value != '') {
		System.out.println(obj_value)
		TestObject obj = new TestObject('objectName')
		obj.addProperty('xpath', ConditionType.EQUALS, obj_value)
		WebUI.click(obj)
		obj_value = ''
	}
}

and also I’m attaching the logs of the above code

2021-08-25 12:08:16.256 DEBUG testcase.Operator_Permessions_dynamicID  - 120: cnt = lst.size()
2021-08-25 12:08:16.257 DEBUG testcase.Operator_Permessions_dynamicID  - 121: out.println(cnt)
3
2021-08-25 12:08:16.258 DEBUG testcase.Operator_Permessions_dynamicID  - 122: for ([a = 0, a < cnt, (a++)])
2021-08-25 12:08:16.258 DEBUG testcase.Operator_Permessions_dynamicID  - 1: obj_value = lst[a]
2021-08-25 12:08:16.262 DEBUG testcase.Operator_Permessions_dynamicID  - 2: if (obj_value != "")
2021-08-25 12:08:16.263 DEBUG testcase.Operator_Permessions_dynamicID  - 1: out.println(obj_value)
//div["@id=question_id_8121951820"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]
2021-08-25 12:08:16.264 DEBUG testcase.Operator_Permessions_dynamicID  - 2: obj = new com.kms.katalon.core.testobject.TestObject(objectName)
2021-08-25 12:08:16.264 DEBUG testcase.Operator_Permessions_dynamicID  - 3: obj.addProperty("xpath", EQUALS, obj_value)
2021-08-25 12:08:16.277 DEBUG testcase.Operator_Permessions_dynamicID  - 4: click(obj)
2021-08-25 12:08:16.665 DEBUG testcase.Operator_Permessions_dynamicID  - 5: obj_value = ""
2021-08-25 12:08:16.665 DEBUG testcase.Operator_Permessions_dynamicID  - 1: obj_value = lst[a]
2021-08-25 12:08:16.666 DEBUG testcase.Operator_Permessions_dynamicID  - 2: if (obj_value != "")
2021-08-25 12:08:16.666 DEBUG testcase.Operator_Permessions_dynamicID  - 1: out.println(obj_value)
//div["@id=question_id_5998220928"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]
2021-08-25 12:08:16.666 DEBUG testcase.Operator_Permessions_dynamicID  - 2: obj = new com.kms.katalon.core.testobject.TestObject(objectName)
2021-08-25 12:08:16.667 DEBUG testcase.Operator_Permessions_dynamicID  - 3: obj.addProperty("xpath", EQUALS, obj_value)
2021-08-25 12:08:16.667 DEBUG testcase.Operator_Permessions_dynamicID  - 4: click(obj)
2021-08-25 12:08:17.132 DEBUG testcase.Operator_Permessions_dynamicID  - 5: obj_value = ""
2021-08-25 12:08:17.132 DEBUG testcase.Operator_Permessions_dynamicID  - 1: obj_value = lst[a]
2021-08-25 12:08:17.133 DEBUG testcase.Operator_Permessions_dynamicID  - 2: if (obj_value != "")
2021-08-25 12:08:17.133 DEBUG testcase.Operator_Permessions_dynamicID  - 1: out.println(obj_value)
//div["@id=question_id_2788011304"]/div[3]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]
2021-08-25 12:08:17.133 DEBUG testcase.Operator_Permessions_dynamicID  - 2: obj = new com.kms.katalon.core.testobject.TestObject(objectName)
2021-08-25 12:08:17.133 DEBUG testcase.Operator_Permessions_dynamicID  - 3: obj.addProperty("xpath", EQUALS, obj_value)
2021-08-25 12:08:17.133 DEBUG testcase.Operator_Permessions_dynamicID  - 4: click(obj)
2021-08-25 12:08:18.572 DEBUG testcase.Operator_Permessions_dynamicID  - 5: obj_value = ""

I will once try using it with another keyword(executeJavascript method) to click on the object.

Sorry for late response

I have just simulated your situation, see video and sample project in this file

Loc Test Project.zip (143.5 KB)

Hope it is useful! Based on that idea you can customize to your testing purposes.

FYI

@srianoop.d

...
2021-08-25 12:08:16.277 ...  - 4: click(obj)
...
2021-08-25 12:08:16.667 ...  - 4: click(obj)
...
2021-08-25 12:08:17.133 ...  - 4: click(obj)
...

This log tells us that your code called WebUI.click(obj) 3 times in a second. You are clicking multiple times very rapidly. Do you do this rapid clicks intentionally?

I don’t know how quickly your target web app can react, but a usual web page is not designed to react to such rapid clicks like a machine gun. It is likely that only the first click is accepted by the target web app, and the 2nd and following clicks are left unhandled.

I think your code should wait, after each single “WebUI.click”, for the page becomes ready to be clicked again. How to make it wait? I do not know. Only you can find it, as It depends how your web page is designed.

2 Likes

Hi @loc.nguyen

Sorry for delayed response.

I have tried the same why as you did in the sample project and i was getting the correct id’s but it is only clicking on the first button for all the subsequent objects too. I have added delay of 20 seconds after clicking on the button to make sure it takes time to click on correct button.

I have also verified the objects which I was getting through the excel file and they were correct and were pointing to the correct buttons.

Can you show the html for those buttons?

Hi @danpoleary yes sure

The HTML Path of the button is -
/html/body/tp-root/tp-layout/div/mat-sidenav-container/mat-sidenav-content/div/tp-module-loader/tp-wpichecklist/div/form/div[2]/div/section/div/div/dl[5]/dd/div/span/mat-form-field/div/div[1]/div/tp-embed-reference/tp-tipochecklistplugin/div/div/form/formly-form/formly-field/formly-dropdown-dev/div/div/div/section/div/div/section/div/div/mat-accordion/mat-expansion-panel/div/div/div/div/div[2]/formly-field/formly-group/formly-field[2]/formly-group/formly-field[2]/each_question_panel/div/div/div[2]/div[1]/formly-multiple-choice/div/div/tp-multiple-choice/div/mat-radio-group/div[1]/mat-radio-button

Put the quotes only around the id string. so instead of:
//div["@id=question_id_8121951820"]
try
//div[@id=“question_id_8121951820”]

1 Like

It worked @danpoleary
Thanks for the response.