Get your Application Audited for Free.

Please fill in the form below to schedule a call or request an estimate.

How to use Selenium with C# in Visual Studio Code?
single,single-post,postid-17503,single-format-standard,ajax_fade,page_not_loaded,,qode-title-hidden,side_area_uncovered_from_content,qode-content-sidebar-responsive,qode-theme-ver-9.5,wpb-js-composer js-comp-ver-4.11.1,vc_responsive
Selenium with C# in Visual Studio

How to use Selenium with C# in Visual Studio Code?

Selenium is an open-source web automation library. It supports many browsers like Chrome, Firefox, Edge etc and many languages – python, java, C#, javascript etc. Here we will get to know How you can use selenium with C# in Visual studio code.

Prerequisites for selenium with C#.

  1. Visual studio code (
  2. Dotnet sdk (
  3. Visual C++ Redistributable for Visual Studio 2015 (In case of geckodriver) (
  4. Driver(Geckodriver, chromedriver)

Steps of Installation.

        1. Create a Folder with name “demoProjects” in any drive(can create a folder with any name according to you).
          steps of installation
        2. Open VS Code and select the ‘demoProject’ folder that you had created recently.
          1. Open VS Code and Click on ‘File’ Tab from the menu bar.
          2. Click on ‘Open Folder’ from options list.
          3. Navigate to the recently created folder and Select Folder.
        3. Add Visual studio code extensions to get it supported.
          C# and NuGet Package Manager

          1. To add “C#” extensions click on extension button in VS code.
            steps of installation-1
          2. Enter C# in the Input box and select the top most option of C# from results.
          3. Click on install button and wait till it add related packages. It will show a success message about installation.
            steps of installation-2
          4. Follow the same steps from above to add “NuGet Package Manager” extension. Enter Nuget package manager in the input box and install it.
            steps of installation-3
        4. Once the above installation is completed, Create a New Folder under demoProject and name it ‘FirstTest’ or with any name, Then open Terminal from View (menu bar). Go to “FirstTest” directory by “cd FirstTest” and trigger the below command to auto generate the related files which will help to run the tests.
          dotnet new consolesteps of installation-4
        5. Now Add selenium Package to the project. Click on ‘View’ from the menu bar, Select Command Palette option. Enter ‘NuGet package manager’ into the input box.
          1. Select ‘Nuget package manager: Add package’ from result options.
          2. After selecting, An input box will open then enter “Selenium” and Press Enter key.
          3. It will show all package related to selenium from which, select ‘Selenium Webdriver’. Then select version according to your dotnet sdk version.
            steps of installation-5
          4. After selecting version, a pop up will be shown with asking permission to fix unresolved dependencies. Click on the restore button.
        6. Either you can run the same ‘hello world’ program to verify it is configured or you can insert the selenium C# test script.
          1. I am sharing an example of C# tests that will launch google and search ‘Fleek IT Solutions’.
            using OpenQA.Selenium;
            using OpenQA.Selenium.Firefox;
            using System;
            using System.Threading;
            namespace selenium
            class Program
            static void Main(string[] args)
            FirefoxBinary binary = new FirefoxBinary();
            FirefoxOptions options = new FirefoxOptions();//optional
            options.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
            IWebDriver driver = new FirefoxDriver("C:/Users/lenovo/Documents/Webdriver", options);
            driver.FindElement(By.Name("q")).SendKeys("Fleek It solutions"+ Keys.Return);

            Note: You may have to adjust the path string according to your geckodriver and firefox.exe.

        7. To run your tests, either you can use debugger or command from the terminal. To run tests trigger the below command.
          dotnet runHitting the above command will start running your tests, and launch firefox to execute test steps.
          If there is no error, then after executing all steps it will close firefox and print a success message. And if it finds any error then it will show all error messages with the number of line where error took place. Have a look at the following image. I have run tests twice to show Pass and Fail of tests, first time without changes (correct program) and the second time with removing Locator id to show Fail message.
          Passed Installation
          [Passed tests]Failed Installation
          [Failed tests]
[email protected]

Mritunjay is a persistent problem solver, has quality with cutting edge in software testing technologies. He has a clear understanding of test planning, test management, and execution. He has the ability to dive into unfamiliar results and proficient knowledge in automation testing.

No Comments

Post A Comment