Using Selenium with C# and BDD (Behavior-Driven Development) is a common and powerful approach for creating readable, maintainable, and automated UI tests. The typical stack includes:

Common Tech Stack

ToolPurpose
Selenium WebDriverAutomate browser interactions
SpecFlowBDD-style test writing in Gherkin
NUnit / xUnitTest runners
MSTestOptional test framework alternative
ChromeDriverBrowser automation backend

Install via NuGet:

Use the NuGet Package Manager Console:

Install-Package Selenium.WebDriver Install-Package Selenium.WebDriver.ChromeDriver Install-Package SpecFlow Install-Package SpecFlow.NUnit Install-Package NUnit Install-Package NUnit3TestAdapter

2. Gherkin Feature File (Login.feature)

Feature: Login functionality Scenario: Successful login with valid credentials Given I navigate to the login page When I enter valid credentials And I click the login button Then I should see the homepage

3. Step Definitions (LoginSteps.cs)

[Binding] public class LoginSteps { private IWebDriver driver; [BeforeScenario] public void Setup() { driver = new ChromeDriver(); } [Given(@"I navigate to the login page")] public void GivenINavigateToTheLoginPage() { driver.Navigate().GoToUrl("https://example.com/login"); } [When(@"I enter valid credentials")] public void WhenIEnterValidCredentials() { driver.FindElement(By.Id("username")).SendKeys("testuser"); driver.FindElement(By.Id("password")).SendKeys("testpass"); } [When(@"I click the login button")] public void WhenIClickTheLoginButton() { driver.FindElement(By.Id("login")).Click(); } [Then(@"I should see the homepage")] public void ThenIShouldSeeTheHomepage() { Assert.IsTrue(driver.FindElement(By.Id("logout")).Displayed); } [AfterScenario] public void TearDown() { driver.Quit(); } }

4. Running the Tests

  • Use Test Explorer in Visual Studio to run your BDD tests.

  • Reports can be added via plugins like SpecFlow+ LivingDoc.