Automate DropDown List in Selenium C#

Today in this tutorial we are going to discuss how to handle the drop-down list in Selenium C#. DropDown List is an element when we click on it, it shows us the many values out of which we can select only one.

Automation of DropDown List in Selenium C#

In order to handle the DropDown List, we will use the class “SelectElement”. This class provides lots of methods and properties for dealing with the DropDown List. We are going to use three commonly used methods for handling the DropDown List.

Common Methods of SelectElement Class

SelectByIndex

Selects the value based on the index supplied.

SelectByValue

Selects the value based on the “Value” attribute.

SelectByText

It does selection based on the visible Text, whatever text you’re seeing at the UI this method will take that as an argument for the selection.

So in our visual studio, we will add one more directory in TestScript called as HandleDropDown, inside this we will create a class called DropDownList which will be a public class with [TestClass] attribute. Inside this class, we will make a public method.

[TestClass]
public class DropDownList
{
[TestMethod]
public void TestList()
{
NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());
LinkHelper.ClickLink(By.LinkText("File a Bug"));
TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername());
TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword());
ButtonHelper.ClickButton(By.Id("log_in"));
LinkHelper.ClickLink(By.LinkText("testng"));
IWebElement element= ObjectRepository.Driver.FindElement(By.Id("bug_severity"));
SelectElement select= new SelectElement(element);
select.SelectByIndex(2);
select.SelectByValue("normal");
select.SelectByText("blocker");
}
}
Explanation of code:

Here we are using NavigationHelper for going on the webpage. All the lines are the same as we have done in the previous tutorial Example of other elements like giving the username password and clicking on the button etc.

For reaching the desired page where we have a DropDown List, we are using LinkHelper and the link text is “testng”.

Once we are on the desired page again we need to locate this particular element with FindElement() method and by using that we will perform the action, so ObjectRepository.Driver.FindElement(By.Id("bug_ severity")); and in brackets give the id of the DropDown List so its id is “bug_ severity” this will gonna return us IWebElement element.

And now we are going to use this element in SelectElement class so SelectElement select= new SelectElement (element); and in brackets here we need to specify the element so this particular element will select the element & this is the class that handles the DropDown List.

If we go to the class definition of SelectElement as you can see here it has a lot of methods and properties for handling the DropDown List.

First of all, we are using select.SelectByIndex here we have supplied 2, next is select.SelectByValue so here we have to specify the value of “value” attribute so we have supplied “normal” & Select.SelectByText here we have to supply the visible text so let’s take “blocker” in it.

Put the breakpoint on IWebElement element= ObjectRepository.Driver.FindElement(By.Id("bug_severity")); this statement and build the solution and run the script in debug mode.

Output: First it has hit the breakpoint if we do a step over so it is able to identify the DropDown List, then we will create the object of the SelectElement class. Currently, the value inside the DropDown is “Enhancement” so if we do a step over then we can see it has selected the value “Major” which is on the 2nd index position.

The index will start with 0. After that, it has selected the “normal” because it is the value of value attribute “normal” for normal visible text

& again if we do a step over it will select the “blocker”.

So in this manner, we can use these three methods to select the value from inside the DropDown List.

Some Properties of SelectElement Class

Now moving on there are some other properties & methods inside the SelectElement class.

Options:

The Options property will help us to get the all values that are present inside the DropDown List.

SelectedOption:

This property helps us to get the current Selected element.

For example- let us suppose in my DropDown List I have a total of 7 elements so the Options property will retrieve all 7 values for us and out of 7 if any 1 is currently selected so the SelectedOptions property will retrieve that value for us.

[TestClass] 
public class DropDownList 
{ 
[TestMethod] 
public void TestList() 
{ NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite()); 
LinkHelper.ClickLink(By.LinkText("File a Bug")); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername()); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword()); ButtonHelper.ClickButton(By.Id("log_in")); LinkHelper.ClickLink(By.LinkText("testng")); 
IWebElement element= ObjectRepository.Driver.FindElement(By.Id("bug_severity")); SelectElement select= new SelectElement(element); select.SelectByIndex(2); 
select.SelectByValue("normal"); select.SelectByText("blocker"); 
Console.WriteLine("SelectedValue:(0)",select.SelectedOptions.text);
IList<IWebElement> list=select.Options;
foreach(IWebElement ele in list)
{
Console.WriteLine("Value:(0),Text:(1)"),ele.GetAttribute("value"),ele.Text)
}
} 
}

If we look at the Options property so it will return us the IList of IWebElement. Again we build the solution and run the script in debug mode.

Output: so it has hit the breakpoint and we do a stepover, so it will identify the element DropDown List then it will create an object for SelectElement class. First, it will do the selection based on the index, then based on the value attribute then by visual text. If we select this code select.SelectedOption.text

so currently, the selected element is blocker so as you can see here currently it is showing the SelectedOption as “blocker”. Again if we do a stepover it is giving items of the list as you can see here the size of the list is 7, inside the list, we have web elements. Now we are going to iterate all the list elements for getting the values.

So if we copy this line of code ele.GetAttribute("value") & paste it on the immediate window so it shows the “blocker”. we will just click on Continue. If we look at the output of the test so it will show the value and the text. So for DropDown also we will make a Helper Class. Inside the ComponentHelper we will add one more class and named it as ComboBoxHelper.

public class ComboBoxHelper
{
private static SelectElement select;
public static void SelectElement(By locator,int index)
{
select= new SelectElement(GenericHelper.GetElement(locator));
select.SelectByIndex(index);
}
public static void SelectElement(By locator,string visibletext)
{
select= new SelectElement(GenericHelper.GetElement(locator));
select.SelectByValue(visibletext);
}
public static IList<string>GetAttribute(By locator)
{
select= new SelectElement(GenericHelper.GetElement(locator));
return select.Options select(x)=>x.Text).ToList();
}
}
Explanation of code:

The first method will do the selection based on the index for this we have written public static void SelectElement(By locator,int index) here the first argument will be By locator using this we will identify the elements in the DropDown in the UI and the second one will be int index it is the index of the element which we want to select.

And here we are going to select= new SelectElement and as we know that the GenericHelper.GetElement will be going to return the web element, so we will directly call it with GetElement(locator).  Then write select.SelectByIndex(index).

The second method will select the value based on the visual text but here the second argument will be a string. So the first method is overloaded here. public static void SelectElement(By locator,string visibletext).

We will use the same piece of code just replace the SelectByValue instead of SelectByIndex method and its parameter will be (visibletext);

We will create one more method which retrieves all values that are present in the DropDown List so public static IList and the IList will contain the string GetAllItem and inside this, we need to pass the locator and here we will use select= new SelectElement (GenericHelper.GetElement(locator));

and here for returning the list by using the LinQ we might research for all the particular operations on Google. So return select.Options.select() & inside the brackets, we will use the LinQ expression so ((x)=> x.text).ToList()); this is a LinQ expression.

[TestClass] 
public class DropDownList 
{ 
[TestMethod] 
public void TestList() 
{ NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite()); 
LinkHelper.ClickLink(By.LinkText("File a Bug")); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername()); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword()); ButtonHelper.ClickButton(By.Id("log_in")); LinkHelper.ClickLink(By.LinkText("testng")); 
//IWebElement element= ObjectRepository.Driver.FindElement(By.Id("bug_severity")); //SelectElement select= new SelectElement(element); //select.SelectByIndex(2); 
//select.SelectByValue("normal"); //select.SelectByText("blocker"); //Console.WriteLine("SelectedValue:(0)",select.SelectedOptions.text); 
//IList list=select.Options; 
//foreach(IWebElement ele in list) 
//{ 
//Console.WriteLine("Value:(0),Text:(1)"),ele.GetAttribute("value"),ele.Text) 
//} 
ComboBoxHelper.SelectElement(By.Id("bug_severity"),2);
ComBoxHelper.SelectElement(By.Id("bug_severity"),"blocker");
foreach(string str in ComboBoxHelper.GetAllItem(By.Id("bug_severity")))
 {
 Console.WriteLine("Text:(0)",str);
 }
} 
}

Now inside our ComboBoxHelper class we will comment out some piece of code and in order to select based on index we will use ComboBoxHelper.SelectElement(By.Id("bug_severity"),2); again for select based on the visible text this is a “blocker” and after that foreach loop for all the items present inside the list foreach ComboBoxHelper.GetAllItems(By.Id("bug_severity")) the locator for this will be string str, and we are going to display that so Console.WriteLine("Text:(0)",str);

So again we will put a breakpoint and build the solution. And run the script in debug mode.

Output: First it has hit the Debug point so currently Enhancement is selected inside the DropDown List

when we do a stepover it will select the item based on the index than based on visible text and after that, it will give us all the items present inside the DropDown List, and one by one it will print them.

If we look at the output of our test so we can see here all the elements present inside the DropDown List.

Inside this particular method, we are using the LinQ expression for certain things and after that, we are converting them into a list.

In this manner, we can handle the DropDown List in the Selenium C#.