Selenium with C# and xUnit
In this blog I will explain how you can start with automating your functional test using C# and xUnit.
A little introduction about the tools and technologies we are going to use.
- C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. Source
- Selenium is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE). It provides supports to C#, Java, Groovy, Perl, PHP, Python and Ruby. The tests can then be run against most modern web browsers. Selenium deploys on Windows, Linux, and Macintosh platforms. Source
- xUnit is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit is the latest technology for unit testing C#, F#, VB and other .NET languages. Source
I am not going in the details as there are plenty of information available on Internet about these, we will focus on creating a actual xUnit test framework using C# and Selenium.
Prerequisites
- Visual Studio (for this blog we are using VS 2015)
Let’s Start
- Open Visual Studio
- Click on New Project, it will open a window, you need to select a class library project, enter name for solution say “Functional” and for project say “Demo” then click OK.
- Now you will see screen as follows:
- Right click on the Project “Demo” and select Manage NuGet Packages…
- It will take you to following screen:
- Search for following packages and install those:
- Selenium.WebDriver
- Selenium.Chrome.WebDriver (chrome driver exe)
- xUnit.runner.visualstudio (to discover xUnit tests)
Just an information, xUnit have it’s own way of working, unlike NUnit or MSUnit there is no [SetUp] or [TestInitialize], here you need to achieve this using parameterless constructor.
You can get all other information in following article: https://xunit.github.io/docs/comparisons.html
Now you’re ready to automate your first test, change class name to Tests(or whatever you want) and start writing your first test as below:
Now just build your code by right click the project Demo Or by pressing Ctrl + Shift + B and you will be able to see your test in “Test Explorer”.
To run you test you need to right click on the test itself or click on Run All in the Explorer, check image below:
It will execute test and if Assert get passed it will show your test case as passed.
This blog is just to give an idea about how you can start Selenium with C# and xUnit, now you can start building a complete framework of your choice. If you need in-depth information or have any feedback, please mention in comments.