We use cookies to ensure that we give you the best experience on our website. By continuing to browse or closing this banner, you acknowledge that you have read and agree to our Privacy Policy and Terms of Service.

Coding Jag - Get The Best News Around Test, DevOps & Automation To Your Inbox, Weekly!

Subscribe Now >

Selenium C# Tutorial

As per, Stack Overflow Annual Developer Survey 2019, C# is one of the most loved programming languages with 67% votes. It is also deemed one of the most popular languages, with 31% votes from all the respondents.

Another detailed survey, State of Testing Report 2020, mentioned that around 75% of the testers spend their time testing web applications. It is only natural to rely on test automation frameworks such as Selenium to reduce the overall turnaround time for test cycles.

Selenium is an open-source test automation framework that is widely used for automated browser testing. Selenium offers compatibility with almost every programming language. This makes it an excellent fit for C# fans to automate their cross browser testing efforts using Selenium with C#.

If you already know how awesome C# is as a programming language, then you can use the chapter links to jump around this Selenium C# Tutorial. However, if you’re wondering whether you should be transit to C# for test automation, let us clarify that for you.

What Makes C# A Great Choice For Test Automation?

C# is a programming language that is widely used for both development and testing purposes. Developed by Microsoft, C# is based on the .NET framework. Currently, the latest version of C# being used is 8.0. Here are a few reasons to choose C# for Selenium Test Automation.

C# Is Object-Oriented Programming Language

Being object-oriented language is one of the reasons behind the tremendous adoption of C#. It allows access to standard OOPS features like encapsulation, polymorphism, and inheritance.

C# Can Be Used To Test On Multiple OS

Apart from the development of web, desktop & mobile apps, C# is also widely used for test automation with the various testing frameworks it provides. With C# programming language, you can perform tests on web applications based on Windows, Android, and iOS platforms.

Increasing Popularity For Selenium Test Automation

C# is gradually getting more traction for being used as a programming language for Selenium test automation. With its compatibility with Selenium WebDriver, many automation testers rely on it for performing automated browser testing.

Visual Studio Makes It Easier To Write Tests

C# is being widely used as a language for Selenium Test Automation because you can easily write your test scripts with Visual Studio, which offers access to features like IntelliSense, debugging, basic refactors, etc. Visual Studio IDE gives faster development and debugging, ultimately boosting the go-to-market launch of your web application.

Page Object Model Makes It Easier To Maintain Code

By using the POM Design pattern along with Selenium C# test scripts, testers can make the maintenance of the test scripts more efficient.

Numerous Automation Frameworks Are Available For Selenium C#

With Selenium C#, there is a wide variety of automation frameworks that can be used for automated browser testing. Each framework has its unique advantages and disadvantages; they are chosen based on their requirement, compatibility, and the kind of solution they’d prefer. These are the most popular Selenium C# frameworks used for test automation.

  • NUnit
  • XUnit
  • Gauge
  • MSTest
  • SpecFlow

Now that you know why C# is an excellent choice for test automation let's see how to use it with Selenium to automate our testing efforts.

Do you know Visual Studio is the best IDE (Integrated Development Environment) to use for Selenium C# test automation? Well, now, you do! In this chapter, we will help you set up Selenium in Visual Studio for automated browser testing of your web-application.

Before we start, it's essential to know that there are several download options available with VS 2019. The non-commercial version is Visual Studio Community, whereas the commercial versions are Visual Studio Professional & Visual Studio Enterprise. For the demonstration in this chapter, we would use the Community Edition of Visual Studio 2019 for Selenium test automation.

Ready to set up Selenium in Visual Studio and run your first Selenium C# example script for automated browser testing of your web application?

Learn More!!

Check out our blog on Setting Up Selenium In Visual Studio.

Do you know using C#, you can develop web-based, develop windows, and console applications? Development is not all you can do with C#. You can also leverage C# for test automation with top frameworks like NUnit, along with language bindings for Selenium.

This chapter will help you run your first test automation script for the Selenium C# framework, i.e., the NUnit testing framework.

Learn More!!

Check this Nunit testing tutorial to run your first test automation script in Selenium C# framework.

While designing test automation scripts, we must consider all the major challenges that might come while performing Selenium test automation. There are certain scenarios where the test script is trying to perform an operation on an element that is not loaded (or not yet visible), and the test ends up failing due to this. This happens as the web elements on the page are loaded dynamically via AJAX (Asynchronous JavaScript and XML) and JavaScript or any other front-end framework.

We have ‘Wait,’ which is one of the most important and widely used commands in Selenium test automation to tackle this issue. It helps us to provide us ample wait or pause time in our script execution. Thereby ensuring that our scripts don’t fail while performing automation testing with Selenium.

In this chapter of our Selenium C# tutorial, we will cover what are Selenium waits, why they are important, & how to implement an Implicit Wait in Selenium C# with examples.

Ready to learn what are waits in Selenium, why they're important, and when to use Implicit waits in Selenium?

Learn More!!

Please check our article on Using Implicit Wait in Selenium.

Wait is an important command used in Selenium test automation for handling dynamic loading of web elements on a web page (or web application). Wait command in Selenium helps to ensure that our web application is less flaky and is more reliable.

