![]() |
Selenium is used to automate the desktop browser testing. It comes up with many components and from them, the major one is the web driver. Web driver is used for writing the code in different programming languages which will target the web elements and then apply the operation on it. We can use C#, Java, Python, and many other languages which can be used to work with it. Table of Content Why is C# useful for Automation Testing?Using C# we easily do automation Testing. It is an open source and due to this different frameworks have support for this language.
Selenium with C#Selenium and C# become a solid combination because C# is one of the chosen languages to work with Selenium due to so many functions to manipulate and test the Websites. In C# we have NUnit Testing which is again a widely used testing framework and Selenium can be easily integrated with it. Due to this, we can run the unit tests for both the backend and frontend simultaneously and that also in parallel. Get the elements using their ID or by their class name or their tag NameC#
1. For DropdownC#
2. For ButtonsC#
Configure Selenium with Visual Studio in C#We will create a Console Application in which we will create a selenium script and we will interact with the website. Step 1: Download and install C# IDE (Visual Studio)
Step 2: Create a New Project in Visual StudioChoose the Create new project from the Menu. ![]() Creating Project Step 3: Select Console Application1. Now from the list choose the Console Application with C# as the Programming Language. ![]() Creating Console Application 2. Our project has been created successfully and now we will install some packages to run the Selenium. Step 4: Installing NuGet PackagesFor creating a selenium application and using selenium methods we have to install 3 packages. 1. Selenium.WebDriver ![]() Selenium.Webdriver 2. Selenium.Support ![]() Selenium.Support 3. Selenium.Webdriver.ChromeDriver This will be used to say to Selenium to choose which browser in our case we are going to use Chrome and thus we are installing this Package. If you want to try any other browser then you have to install those packages accordingly. ![]() Chrome Driver Package Step 5: Installing Chrome DriverFor executing the scripts we will require a Chrome driver which we load as soon as we run the application and all the operations will be executed in that only. We can download the Chrome driver from Here.
![]() Chrome Driver So, now we are good to go with writing the script to automate the browser with Selenium. But before that let’s create one HTML form with which we can interact and fill that form using selenium. Code for HTML FormHTML
Now interacting with this form will help us understand the working and the flow of the selenium application. So we will first get the elements and then on that, we will perform the operations. So, that we can get an idea of both the process. Selenium Test Script using C#C#
In the above code, the FindElement method is used to get the element. We can store these elements in the variable if we want to use them later. In this case, we have directly sent the keys which will be used to write into the field. Later on, we will find the button and then we will click on that. Output of Selenium Test Script in Visual Studio![]() Output ConclusionSo, this was basic of the Selenium with C# in which we have just found the element and then applied some operation. We can also move ahead with this, here we have used ID to get the element. But we can use classname or we can also use Xpath. These are the advanced features of selenium in C# which are generally done after this basic one. |
Reffered: https://www.geeksforgeeks.org
Selenium |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |