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.
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
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++) { … }:
@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
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()
}