Selenium with C#

Selenium with C#

Selenium is popular open source test automation solution. It supports a lot of programming languages, a few being java, csharp, ruby, perl etc. Although there are large number of test automation tools available in the market, many commercial and open source. But Selenium remains popular across the global market. The following graph depicts that


Selenium with Java is one of the most popular combinations, the other programming language combination with Selenium is gaining momentum as the roles of developers and testers are merging creating new job description of being a SDET. Selenium with c# has gained quite a popularity and risen in demand in the job market. And if you are a .net enthusiast, then it is a great time to know this tool to increase your market worth and grow in your career.

Setting the System

C# is a popular .net language using which a lot of applications are developed. Its syntax is close to that of the Java programming language, and the IDE Visual studio is most effective for this language. To set up our system to run our very first test in Selenium using C#, we will require the following set of tools

a.       Visual Studio Community Edition -

b.      The Selenium webdriver package from the NuGet package manager from the visual studio environment-

c.       The Chromedriver package from the NuGet package manager from the visual studio environment-

d.      The application –, to create and run our very first selenium c# test on

e.      The NUnit NuGet package -

Scenario to be Tested

We will be performing the following steps to generate our very first test for selenium with C#. Let us first understand the manual test case scenario. The scenario we are picking is of the Login Logout from the UAT application, using the credentials  for user name –, and password – “USER123”. The steps are –

a.       Open application using url –

b.      Click on the my account link

c.       Type the username in the email address text box

d.      Type the password in the password text box

e.      Click the sign in button

f.        Verify “My Account Information” text is available on page.

g.       Click on the Log Off link

h.      Click on the continue button

These are the steps along with verification for the login logout scenario from the application to be tested.

Creating Test Case in Visual Studio

We create a console project in the visual studio, and then we add the NuGet packages for selenium webdriver, chrome webdriver and nunit in the environment.


Once these are installed, we create a class and add the following code in there

using System;

using System.Text;

using NUnit.Framework;

using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;


namespace ProjectSelenium5ElementsLearning.SeleniumPrograms.Day_10



        public class LoginLogoutTest


            private IWebDriver driver;

            private StringBuilder verificationErrors;

            private string baseURL;

            private bool acceptNextAlert = true;



            public void SetupTest()


                driver = new FirefoxDriver();

                baseURL = "";

                verificationErrors = new StringBuilder();




        public void TheLoginLogoutTest()


            driver.Navigate().GoToUrl(baseURL + "/demosite/");

            driver.FindElement(By.LinkText("My Account")).Click();







            if (driver.PageSource.Contains("My Account Information")) {

                driver.FindElement(By.LinkText("Log Off")).Click();


                Assert.True(true, "User login Success");




                Assert.Fail("Login not successful");



        private bool IsElementPresent(By by)





                return true;


            catch (NoSuchElementException)


                return false;





            public void TeardownTest()






                catch (Exception)


                    // Ignore errors if unable to close the browser


                Assert.AreEqual("", verificationErrors.ToString());




The above program will execute the login logout test on the application using the chrome browser.  To understand and know more about the course of Selenium with C# which we offer, please refer to this url -


No comments

Leave a reply

Your email is never published nor shared. Required fields are marked *