Adding Custom Keyword for Dropdown verfication

I have a scenario which need to validate the values in dropdown field are in Aplhabetical Order. I could do in Selenium with below reusable method

public static void verifyOptionsInDropdownInAphabeticalOrder(WebElement element) {

Select ele = new Select(element);
List<String> expectedOptions = new ArrayList<>();
List<String> actualOptions = new ArrayList<>();

for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());
expectedOptions.add(option.getText());
}

Collections.sort(actualOptions);
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size());
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray());

}

How can I do the same in Katalon Custom Keyword? I tried adding it but couldn’t find how to pass WebElement in the arg

Thanks got it worked, it would be great if you add this to built-in keyword list in next release of Katalon. This can be used for any dropdown by passing webelement to it. What say?

package reusableComponents
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 com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import java.util.*
import internal.GlobalVariable
import org.openqa.selenium.By
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
import org.testng.Assert
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.common.WebUiCommonHelper
public class VerifyDrodownValues_AlphabeticalOrder {
static WebDriver driver = DriverFactory.getWebDriver()
static WebElement element = null
@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element)
List<String> expectedOptions = new ArrayList<>()
List<String> actualOptions = new ArrayList<>()
for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText())
actualOptions.add(option.getText())
expectedOptions.add(option.getText())
}
Collections.sort(actualOptions)
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size())
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray())
System.out.println("Yes Dropdown values are in Alphabetical order")

}
}

It would be great if you provide a custom keyword for the same.
Here’s the URL https://seleniumbycharan.wordpress.com/
Scenario: Validate values in ‘Categories’ Drodown (in the right side of page) are in Alphabetical Order or not?

Thanks for the response. but my question is how to pass that webelement from the Testcase (Manual mode). In the above code it’s been hardcoded with btn_step or someother. I wanted to pass ‘btn_step’ from the testcase (manual mode)
static WebElement element = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/btn_step’), 20)

Maybe I failed to understand the logic you’re saying, could you elaborate more?

The reason behind it is, I wanted to create a reusable component to verify & validate dropdown values. This can be reused in any page having Dropdown

Sorry I didn’t get it. How can I pass the webelement from the keyword? Can you check my code to suggest the modification required

public class VerifyDrodownValues_AlphabeticalOrder {
static WebDriver driver = DriverFactory.getWebDriver()
static WebElement element
@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element);
List expectedOptions = new ArrayList<>();
List actualOptions = new ArrayList<>();

for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());
expectedOptions.add(option.getText());
}

Collections.sort(actualOptions);
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size());
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray());
}
}

Hi there,

Sorry for the confusions. Based on your scenario, here is my script:

Custom Keyword:

@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element)
List<String> expectedOptions = new ArrayList<>()
List<String> actualOptions = new ArrayList<>()
for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText())
actualOptions.add(option.getText())
expectedOptions.add(option.getText())
}
Collections.sort(actualOptions)
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size())
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray())
}

2. Usage in test case:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('https://seleniumbycharan.wordpress.com/')

WebElement element = WebUiCommonHelper.findWebElement(findTestObject('Object Repository/sl_Category'), 20)
CustomKeywords.'com.example.WebUICustomKeywords.verifyOptionsInDropdownInAphabeticalOrder'(element)

3. Test Object view: http://prnt.sc/f6zyvo

You need to ‘convert’ from Katalon Studio test object into WebElement using that findWebElement if you want to pass WebElement into your case.

Hope these example will not confuse you anymore.

1 Like

Hi there,

The WebElement can be retrieved from web application easily in Katalon Studio using: WebUiCommonHelper.findWebElement() API. In your case, here are modifications of your custom keyword, please be are that ‘btn_step’ is just a sample test object, you can pass any test object you want.

public class VerifyDrodownValues_AlphabeticalOrder {
static WebElement element = WebUiCommonHelper.findWebElement(findTestObject('Object Repository/btn_step'), 20)
@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element);
List<String> expectedOptions = new ArrayList<>();
List<String> actualOptions = new ArrayList<>();

for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());
expectedOptions.add(option.getText());
}

Collections.sort(actualOptions);
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size());
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray());
}
}

Additional example is how to call it within test case:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebUiCommonHelper

WebElement ele = WebUiCommonHelper.findWebElement(findTestObject('Object Repository/btn_step'), 20)
CustomKeywords.'com.example.WebUICustomKeywords.verifyOptionsInDropdownInAphabeticalOrder'(ele)

Thanks

Hi there,

So if you want to pass webelement from the test case dynamically, you can adjust my second solution from that topic a little bit, like this:

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

import com.kms.katalon.core.webui.driver.DriverFactory
WebDriver driver = DriverFactory.getWebDriver()
WebElement ele = driver.findElement(By.id('id'))

'ele’ variable is the dropdown element you need to pass in your custom keyword. It’s similar to how WebElement is used in Selenium.

Thanks

The solution which you’ve suggested has Hardcoded webelement. Isn’t it?
‘TestObject to = findTestObject(‘Object Repository/Page_AppointmentConfirmation/lbl_Comment’)’

I want to pass webelement from the Testcase (Dynamically). So that Custom keyword can be used in any Testcase

Hi there,

This topic has a solution on how to pass the element (WebElement). You can refer on it so that you to how to get WebElement and pass it in custom keyword

I don’t understand this I have tried to copy and paste both of your code and I keep getting errors left and right when I try and save the keyword.

Groovy:unable to resolve class select
or
Groovy:Appparent variable ‘Assert’ was not found in a static scope but…

I’d appreciate if you could explain this a bit more as well. It would help to add some comments.

Sorry, also like B L getting errors, did you test this before you posted? Specifically getting Groovy syntax errors here when I copy and paste into Katalon keywords code…
List&lt;String&gt; expectedOptions = new ArrayList&lt;&gt;()
List&lt;String&gt; actualOptions = new ArrayList&lt;&gt;()

Vinh Nguyen said:

Hi there,

Sorry for the confusions. Based on your scenario, here is my script:

Custom Keyword:

@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element)
List&lt;String&gt; expectedOptions = new ArrayList&lt;&gt;()
List&lt;String&gt; actualOptions = new ArrayList&lt;&gt;()
for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText())
actualOptions.add(option.getText())
expectedOptions.add(option.getText())
}
Collections.sort(actualOptions)
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size())
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray())
}

2. Usage in test case:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('https://seleniumbycharan.wordpress.com/')

WebElement element = WebUiCommonHelper.findWebElement(findTestObject('Object Repository/sl_Category'), 20)
CustomKeywords.'com.example.WebUICustomKeywords.verifyOptionsInDropdownInAphabeticalOrder'(element)

3. Test Object view: http://prnt.sc/f6zyvo

You need to ‘convert’ from Katalon Studio test object into WebElement using that findWebElement if you want to pass WebElement into your case.

Hope these example will not confuse you anymore.

Hari Charan said:

Thanks got it worked, it would be great if you add this to built-in keyword list in next release of Katalon. This can be used for any dropdown by passing webelement to it. What say?

package reusableComponents
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 com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import java.util.*
import internal.GlobalVariable
import org.openqa.selenium.By
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
import org.testng.Assert
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.common.WebUiCommonHelper
public class VerifyDrodownValues_AlphabeticalOrder {
static WebDriver driver = DriverFactory.getWebDriver()
static WebElement element = null
@Keyword
public static void verifyOptionsInDropdownInAphabeticalOrder(element) {
Select ele = new Select(element)
List&lt;String&gt; expectedOptions = new ArrayList&lt;&gt;()
List&lt;String&gt; actualOptions = new ArrayList&lt;&gt;()
for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText())
actualOptions.add(option.getText())
expectedOptions.add(option.getText())
}
Collections.sort(actualOptions)
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size())
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray())
System.out.println("Yes Dropdown values are in Alphabetical order")

}
}

Hi Vinh/ Charan,

You script is working as expected.

But in my case, all the dropdown in my test application has Select as the first option. In this scenario the script fails. So please let me know how to handle this.

Below is the HTML code.

I’m confused, you were able to get the example code to work without modifications? At the very least I had to add <> for the &lt and &gt, also having issues as Katalon 5.8 can’t access this constructor: import org.openqa.selenium.support.ui.Select. Can you please copy your code that worked?

It helped me. Thanks!