Selenium with C# : How to start running Automated Tests

On This Page What is C #?Why is C # used for Automation Testing?June 14, 2026 · 11 min read · Tool Comparison

Selenium with C #: How to get lead Automated Tests

Struggling to get Selenium tests running reliably in C #? Setting up the IDE, configure drivers, selecting the right framework, and ensuring cross-browser compatibility can be challenging for any squad. This hap because Selenium requires proper integration with Visual Studio, each browser driver has its own setup nuances, and frameworks differ in structure and conventions.

Need help put up Selenium with C #?

Connect with automation experts who can lead you through driver frame-up, framework constellation, and troubleshooting.

This usher walks through everything involve to write and run Selenium examination in C #. From setting up Ocular Studio to configuring ChromeDriver and GeckoDriver, choosing the right testing framework, and scarper tests that work reliably.

What is C #?

C # (also referred to as C Sharp) is a modern, object-oriented, and type-safe scheduling speech. It enable developers to make many types of secure and robust applications that run in the .NET ecosystem. C # has its roots in the C family of languages and will be immediately conversant to C, C++, Java, and JavaScript programmers.

Why is C # used for Automation Testing?

C # is utilise for testing because it is a modern, statically-typed, object-oriented programming words. Its syntax is easygoing to read and understand, making it a full pick for writing tests that need to be maintained over time. It provides a comprehensive set of feature for creating automated exam.

Additionally, C # has a rich set of libraries and frameworks, such as, that can be used to write tests for a across-the-board range of applications, from desktop and web applications to games and mobile apps.

Getting Started with Selenium and C #

Before running a tryout with Selenium and C #, we need to finish the basic prerequisites.

  1. Install Visual Studio
  2. Create Selenium C # automation framework .NET Core 5.0 from scratch using Visual Studio
  3. Add Chromedriver to Selenium C # Project using Visual Studio
  4. Add GeckoDriver to Selenium C # Project

Once the setup is complete, the succeeding challenge is ensuring that your Selenium C # tests run faithfully across browsers and environments. BrowserStack ’ s QA specialist can help you optimise frameworks, streamline integrations, and scale automation effectively.

Get Expert QA Guidance Today

Schedule a call with BrowserStack QA specialists to discuss your testing challenges, mechanisation scheme, and puppet integrations. Gain actionable brainstorm tailored to your projects and ensure fast, more authentic package delivery.

What is Visual Studio IDE?

Microsoft Visual Studio is an integrated growing environment (IDE) from Microsoft. It provides rich maturation and testing experience with Intellisense, multiple programming language support, and easygoing debugging.

Using Visual Studio one can easy set up all configurations required for development and testing, write and run tests, and get instant results. This tool is quite easy to set up and use, even for beginners.

Visual Studio Community Version is whole costless. One can download and enjoy the features of Visual Studio IDE seamlessly.

How to Install Visual Studio

Visual Studio IDE is available in both free and paid version. The community version of Ocular Studio is free.

  • Click on Free Download, and the .exefile will be downloaded.
  • Click on the .exe file to begin the Visual Studio Installer.
  • Click on the installer file, thePrivacy and License Termswindow look, click onContinueto start instalment.
  • Optical Studio Installer downloads and prepares Visual Studio for installation.

Note: This is Visual Studio Installer, not the Actual Visual Studio

  • Once the Preparation is complete, theVisual Studio initiation windowappears. Users will hold to choose a set of tools. To build an mechanisation framework one can choose theAsp.net and Web developmentoption. Click onInstallto move.
  • Installation begins and its advancement pct will be present accordingly.

Note: The facility takes some clip depending on the internet speeding.

  • Once the installment is complete,the Restart pop up windowlook. Click onRestart.
  • After a successful restart of the scheme, theVisual Studio setuppop upwill appear. Click onNot now, Maybe later.

Note: Sometimes, after restart the above window doesn ’ t appear. If it doesn ’ t seem automatically just Open Visual Studio from the Windows Start Menu.

  • Choose a Theme for Ocular Studio, and click onStart Visual studio.
  • Optical Studio prepares scope for First Use.

Note:This stage takes some clip to finish.

  • Once the above pace is completed, the Get Started Window open.
  • Optical Studio Installation is complete.

Read More:

Selenium C # Automation Framework using Visual Studio

  • Open Visual Studio from theWindows Start Menu.
  • Click on Create New Project.
  • Search forNUnit Template. From theSearch Results choose C # NUnit Test Project (.NET core).

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Note: Selenium C # labor can be created use MSTest libraries as easily, but Nunit is most wide apply with C # and Selenium.

  • In the Configuration Section, enter the required details:
    Project Name:Any Name (ex: SeleniumCsharp)
    Location:Desired Location (Ex: C: /MyFolder)
    Solution Name:Any Name (Ex: SeleniumCsharp)
    After entering the point, detentNext.
  • In the additional Information section, chooseTarget Framework:.NET 5.0 (Current) Click onCreate.
  • After tickCreate, Visual Studio creates Selenium C # framework with NUnit Test Runner.
  • The default fabric contains dependencies for NUnit and NUnit Test Adapter. One file with .cs extension will be make by nonremittal i.eUnitTest1.cs.
  • Add Selenium Dependencies for the project.To run Selenium tests using C # and NUnit, add Selenium dependencies: - Click on theToolsMenu- Click onNuGet Package Manager
    & # 8211; Click on ManageNuGet Package for Solution
  • Search forSelenium Webdriverand add the package.In theNuGet Package Explorer window: - Click onBrowse,
    & # 8211; In the Search box eccentricSelenium
    & # 8211; ChooseSelenium.Webdriver by Selenium
    & # 8211; On the window to the rightfield, select thecheckboxwhich showsProject Name
    & # 8211; Click onInstall
  • If it prompts for theLicense Acceptance Window, click onAccept.
  • Wait for the installation to finish.
  • Install the Selenium Support Package from NuGet:
    & # 8211; Navigate toNuGet Package Manager(Tools & gt; NuGet Package Manager & gt; Manage NuGet Package for Solution)
    & # 8211; TypeSelenium Support
    & # 8211; Click onSelenium Support from Search Results
    & # 8211; Choose theProject Namecheckbox (the current undertaking)
    & # 8211; Click onInstall
  • Wait for the installation to finish.
  • Once and Selenium Support NuGet packet is installed one should see the dependencies append in the project:

All required dependencies feature be add for a Selenium C # project.

Read More:

In order to execute tests, one must hold the desired browser driver. For illustration, to run examination on Chrome, testers need. To run test on Firefox they need, etc.

Using ChromeDriver in Selenium C # using Visual Studio

  • Navigate toSolution Explorerin Visual Studio.
  • Right click on the Project (ex: SeleniumCsharp)
    Note: Right Click on the Project Name, not on the Solution Name.
  • Click Add.
  • ClickNew Folder.

Name the new leafletdrivers.

  • Click on ChromeDriver & lt; XX.XX.XXX & gt;.
  • A new web page will open up and ask for the quarry Operating System (Windows, in this case).
  • Click on the right pick and ChromeDriver will be downloaded as a Zip file.
  • Extract the zip folder to the hope project placement: the previously created folderdrivers, in this case. To do that, simply correct chink on thedriversfolder in Optical Studio, and optOpen Folder in Explorer.

This will open thedriver folder in Windows File Explorer. Now, the ChromeDriver zip file is downloaded to the needful location, extract the ChromeDriver zip hither.

Alternatively,flop click on zip file & gt; Click on Extract All & gt; Specify booklet locating.

After extraction, thedriversfolder should curbchromedriver.exe.

Using GeckoDriver in Selenium C # using Visual Studio

Note: This footstep is expect to run tests on Firefox.

  • Navigate Gecko driver Download web page:
    & # 8211; Navigate toMozilla Github
    & # 8211; Click onReleases under Downloads
  • Choose the correct platform for GeckoDriver and download.
    & # 8211; In the Releases page, scroll down to theAssets section.
    & # 8211; Choose the desired GeckoDriver for the Operating System being used (Windows, in this case).
  • After completion of Step 2, the zip file containinggeckodriver.exewill be downloaded.
  • Extract the GeckoDriver to thedriverspamphlet created earlier.

Note: Step 4 is similar to Chromedriver download. Simply extract geckodriver.exe and place it in drivers folder.After completing this measure, the driver folder should curb two .exe files namelychromedriver.exe and geckodriver.exe.

Which framework is best for Selenium C #?

NUnit Framework is the better for Selenium C #. NUnit provides a rich set of characteristic for organizing and structuring exam, making it easy to write and maintain automated tests. Additionally, it endorse and parallel trial performance, which can help to importantly reduce the clip taken to run a suite of trial.

Selenium C # Test using NUnit in Visual Studio IDE

In the existing project, there already exists a file make by default nominateUnitTest1.cs. Rename that toSeleniumTest.cs(for easy readability).

Also Read:

Double click on theSeleniumTest.csfile and write a few tests.

In this case, 3 exam are being make.

  • Test 1: Navigates to the and verifies the company logo.
  • Test 2: Verifies for all menu items count on the BrowserStack homepage.
  • Test 3: Navigates to the BrowserStack pricing subdivision and verifies the header.

Source Code: SeleniumTest.cs

