• Selenium Video Tutorials

Selenium with C# Tutorial



Selenium can be used with multiple languages like Java, Python, JavaScript, Ruby, C#, and so on. Selenium is used extensively for web automation testing. Selenium is an open-source and a portable automated software testing tool for testing web applications.

It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently. C# is another language binding for Selenium.

There are no major differences between the Java and C# language bindings in Selenium. The main difference lies in the language only. Also, there is a difference in the Change log of both of these languages.

In Java, we say use the Webdriver, WebElement but in C# we call it IWebdriver, IWebElement. So the convention which is followed in C# is that, while declaring an interface, the letter ‘I’ is added prior to the name of the Interface.

In Page Factory using Java, the element is identified using the @FindBy annotations, however, while using the Page Factory in C#, it is known as an attribute and denoted by [FindBy] is used to locate elements.

Setup Selenium with C# and Launch a Browser

Step 1 − Install the C# code editor called Visual Studio from the link − https://visualstudio.microsoft.com/downloads/.

Using this editor, we can start working on a C# project to start our test automation.

To get a more detailed view on how to set up the Visual Studio, refer to the link − https://www.tutorialspoint.com/ebook/.

Step 2 − Create a new project, by either right clicking on an existing project, and click on the Add option then click on the New Project, or if no project is opened, click on the File menu, then select the New Project option.

Selenium Csharp Tutorial 1

Step 3 − Select the option Console Application, then click on the Next button.

Selenium Csharp Tutorial 2

Step 4 − Enter a project name, say SeleniumTest, then click on the Create button.

Selenium Csharp Tutorial 3

Step 5 − The newly created project - SeleniumTest should be visible.

Selenium Csharp Tutorial 4

Step 6 − Right click on the newly created project and select the option Manage NuGet Packages.

Selenium Csharp Tutorial 5

Step 7 − Enter selenium in the search box at the top right, all the packages based on selenium search should display. Click on the Selenium.WebDriver and click on the Add Packages button. Install all the Selenium related packages.

Selenium Csharp Tutorial 6

Step 8 − Once done, Selenium.Webdriver successfully added a message to display at the top of the Visual Studio.

Selenium Csharp Tutorial 7

Step 9 − Restart the Visual Studio. After it is launched again, all the installed packages should be displayed under the NuGet folder in the Solution Explorer.

Selenium Csharp Tutorial 8

Step 10 − Write a code to obtain the page title - Selenium Practice - Modal Dialogs of the below page.

Selenium Csharp Tutorial 9

Example

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args) {
	  
         // Initiate Webdriver
         IWebDriver driver = new ChromeDriver();

         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

         // launch the application
         driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/modal-dialogs.php");

         // get the page title
         String pageTitle = driver.Title;
         Console.WriteLine("Page title is: " + pageTitle);
      }
   }
}

Output

Page title is: Selenium Practice - Modal Dialogs

In the above example, we had launched an application and obtained its page title in the console with the message - Page title is: Selenium Practice - Modal Dialogs.

Launch Browser and Quit Driver with Selenium C#

We can launch the browser and open an application using the driver.Navigate().GoToUrl method, and finally quit the browser with the Quit() method.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args){
	  
         // Initiate Webdriver
         IWebDriver driver = new ChromeDriver();

         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

         // launch the application
         driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/check-box.php");

         // quitting browser
         driver.Quit();
      }
   }
}

In the above example, we had first launched an application in the Chrome browser then quit the browser.

Identify Element and Check Its Functionality Using Selenium C#

Once we navigate to a webpage, we have to interact with the web elements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.

For this, our first job is to identify the element. There are some locators available in Selenium for this purpose, they are id, class, class name, name, link text, partial link text, tagname, css, and xpath. These locators need to be used with the FindElement() method.

For example, driver.FindElement(By.Id("id")) will locate the first web element with the given id attribute value. In case there is no element with the matching value of the id attribute, NoSuchElementException should be thrown.

Let us see the html code of the radio button beside the Impressive label highlighted in the below image −

Selenium Csharp Tutorial 10
<input class="form-check-input" type="radio" name="tab" 
   value="igotthree" onclick="show3();">

Let us click on that radio button after which we would get the text You have checked Impressive on the web page.

Selenium Csharp Tutorial 11

Example

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args){
	  
         // Initiate the Webdriver
         IWebDriver driver = new ChromeDriver();
         
         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
         
         // launch an application
         driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
         
         // identify a radio button then click on radio button	
         IWebElement r = driver.FindElement
            (By.XPath("/html/body/main/div/div/div[2]/form/div[3]/input"));
         r.Click();

         // identify text after clicking radio button
         IWebElement txt = driver.FindElement(By.Id("check1"));
         
         // obtain text
         String text = txt.Text;
         Console.WriteLine("Text is: " + text);
         
         // quitting browser
         driver.Quit();
      }
   }
}

Output

Text is: You have checked Impressive

In the above example, we had obtained the text after clicking the radio button beside the label Impressive with the message in the console - You have checked Impressive.

This concludes our comprehensive take on the tutorial on Selenium - C# Tutorial. We’ve started with describing how to set up Selenium with C# and launch a browser, and how to identify an element and check its functionality using Selenium C#.

This equips you with in-depth knowledge of the Selenium - C# Tutorial. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements