Along side the web elements, the drop down commands are the final piece necessary to create our full featured tests.

The following are just a few of the available methods for testing drop down lists. Just as web elements, a css selector by id or xpath can be used. to find the element.

CommandC#Description
SelectByTextIWebelement el = driver.FindElement(By.xpath(“t”)); SelectElement sl= new SelectElement(el); select.SelectByText(“”);Select an option.
SelectByValueIWebelement el= driver.FindElement(By.xpath(“”)); SelectElement sl = new SelectElement(el); select.SelectByValue(“”);Select an option

Select List Option Commands

Select List Unit Test

On the main page, we add two drop down lists that contain the numbers 1 and 2, the selected value will also be 1 or 2. When the two drop down values are selected, a javascript element runs that adds the two values together and stores it in a label.

First modify index to satisfy the above problem statement. The drop downs will be labeled ‘one’ and ‘two’ and the label will have id ‘result’.

Index.cshtml showing two dropdowns a label and a javascript function

Next develop the test to select the two values and assert the result.

Second test verifies drop down values

This test retrieves the down downs and sets their respective values. The label element is then retrieved and compared with an assert statement.

Download the source here.