How to get the length so that I can loop the elements?

Hello All,

In our Application we have this page wherein the elements are listed as below-

Here, under ‘BID OPTIMIZATION’ section the list of items shown will be dynamic, so in order to use ‘for loop’ and click on view button i need a count/length to specify number of times the loop should run.
I am not able to figure out how to find the length.

Please can anyone guide me to achieve this?
Thanks in advance.

You can not do it in the Manual mode of Test Case editor of Katalon Studio. There is no build-in Keyword for non-programmers to achieve iterating over a list of <md-card> element selected out of your target HTML.

You need to write some lines of Groovy code in the Script mode. If you are ready and willing to write code, you can use findElements(TestObject) method of the
com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords class

See

https://docs.katalon.com/javadoc/com/kms/katalon/core/webui/keyword/WebUiBuiltInKeywords.html#findWebElements(com.kms.katalon.core.testobject.TestObject,%20int)


@yashaswini.sadananda How about trying this:

Maybe you can use the class of “tri-counter-widget blah blah blah” as the XPath.

A loop consisting of three parts is executed as follows:

for (A; B; C)

A - Executed before the enumeration
B - condition to test
C - expression after each enumeration (so, not if B evaluated to false)
So, yes: The .length property of an array is checked at each enumeration if it’s constructed as for(var i=0; i<array.length; i++). For micro-optimisation, it’s efficient to store the length of an array in a temporary variable (see also: What’s the fastest way to loop through an array in JavaScript?).

Equivalent to for (var i=0; i<array.length; i++) { … }:

var i = 0;
while (i < array.length) {

i++;
}

@kazurayam Thanks for the reply.
can you please tell me how to use this method? It is not coming in the Add option of Katalon Studio. I saw the link you shared but is is bit confusing, couldn’t figure out how to use it in the studio.

Have you written a test case to test your target web page? If none, please make one your self, and share the code here. And please share the HTML source code of the page.

Then I would be able to add a few lines of code which iterate over the elements.

@kazurayam Here is the code I have written (it is incomplete)-
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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
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.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil

WebUI.callTestCase(findTestCase(‘Intentwise/Login/Test03_Login_Production’), [:], FailureHandling.STOP_ON_FAILURE)

WebUI.callTestCase(findTestCase(‘Intentwise/NavigateToAccount/Navigate_to_IntentwiseDemo’), [:], FailureHandling.STOP_ON_FAILURE)

WebUI.click(findTestObject(‘Intentwise/Customer Success - Daily Activities/Recommendations/sel_Recommendations’))

String bid_opti = WebUI.getText(findTestObject(‘Intentwise/Customer Success - Daily Activities/Recommendations/bid_optimization’))

KeywordUtil.logInfo(bid_opti)

boolean text1 = WebUI.verifyElementText(findTestObject(‘Intentwise/Customer Success - Daily Activities/Recommendations/bid_optimization’),
‘BID OPTIMIZATION’)

int i = 1

while (text1) {
String new_xpath = (’//div[@id=‘content’]//div[1]//md-card[’ + i) + ‘]//md-card-actions[1]//md-card-icon-actions[1]//button[1]’

TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', com.kms.katalon.core.testobject.ConditionType.EQUALS, 
    new_xpath, true)

WebUI.click(dynamicObject)

KeywordUtil.logInfo(i.toString())

WebUI.click(findTestObject('Intentwise/Customer Success - Daily Activities/Recommendations/sel_Recommendations'))

i = (i + 1)

}

Here is the HTML source code -

I suppose you want somthing like this.

import org.openqa.selenium.By
import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject selectorMdCard = new TestObject()
selectorMdCard.addProperty("xpath", ConditionType.EQUALS, 
	"//div[@id='content']//div[1]//md-card")

// find the list of <md-card> elements
List<WebElement> mdCardList = WebUI.findWebElements(selectorMdCard, 30) 

// iterate over the list
for (mdCard in mdCardList) {
	// find a <button> in each of the <md-card>
	WebElement button = mdCard.findElement(
		By.xpath("md-card-actions[1]//md-card-icon-actions[1]//button[1]"))
               // this xpath is interpreted as relative to the <md-card> element 
	// click button
	button.click()
}

Selenium WebDriver API is here

Or

import org.openqa.selenium.By
import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject selectorMdCard = new TestObject()
selectorMdCard.addProperty("xpath", ConditionType.EQUALS,
	"//div[@id='content']//div[1]//md-card")

// find the list of <md-card> elements
List<WebElement> mdCardList = WebUI.findWebElements(selectorMdCard, 30)
for (int i = 0; i < mdCardList.size(); i++) {
	String new_xpath = "//div[@id='content']//div[1]//md-card[${i}]//md-card-actions[1]//md-card-icon-actions[1]//button[1]"
	TestObject dynamicObject = new TestObject('dynamicObject').addProperty('xpath', ConditionType.EQUALS, new_xpath)
	WebUI.click(dynamicObject)
}

It seems you use // operator in XPath too much, carelessly. That may cause unexpected result. You should use it only when it is appropriate.

Operator Description
/ Path (Children): the child operator (’/’) selects from immediate children of the left-side collection.
// Descendants: the descendant operator (’//’) selects from arbitrary descendants of the left-side collection.

quote from http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc30020_1251/html/xmlb/xmlb31.htm

@kazurayam This worked for me. Thanks alot !

Thanks for pointing out. I will be cautious now on.