Creating an Array from a dropdown list


#1

My skills in these area’s are novice at best. I have been struggling to create a Test Object that defines my lists properly in order to make a selection from it.

In the example i’m trying right now, there appears to be two drop down lists being generated, one in the form of a select list, and the other in the form of an input that is created by a script?

I’m trying to create an array/list that i can read from and then follow up with something like this;

Any help is greatly appreciated.


#2

Assuming that you want two separate lists of elements (one that contains all of the <option> elements under the <select>, and one that contains all of the <li> elements), you will need two Test Objects to identify the individual sets.

1.) First, lets handle the list elements, because the approach can be used for both (however, I will give a better way that’s specific to <select> elements below). Based on your HTML screenshot, the Test Object could look something like:

Note: I’ve excluded the <li> that acts as the “header” (I’m assuming you don’t want to include this…).

Then your test code would look like:

List<WebElement> listElements = WebUiCommonHelper.findWebElements(findTestObject('listItemObject'), 30);

2.) Now lets handle the <select> element. Here’s the Test Object for it, again based on your HTML screenshot:

You could follow the same approach as above to do this, but luckily Selenium has a pretty convenient set of methods that specifically handle <select> elements. So you could also get a list of all options under the select as follows:

WebElement selectElement = WebUiCommonHelper.findWebElement(findTestObject('selectObject'), 30);
Select select = new Select(selectElement);
List<WebElement> options = select.getOptions();

Again, you could skip the Select class altogether if you want, and follow the same approach as for the list items. If you decided to go this route, your Test Object would look something like:

… and your test code would be the same as for the list items.

Hopefully this is helpful!


#3

A little bit helpful so far. I’m out of time now, but i’ll follow up with more of my currently running script and what data i’m trying to view and select.

Currently, trying to create a list from custom test objects was unable to locate the element via the Xpath.


#4

When you get another chance to investigate, a good way to test if an XPath is correct is to execute the path in your browser console while you’re sitting on the page:

If it comes back empty or undefined, there’s quite a few things to look at that may be causing issues.


#5

Oops! Just realized the xpath I shared for the list elements is wrong. It should be:

//ul[@id='TDropdownUL_1']//li[not(@class='Li_Header')]

I’ve updated the screenshot from my original response to reflect this.

Sorry about that!


#6

Fixed the XML path and list building has been accomplished.

I added a loop to print out all the lines as well to make sure it’s grabbing everything right, and looks good. Next steps, onto more selecting things. :slight_smile:


#7

Here’s my solution for those interested;

  • Clicks dropdown to locate li elements
  • uses @Brandon_Hein suggestion to create List using web element and test object
  • Creates web element for selected option (static, random commented out)
  • Clicks the selected option, also by @Brandon_Hein
  • Commented out loop to print the list

WebUI.click(findTestObject(‘Test Object for Dropdown’))
WebUI.delay(1)
List listElements = WebUiCommonHelper.findWebElements(findTestObject(‘Test Object with XML Path’), 30);
WebElement docTypeSelect = listElements.get(0/new Random().nextInt(listElements.size())/);
docTypeSelect.click();
/for(WebElement w : listElements) {
println(w.getText());
}
/


#8

There is no WebElement type in Katalon…

groovy: 44: unable to resolve class WebElement


#9

There is, you just need to import it. Anytime you are going to utilize a class that does not already have an import statement, just type ctrl + shift + o to automatically search for appropriate imports.