I wrote a sample Test Case that touch the “Dynamic Dropdown” page, and look at the HTML elements as “pulldowns” in it.
import org.openqa.selenium.WebElement
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
class MyDataStructure {
String type
Map<String, List<String>> literals
MyDataStructure(String type, literals) {
this.type = type
this.literals = literals
}
String getIdOne() {
return type + "-one"
}
String getIdTwo() {
return type + "-two"
}
Set<String> keySet() {
return literals.keySet()
}
List<String> get(String key) {
return literals.get(key)
}
}
def textFixture = new MyDataStructure("text", ["Beverages":["Coffee","Coke"], "Snacks": ["Chips", "Cookies"]])
def jsonFixture = new MyDataStructure("json", ["Beverages":["Coffee","Coke"], "Snacks": ["Chips", "Cookies"]])
def dbFixture = new MyDataStructure("db", ["Beverages":["Coffee","Coke"], "Snacks": ["Chips", "Cookies"]])
String url = "https://css-tricks.com/examples/DynamicDropdown/"
WebUI.openBrowser("")
WebUI.navigateToUrl(url)
boolean result
result = verifyDynamicPulldown(textFixture)
result = verifyDynamicPulldown(jsonFixture)
result = verifyDynamicPulldown(dbFixture)
WebUI.closeBrowser()
Boolean verifyDynamicPulldown(MyDataStructure mds, FailureHandling flowControl = , FailureHandling.CONTINUE_ON_FAILURE) {
// verify the first pulldown
WebUI.verifyElementPresent(makeTestObject("//select[@id='${mds.getIdOne()}']"), 10, flowControl)
Set<String> keysOne = mds.keySet()
List<WebElement> optsOne =
WebUI.findWebElements(makeTestObject("//select[@id='${mds.getIdOne()}']/option[not(@selected)]"), 10)
optsOne.each { option ->
assert option.getText() in keysOne
}
// iterate over the options of the 1st pulldown and do the following for each,
// - choose one option of the 1st pulldown,
// - verify the first pulldown if the value is found in the MyDataStructure instance
// - click the 1st pulldown, wait for response
// - verify the second pulldown if the value is found in the MyDataStructure instance
return true
}
TestObject makeTestObject(String xpath) {
TestObject tObj = new TestObject(xpath)
tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
return tObj
}
This sample is incomplete still. It checks the 1st pulldown, but does not check the 2nd pulldown — to be implemented.
I tried to make my code as concise as possible. The core portion is the following lines:
...
def textFixture = new MyDataStructure("text", ["Beverages":["Coffee","Coke"], "Snacks": ["Chips", "Cookies"]])
...
result = verifyDynamicPulldown(textFixture)
...
Is this code too much complicated?
---- Yes, it is complexed. But the Dynamic Dropdown page deserves this level of complexity. I needed Groovy language constructs for modularization: class
, function
, Map<String, List<String>>
to make the code as concise as possible. I could not find a way to solve this problem in Katalon’s “Manual mode” where I can only use a linear sequence of calls to Keywords.
The literal: ["Beverages":["Coffee","Coke"], "Snacks": ["Chips", "Cookies"]]
is important for my sample code. It is an object literal in Groovy, similar to JSON.
If you want to externalize the data, then you would want to create a JSON file, and your test reads the JSON file for itself. Katalon’s “Data Driven testing” approach does not help dealing with JSON files.