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?
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<>();
Collections.sort(actualOptions);
System.out.println("Numbers of options present in the dropdown: " + actualOptions.size());
Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray());
}
}
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<>();
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)
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
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
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<String> expectedOptions = new ArrayList<>() List<String> actualOptions = new ArrayList<>()
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?
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")
} }
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.
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 < and >, 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?