Skip to main content

_csharp

Example

For this example we will use the dropdown list from the-internet. In this list there a few options to select, one which should be disabled. Let's find this element and assert that it is in fact disabled.

Let's start by including our requisite classes for our test framework (e.g., NUnit.Framework), driving the browser with Selenium (e.g., OpenQA.Selenium, etc.), and start our class off with some setup and teardown methods.

// filename: DisabledElements.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

public class DisabledElements
{
IWebDriver Driver;

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

[TearDown]
public void TearDown()
{
Driver.Quit();
}
// ...

Now to wire up our test.

// filename: DisabledElements.cs
// ...
[Test]
public void ElementDisabled()
{
Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/dropdown");
var Dropdown = new SelectElement(Driver.FindElement(By.Id("dropdown")));
// The SelectedOption we want is selected by default on page load
Assert.False(Dropdown.SelectedOption.Enabled);
}
}

After visiting the page we find the dropdown list with the SelectElement function and store it in a variable. We then use in our assertion, focusing on the currently selected option which is the first one by default on page load (e.g., Dropdown.SelectedOption) and check that it is enabled (e.g., .Enabled). This will return a boolean result. If the element is disabled (e.g., not selectable) then Selenium will return false. So that's what we use in our assertion (e.g., Assert.False).

Expected Behavior

When you save this file and run it (e.g., nunit3-console.exe .\DisabledElements.sln from the command-line) here is what will happen:

  • Open a browser
  • Visit the page
  • Grab the dropdown list
  • Assert that the target element is not enabled
  • Close the browser

Summary

Hopefully this tip has helped make the task of seeing whether an element is enabled or disabled more approachable.

Happy Testing!