Reading text of the currently selected value from a dropdown list
My first attempt was to use
.getAttribute which has worked for other fields.
String branchLocation = WebUI.getAttribute(findTestObject('Dropdown Location'),'value') returns
c6f25c57-47a7-4ae3-b269-a57567faa23f which is a GUID and can't be translated into anything I can work with.
I then tried it with
getText which does work, but brings back all the options available in the dropdown, not just the currently selected one.
String branchLocation = WebUI.getText(findTestObject('Dropdown Location'))
Location #1 Location #2 Location #3 Location #4 Etc
This is better, but is clearly more information than I want. All I want is the first entry. With that in mind, the
String is turned into a
List with a
split on the CRLF that exists at the end of each line. This gives one entry for each index of the
String allBranchLocations = WebUI.getText(findTestObject('Dropdown Location')) List allBranchesList=allBranchLocations.split("\\r?\\n") //Remove CRLF from each dropdown entry String branchName=allBranchesList
With that little conversion,
branchName contains the first item from the dropdown, which is the default value. This can now be used for my comparison. This may not be the best way to get the first item, or perhaps the most reliable, but it works situation and is easier than some of the other solutions I saw offered up.
Other articles of interest:
- Parsing Strings in Katalon – Split, Substring and Readlines
- Using Sets to fill in Form Details with Katalon Studio
- Comparing Strings Not in the Same Order
- Determine if a checkbox has been checked/selected
- Adding Feature Flags to Test Cases
- Waiting on elements to appear using While Loops
- Simple wildcard searches for pattern matching
- What’s in a Method?
- Passing Katalon Objects with Parameters to Custom Keywords
- Changing the scope of a variable to be available within a Method