//Inside SeleniumTest.cs using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Firefox; using System; expend System.Collections.ObjectModel; apply System.IO; namespace SeleniumCsharp {public stratum Tests {IWebDriver driver; [OneTimeSetUp] public void Setup () {//Below code is to get the drivers folder way dynamically. //You can also specify chromedriver.exe path dircly ex: C: /MyProject/Project/drivers string path = Directory.GetParent (Environment.CurrentDirectory) .Parent.Parent.FullName; //Creates the ChomeDriver object, Executes trial on Google Chrome driver = new ChromeDriver (path+ @ '' \drivers\ ''); //If you want to Execute Tests on Firefox uncomment the below code // Specify Correct localization of geckodriver.exe pamphlet path. Ex: C: /Project/drivers //driver= new FirefoxDriver (way + @ '' \drivers\ '');} [Test] public void verifyLogo () {driver.Navigate () .GoToUrl (`` https: //www.browserstack.com/ ''); Assert.IsTrue (driver.FindElement (By.Id (`` logo '')) .Displayed);} [Test] public void verifyMenuItemcount () {ReadOnlyCollection & lt; IWebElement & gt; menuItem = driver.FindElements (By.XPath (`` //ul [contains (@ class, 'horizontal-list product-menu ')] /li '')); Assert.AreEqual (menuItem.Count, 4);} [Test] public void verifyPricingPage () {driver.Navigate () .GoToUrl (`` https: //browserstack.com/pricing ''); IWebElement contactUsPageHeader = driver.FindElement (By.TagName (`` h1 '')); Assert.IsTrue (contactUsPageHeader.Text.Contains (`` Replace your device lab and VMs with any of these program ''));} [OneTimeTearDown] public emptiness TearDown () {driver.Quit ();}}}

The tests have be created, as seen above.

How to run Selenium C # Tests with Example

  • Open Test Explorer window. Click on Test Menu. Click on Test Explorer. New tab call Test Explorer open up on the left of Optic Studio IDE
  • Click on Run Allin the Test Explorer window
  • Tests get executing.
  • Once all your tests finish executing, the results will be marked as shown below:

Having trouble running Selenium C # tests successfully?

Talk to our specialists to troubleshoot issues and run tests reliably across browser.

How to Execute C # Selenium Tests on Firefox

In the seed codification above underSeleniumTest.cs, ChromeDriver is created in the undermentioned line:

driver = new ChromeDriver (path+ @ '' \drivers\ '');

Now, to run the test onFirefox, one must createFirefoxDriverObject. To do so, justcomment the Chrome Driver code and uncomment the FirefoxDrivercode, as below:

driver= new FirefoxDriver (path + @ '' \drivers\ '');

Note: For object the code is taking a dynamic path. But alternatively, one can directly condition the path to geckodriver.exe.

driver = new FirefoxDriver (@ ' C: /MyProject/Project/driver ');

The remainder of the test executing measure remain the same.

Selenium C # Resources

Why run Selenium C # Tests on BrowserStack Real Device Cloud?

You should run Selenium C # Tests on a real device cloud like for below reasons:

  • Naturalistic Testing Conditions:Real device clouds provide admission to a broad spectrum of device and environments, ensuring examination reflect actual user conditions accurately.
  • Enhanced Security:Maintained with eminent security standards, existent device cloud offer secure, sequester testing surround, minimizing information breach risks.
  • Broad Browser and OS Coverage:Helps identify compatibility issues across various browser and operating systems, enhancing user experience.
  • Performance Insights:Real devices yield veritable execution information essential for optimise application reactivity.
  • Scalability and Accessibility:Facilitates scalable and approachable examination, suitable for distributed squad.
  • CI/CD Integration:Integrates smoothly with CI/CD line for uninterrupted testing and early number detection.
  • Cost-Effectiveness:Although initially more high-priced, it saves on long-term disbursement related to fixes and support.

Conclusion

Now that a Selenium & amp; C # framework has been create employ Visual Studio, it can be enhanced to meet the specific motive of teams and organizations. Bear in nous, however, that tests must be fulfil on real device and browser. Remember that is a major concern for every developer and tester. Every site has to work seamlessly on multiple device-browser-OS combinations. With be utilise to access the internet globally, all software has to be optimise for different configurations, viewports, and screen resolve.

In this province, no emulator or simulator can double. Software needs to be tested on real device so that they can work in real-world lot such as a low battery, incoming calls, weak network strength, and so on. If an in-house lab is not accessible, opt for a that volunteer existent devices.

BrowserStack ’ s offers 3500+ real device and browsers for machine-driven examination. That means exploiter can run tests on multiple real devices and browser by but subscribe up, logging in, and selecting the required combinations.

Tags
70,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free