Dropdown & Multiple Select Operations

Just like CheckBox & Radio ButtonsDropDown & Multiple Select Operations also works together and almost the same way. To perform any action, the first task is to identify the element group. I am saying it a group, as DropDown /Multiple Select is not a single element. They always have a single name but and they contains one or more than one elements in them. I should rather say more than one option in DropDown and Multiple Select. The only difference between these two is deselecting statement & multiple selections are not allowed on DropDown . Let’s look at the different operations:

--

It is just an ordinary operation like selecting any other type of element on a webpage. You can choose it by ID, Name, Css & Xpath etc. But to perform any action on this element it is required to import ‘importorg.openqa.selenium.support.ui.Select' package and to use it we need to create a new Select Object of class Select.

Select Class in Selenium

Models a SELECT tag, providing helper methods to select and deselect options. Select is a class which is provided by Selenium to perform multiple operations on DropDown object and Multiple Select object. This class can be found under the Selenium’s Support.UI.Select package. As Select is also an ordinary class, so it’s object is also created by a New keyword with regular class creation syntax.


Select  objofSelect = new Select());


It clearly says that Select is asking for a element type object for its constructor. The code will be:

WebElement element = driver.findElement(By.id("Country"));

Select objofSelect = new Select(element);

//Or it can be also written as

Select objofSelect = new Select(driver.findElement(By.id("Country")));

The above code will generate compile time error in Eclipse, as Select() is asking for constructor. Bring the cursor over Select(), Eclipse will populate a suggestion.


Note: Select class only works for elements with <select> tags.

Different Select Commands

we will learn how to deal with DropDown and Multi Select elements. There will be many interesting operations are availbale on these elements. But you may be wondering that how a DropDown looks like in the HTML code. Will use the same example for the refernse of different select commands.
selectByVisibleText
selectByVisibleText(String arg0) : void – It is very easy to choose or select an option given under any dropdowns and multiple selection boxes with selectByVisibleText method. It takes a parameter of String which is one of the Value of Select element and it returns nothing.
Command – oSelect.selectByVisibleText(“text”);
Example – Refer the above Screen shot of YEAR Drop Down*
Code – To select the value 2010
Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));

oSelect.selectByVisibleText("2010");
selectByIndex
selectByIndex(int arg0) : void – It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text.It takes a parameter of int which is the index value of Select element and it returns nothing.
Command – oSelect.selectByIndex(int);
Example – Refer the above Screen shot of YEAR Drop Down*
Code – To select the value 2010 using index.
Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));

oSelect.selectByIndex(4);
Note: Index starts from Zero, so the fifth position value will be at index 4.

selectByValue
selectByValue(String arg0) : void –It is again the same what we have discussed earlier, the only difference in this is that it ask for the value of the option rather the option text or index. It takes a parameter of String which is on of the value of Select element and it returns nothing.
Command – oSelect.selectByValue(“text”);
Example – Refer the above Screen shot of YEAR Drop Down*
Code – To select the value 2014.
Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));

oSelect.selectByValue("2014");
Note: The value of an option and the text of the option may not be always same and there can be a possibility that the value is not assigned to Select webelement. If the value is given in the Select tag then only you can use the selectByValue method.

getOptions
getOptions( ) : List<WebElement> –This gets the all options belonging to the Select tag. It takes no parameter and returns List<WebElements>.
Command – oSelect.getOptions();
Sometimes you may like to count the element in the dropdown and multiple select box, so that you can use the loop on Select element.
Example – Refer the above Screen shot of YEAR Drop Down*
Code – To get the Count of the total elements inside SELECT.
                Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));
                List <WebElement> elementCount = oSelect.getOptions();
                System.out.println(elementCount.size());
All of the above methods work on both Dropdown and Multiple select box.

DeSelect Methods
The way we select different values of DropDown & Multi Select, the same way we can also deselect the values. But the only challenge in these methods are they do not work for DropDown and only work for Multi Select elements.
In case you want to deselect any pre-selected option, that can be done with either deselectAll(), deselectByIndex, deselectByValue and deselectByVisibletext.

deselectAll( ) : void – Clear all selected entries. This is only valid when the SELECT supports multiple selections.
Command – oSelect.deselectAll;
deselectByIndex(int arg0) : void –Deselect the option at the given index.
Command – oSelect.deselectByIndex;
deselectByValue(String arg0) : void –Deselect all options that have a value matching the argument.
Command – oSelect.deselectByValue;
deselectByVisibleText(String arg0) : void – Deselect all options that display text matching the argument.
Command – oSelect.deselectByVisibleText
isMultiple
isMultiple( ) : boolean – This tells whether the SELECT element support multiple selecting options at the same time or not. This accepts nothing by returns boolean value(true/false).
Command – oSelect.isMultiple();
This is done by checking the value of the “multiple” attribute.
Example – Refer the above Screen shot of MULTI SELECT for multiple attribute*


Multi Select Methods
This one also just works on Multiple selection boxes and not on regular List boxes or dropdowns. There is no additional logic behind selecting multiple options of Select element. All you need to do is to fire select commands on multiple elements one by one that’s it.
                Select oSelect = new Select(driver.findElement(By.id(Element_ID)));
                oSelect.selectByIndex(index)
                oSelect.selectByIndex(index)

                // Or can be used as

                oSelect.selectByVisibleText(text)
                oSelect.selectByVisibleText(text)

                // Or can be used as

                oSelect.selectByValue(value)
                oSelect.selectByValue(value)

Comments

Popular posts from this blog

Handling Dynamic Web Tables Using Selenium WebDriver

Verify Specific Position of an Element

Read it out for TESTNG before going for an iterview