In the previous chapter of this Selenium C# tutorial, we discussed the basics of Selenium Wait and implicit wait command.

In this chapter, we will walk you through two more types of Selenium waits in the WebDriver – Explicit wait and Fluent waits.

What is Explicit Wait in Selenium C#

Unlike implicit wait, explicit wait in Selenium will wait for certain conditions to occur. The conditions could be waiting for the presence of the web element, waiting for the element to be clickable, waiting for the element to be visible, etc.

Explicit wait in Selenium is also called smart wait as the wait is not for the maximum time-out. If the condition for the explicit wait is satisfied, the wait condition is exited, and the execution proceeds with the next line of code. Depending on the test scenario, you should choose the best-suited wait condition for an explicit wait.

Unlike implicit wait that applies until the Selenium WebDriver instance (or IWebDriver object) is alive, explicit wait in Selenium works only on the particular web element on which it is set then all the elements on the page. Explicit wait in Selenium can be used in test scenarios where synchronization is needed, i.e., loading of the web page is complete, and you are waiting for the element (under test) to be visible.

The figure shows what happens under the hoods when explicit wait in Selenium is triggered:

Selenium C#

What is Fluent Wait in Selenium C#

Fluent Wait is another Wait variant in Selenium C# that lets you control the maximum amount of time the Selenium WebDriver needs to wait for the defined condition to appear. Fluent Wait functionality in Selenium C# can be achieved by defining the frequency at which the WebDriver checks for the element before it throws ElementNotVisibleException.

One major difference between fluent wait and explicit wait in Selenium test automation is that the polling frequency (.pollingInterval) at which the presence for the web element is checked is controllable in fluent wait in Selenium, whereas it is 250 ms in explicit wait.

If the polling frequency in fluent wait is not set, it defaults to 250 ms. The user also has the flexibility to ignore exceptions that may occur during the polling period using the IgnoreExceptionTypes command. The DefaultWait class in C# is used to configure the timeout and polling interval on the fly.

Now that you know what explicit and fluent wait in Selenium is, you must be interested in learning how to implement them for your test automation scripts in Selenium C#.

Learn More!!

To do that, check out our article Using Explicit and Fluent Wait in Selenium.

Alert windows are pop-up windows that are extensively used across different types of websites or web applications. Alert window alerts the user and expects action from the user post that alert.

Alerts are also shown when the user is seeking permission to perform a certain operation. Alerts are also used to issue warnings to the end-user of the web product. Alerts are triggered via the alert() method in JavaScript. In this chapter, we look at using Selenium test automation for handling alerts in Selenium C#.

Learn More!!

Check out our blog on Handling Alert Windows to learn more.

A common scenario in a website (or web application) is opening up a new browser (or tab) on the click of a button. With this, multiple browser windows can be automated using Selenium test automation.

Once the Selenium WebDriver instance is instantiated, a unique alphanumeric id is assigned to the window. This is called a window handle and is used to identify browser windows. Since the id is unique, it is used by the Selenium WebDriver to switch between different windows (or tabs).

The id is retained till the closure of the Selenium WebDriver session via WebDriver.Quit or manual killing of the process. The SwitchTo() command is used to do a context switch to the intended browser window. This chapter will guide you on window handling in Selenium C#.

Ready to learn how to handle multiple windows in Selenium C# along with some Selenium test automation examples?

Learn More!!

Check out our Handling Multiple Browser Windows Guide here to learn more.

As an automation tester, one of the challenges you might face while writing your Selenium test automation scripts is handling frames or iFrames in Selenium while performing automated browser testing. While frames are used to split-screen vertically or horizontally, iFrames are used to insert content from other sources such as ads into your website.

While writing your Selenium test automation scripts, you need to know how to handle frames and iFrames in Selenium. The switch command in Selenium C# is used for automating scenarios involving frames or iFrames in Selenium.

This chapter aims to show you how to handle nested frames and iFrames in Selenium using SwitchTo() command.

Learn More!!

Check out this guide on Handling Frames & iFrames With Examples to learn more!

While writing Selenium test automation scripts, it is important to make sure that your test scripts are scalable and can keep up with any changes in your web page's UI. But, sadly, it's easier said than done! The goal of automated UI web tests is to verify the functionality of the web page elements. With the ever-changing UI, the web locators also change at times. These frequent changes in the web locators can make the task of code maintenance quite challenging.

We have come across many scenarios where tests failed due to lack of proper maintenance. At times, a change in web locator, when the Selenium test automation scripts are not updated, can cause 'most' of the tests to fail!

The solution to this was simple: restructure the Selenium test automation scripts to make it more modular and minimize code duplication. So, how did we do it? We used a design pattern called the Page Object Model (POM), which helped in restructuring the code, minimizing the overall effort spent in test code maintenance activity.

This chapter aims to give you a detailed look at the Page Object Model and how to implement it to make sure you maintain your Selenium test automation scripts in a better manner.

Ready to learn how to avoid code duplication and improve test maintenance with the Page object model?

Learn More!!

Check out our Page‌ ‌Object‌ ‌Model‌ Tutorial With‌ ‌Examples‌.

Join 400,000+ developers & testers who rely on LambdaTest for web testing