Skip to main content

_csharp

TL;DR - Show Me The Code

5-select-from-a-dropdown/code/csharp/DropdownTest.cs
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Collections.Generic;

namespace csharp
{
[TestClass]
public class DropdownTest
{
IWebDriver _driver;

[TestInitialize]
public void SetUp()
{
_driver = new FirefoxDriver();
}

[TestCleanup]
public void TearDown()
{
_driver.Quit();
}

[TestMethod]
public void SelectFromDropdownTheHardWay()
{
_driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/dropdown");
IWebElement Dropdown = _driver.FindElement(By.Id("dropdown"));
IReadOnlyCollection<IWebElement> DropdownOptions = Dropdown.FindElements(By.TagName("option"));
foreach(IWebElement Option in DropdownOptions)
{
if(Option.Text.Equals("Option 1"))
Option.Click();
}
string SelectedOption = "";
foreach (IWebElement Option in DropdownOptions)
{
if (Option.Selected)
SelectedOption = Option.Text;
}
Assert.AreEqual("Option 1", SelectedOption);
}

[TestMethod]
public void SelectFromDropdownTheEasyWay()
{
_driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/dropdown");
SelectElement Dropdown = new SelectElement(_driver.FindElement(By.Id("dropdown")));
Dropdown.SelectByText("Option 1");
Assert.AreEqual("Option 1", Dropdown.SelectedOption.Text);
}

}
}

Code Walkthrough

Importing Libraries, Setup and Teardown

The first few lines are pulling in our requisite classes for our testing framework (e.g., using Microsoft.VisualStudio.TestTools.UnitTesting;), driving the browser with Selenium (e.g., using OpenQA.Selenium;), launching an instance of Firefox (e.g., using OpenQA.Selenium.Firefox;), and Selenium's wait functions (e.g., using OpenQA.Selenium.Support.UI;), and access to C# collections (e.g., System.Collections.Generic).

Next, we'll start our class off with some setup and teardown methods, covered in the following lines.

Example 1

Our first example is SelectFromDropdownTheHardWay. After visiting the example application we find the dropdown list by its ID and store it in a variable. We then find each element in the dropdown list (e.g., each element with an option tag) with FindElements (note the plural).

Grabbing all options with FindElements returns a collection that we iterate over. When the text matches what we want (e.g., "Option 1") we click on it.

We finish the test by performing a check to see that our selection was made correctly. This is done by iterating over the dropdown options collection once more. This time we're getting the text of the item that was selected, storing it in a variable, and making an assertion against it.

While this works, there is a simpler, built-in way to do this with Selenium. Let's give that a go.

Example 2

Our second example is SelectFromDropdownTheEasyWay, and similar to the first example, we are finding the dropdown list by its ID. But instead of iterating over its option elements and clicking based on a conditional we are leveraging a built-in helper function of Selenium. With SelectElement and its SelectBy methods (e.g., SelectByText) we're able to easily choose the item we want.

We then perform our assertion against the text of the currently selected option (e.g., Assert.AreEqual("Option 1", Dropdown.SelectedOption.Text)).

As an aside, in addition to selecting by text you can also select by value.

    Dropdown.SelectByValue("1");

Executing the Test

Before executing the test, we need to make sure the required dependencies are declared on the project file (e.g., csharp.csproj in this case).

Toggle to see the csharp.csproj file.
5-select-from-a-dropdown/code/csharp/csharp.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="Selenium.Support" Version="4.26.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.26.0" />
</ItemGroup>

</Project>

Finally, we can run the test by executing dotnet test from the command-line.