The importance of automation testing cannot be overstated. Within narrow deadlines and changing customer demands hovering over them, QAs must automate numerous tests, test cases, and projects to meet said deadlines, save time, effort and get error-free results.
Selenium is undoubtedly the most popular tool for test automation. Quite often, Selenium needs to be used with C# and Visual Studio IDE together for a better-automated testing experience.
Using Selenium, C#, and Visual Studio together provides a unique opportunity to create a robust, scalable, and flexible automation framework. This article will discuss how to do exactly that.
What is C#?
C# (also referred to as C Sharp) is a modern, object-oriented, and type-safe programming language. It enables developers to build many types of secure and robust applications that run in the .NET ecosystem. C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers.
Can I use Selenium with C#?
Yes, you can use Selenium with C#. Selenium enables you to write automated tests in C#.
What is Visual Studio IDE?
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It provides rich development and testing experience with Intellisense, multiple programming language support, and easy debugging.
Using Visual Studio one can easily set up all configurations required for development and testing, write and run tests, and get instant results. This tool is quite easy to set up and use, even for beginners.
Visual Studio Community Version is completely free. One can download and enjoy the features of Visual Studio IDE seamlessly.
Table of ContentsBefore running a test with Selenium and C#, we need to complete the basic prerequisites.
Visual Studio IDE is available in both free and paid versions. The community version of Visual Studio is free.
Project Name: Any Name (ex: SeleniumCsharp)
Location: Desired Location (Ex: C:/MyFolder)
Solution Name: Any Name (Ex: SeleniumCsharp)
After entering the details, click Next.
All required dependencies have been added for a Selenium C# project.
In order to execute tests, one must have the desired browser driver. For example, to run tests on Chrome, testers need Chromedriver. To run tests on Firefox they need GeckoDriver, etc.
This will open the driver folder in Windows File Explorer. Now, the ChromeDriver zip file is downloaded to the required location, extract the ChromeDriver zip here.
Alternatively, right click on zip file > Click on Extract All > Specify folder location.
After extraction, the drivers folder should contain chromedriver.exe.
Note: This step is required to run tests on Firefox.
In the existing project, there already exists a file created by default named UnitTest1.cs. Rename that to SeleniumTest.cs (for easy readability).
Double click on the SeleniumTest.cs file and write a few tests.
In this case, 3 tests are being created.
Source Code: SeleniumTest.cs
//Inside SeleniumTest.cs using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Firefox; using System; using System.Collections.ObjectModel; using System.IO; namespace SeleniumCsharp { public class Tests { IWebDriver driver; [OneTimeSetUp] public void Setup() { //Below code is to get the drivers folder path dynamically. //You can also specify chromedriver.exe path dircly ex: C:/MyProject/Project/drivers string path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName; //Creates the ChomeDriver object, Executes tests on Google Chrome driver = new ChromeDriver(path+@"\drivers\"); //If you want to Execute Tests on Firefox uncomment the below code // Specify Correct location of geckodriver.exe folder path. Ex: C:/Project/drivers //driver= new FirefoxDriver(path + @"\drivers\"); } [Test] public void verifyLogo() { driver.Navigate().GoToUrl("https://www.browserstack.com/"); Assert.IsTrue(driver.FindElement(By.Id("logo")).Displayed); } [Test] public void verifyMenuItemcount() { ReadOnlyCollection<IWebElement> menuItem = driver.FindElements(By.XPath("//ul[contains(@class,'horizontal-list product-menu')]/li")); Assert.AreEqual(menuItem.Count, 4); } [Test] public void verifyPricingPage() { driver.Navigate().GoToUrl("https://browserstack.com/pricing"); IWebElement contactUsPageHeader = driver.FindElement(By.TagName("h1")); Assert.IsTrue(contactUsPageHeader.Text.Contains("Replace your device lab and VMs with any of these plans")); } [OneTimeTearDown] public void TearDown() { driver.Quit(); } } }
The tests have been created, as seen above.
In the source code above under SeleniumTest.cs, ChromeDriver is created in the following line:
driver = new ChromeDriver(path+@"\drivers\");
Now, to run the tests on Firefox, one must create FirefoxDriver Object. To do so, simply comment the Chrome Driver code and uncomment the FirefoxDriver code, as below:
driver= new FirefoxDriver(path + @"\drivers\");
Note: For FirefoxDriver object the code is taking a dynamic path. But alternatively, one can directly specify the path to geckodriver.exe.
driver = new FirefoxDriver(@'C:/MyProject/Project/driver');
The rest of the test execution steps remain the same.
Now that a Selenium & C# framework has been created using Visual Studio, it can be enhanced to meet the specific needs of teams and organizations. Bear in mind, however, that Selenium WebDriver tests must be executed on real devices and browsers. Remember that device fragmentation is a major concern for every developer and tester. Every website has to work seamlessly on multiple device-browser-OS combinations. With 9000+ distinct devices being used to access the internet globally, all software has to be optimized for different configurations, viewports, and screen resolutions.
In this state, no emulator or simulator can replicate real user conditions. Software needs to be tested on real devices so that they can work in real-world circumstances such as a low battery, incoming calls, weak network strength, and so on. If an in-house lab is not accessible, opt for a cloud-based testing option that offers real devices.
BrowserStack’s cloud Selenium grid offers 2000+ real devices and browsers for automated testing. That means users can run tests on multiple real devices and browsers by simply signing up, logging in, and selecting the required combinations. Testers can also conduct Cypress testing on 30+ real browser versions across Windows and macOS. Detects bugs before users do by testing software in real user conditions with BrowserStack.
Run Selenium Tests on Real Browsers
1. What is Selenium C# used for?
Selenium C# is used for automating web application testing. It allows developers and testers to write scripts in C# language to simulate user interactions with a web application, test its functionality, and verify that it behaves as expected. With Selenium C#, you can automate repetitive tasks, catch and debug errors early, and ensure the quality and reliability of your web application.
2. Which is better Selenium with Java or C#?
The choice between using Selenium with Java or C# primarily depends on your background, skills, and personal preferences. In terms of performance and capabilities, both Java and C# are equally good.
Java is a widely used programming language and has a large community of developers and testers who have created many resources and libraries that can be leveraged when using Selenium with Java. Java also has a long history of being used for automation testing, which has led to the creation of many testing frameworks and tools that integrate well with Selenium.
C#, on the other hand, is a modern and expressive language that is well-suited for Windows development. It is the primary language for .NET development, and the .NET ecosystem provides a wealth of tools and libraries that can be used in combination with Selenium C#.
3. Why is C# used for testing?
C# is used for testing because it is a modern, statically-typed, object-oriented programming language. Its syntax is easy to read and understand, making it a good choice for writing tests that need to be maintained over time. It provides a comprehensive set of features for creating automated tests. Additionally, C# has a rich set of libraries and frameworks, such as NUnit, that can be used to write tests for a wide range of applications, from desktop and web applications to games and mobile apps.
4. Which framework is best for Selenium C#?
NUnit Framework is the best for Selenium C#. NUnit provides a rich set of features for organizing and structuring tests, making it easy to write and maintain automated tests. Additionally, it supports data-driven testing and parallel test execution, which can help to significantly reduce the time taken to run a suite of tests.ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6snKWdnp7CrnnWoquhZZNiwKmt0alkn6eiYq62wM6mmK2dlGLBpr%2FT