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

Posted by: admin September 19, 2023 No Comments
Selenium with C#

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#.
Visual studio code (https://code.visualstudio.com/download)
Dotnet sdk (https://dotnet.microsoft.com/download/dotnet-core)
Visual C++ Redistributable for Visual Studio 2015 (In case of geckodriver) (https://www.microsoft.com/en-in/download//details.aspx?id=48145)
Driver(Geckodriver, chromedriver)
Steps of Installation.

Step 1: Create a Project Folder

Open any drive and create a folder named “demoProjects” (you can use a different name if you prefer).

Step 2: Open the Folder in VS Code

Launch Visual Studio Code.
Click on the “File” tab in the menu bar.
Select “Open Folder” from the options list.
Navigate to the “demoProjects” folder and select it.

Step 3: Add Visual Studio Code Extensions

You’ll need to add two extensions: “C#” and “NuGet Package Manager.”
Click on the extension button in VS Code.
In the input box, type “C#” and select the topmost option from the results.
Click “Install” and wait for the installation to complete.
Follow the same steps to add the “NuGet Package Manager” extension.

Step 4: Create a New Folder and Terminal

Inside the “demoProjects” folder, create a new folder (e.g., “FirstTest”).
Open the Terminal from the “View” menu in the menu bar.
Navigate to the “FirstTest” directory using the command “cd FirstTest.”

Step 5: Generate Required Files

In the terminal, enter the following command to auto-generate the necessary files for running tests:

javascriptCopy code
dotnet new console

Step 6: Add Selenium Package

Click on “View” in the menu bar.
Select “Command Palette.”
Enter “NuGet package manager” into the input box.
Choose “Nuget package manager: Add package” from the result options.
In the input box that appears, type “Selenium” and press Enter.
Select “Selenium Webdriver” from the list. Choose a version compatible with your .NET SDK version.
A pop-up will ask for permission to fix unresolved dependencies. Click “Restore.”

Step 7: Write Selenium C# Test Script

csharpCopy code

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.Navigate().GoToUrl(“https://www.google.com”);
driver.FindElement(By.Name(“q”)).SendKeys(“Fleek It Solutions” + Keys.Return);
Thread.Sleep(2000);
driver.Quit();
}
}
}

Note: Adjust the path string according to your geckodriver and firefox.exe.

Step 8: Run Your Tests

To run your tests, use the debugger or the terminal.
Trigger the following command:

arduinoCopy code
dotnet run

This command will execute your tests, launching Firefox.

Step 9: Review Test Results

If there are no errors, the tests will run successfully, and Firefox will close. You’ll see a success message.
If there are errors, you’ll see error messages with the line numbers where errors occurred.

Also Read : SELENIUM WEBDRIVER AND C#: WEB APPLICATION TESTING MADE EASY

Step 10: Celebrate Your Success!

You’ve set up Selenium with C# in Visual Studio Code successfully. Happy testing!

Leave a Reply