Creating a C# based, XUnit + Selenium Tests using Visual Studio Code in a Linux operating system machine.

Prerequisites :

  • Visual Studio Code : We will be using Visual Studio Code 1.28.2 for setting up the selenium tests. If you want to know about installing the Visual Studio Code for Linux, please check the url
  • .Net Core SDK: We are using XUnit tests to run the selenium webdriver tests. This will require .Net Core SDK to be installed. If it is not installed already, please install using the link
  • Linux Operating system :  Ubuntu 18.04.1 LTS

Lets get started : ( You can clone the Repo for the source codes used in this post )

Setting up a C# extension for Visual Studio Code 

  • Launch the Visual Studio Code using the command: $ code . from the Terminal.
  • From the Extensions menu (Ctrl+Shift+X) search for a C# extension. (You can use the Omnisharp) and install it. Once installed, click Reload to Active option. This will add the C# extension to your Visual Studio Code.

CSharpExtensionForVSCode

Installing XUnit packages for the project

  • Create a folder where you want to store your tests :
  • Ensure the installation of the .Net Core SDK: Navigate to the folder where you want to create/store the tests. From the folder open a terminal (Ctrl+Shift+T) and then run the command:  $ dotnet –version

dotnet version

  • Now create a new Class Library: Run the command: $ dotnet new classlib . This will create a .Net standard  library class.

Classlibcreation

classlibrary

  • Updating the .csproj file with target framework as netcoreapp2.1: Open the .csproj file created inside the folder. Update the TargetFramwork to- netcoreapp2.1. The updated .csproj file will look like the below.

NetCoreAppUpdated

  • Build the project: Use the command: $ dotnet build to verify the project builds successfully.

buldsuccess.png

Read more details about the XUnit package setup please check the link

Setting up XUnit packages for the project

  • Open the Visual Studio Code from the folder where we created the class libraries. This can be done by opening a terminal and running the command: $ code .
  • Install  Nuget Manager extension for Visual Studio Code: (If you have already installed this then you can skip this step). You can use the extension manager(Ctrl+Shift+X) and search for Nuget package manager and Install it. Once installed Click on Reload to active.

nugetinstall

  • Install the Xunit packages for the project: Using Nuget Manager: Add Package, Packages to add :

xunit

xunit.runner.visualstudio

Microsoft.Net.Test.SDK

Nuget manager

  • Once the necessary packages are added, open the .csproj file to verify. The .csproj file will look similar to below.

csprojafterxunit

  • Create and  Run XUnit Tests: Now we can create and run our XUnit tests using Visual Studio Code. Go back to the VS code and Edit the Class1.cs file to add a new Test method. Run the tests using VS Code or from terminal using the command: $dotnet test

Updated Class1.cs file :

class1csunit

Running the tests in Terminal (using the command: $ dotnet test)

runxunittests

Setting up Selenium Packages for the project

  • Install Selenium Webdriver Package : Now from Visual Studio Code , Open the command pallette (Ctrl+Shift+P) , type Nuget Package Manager and Select Nuget package manager: Add Package – > Type Selenium.Webdriver and press enter. Then select the latest version. This will install the selenium package automatically.

seleniumdriver.png

Install below additional packages in the same way.

Selenium.Support

Selenium.Chrome.WebDriver.

  • Updated .csproj file: Now after adding the selenium related packages the .csproj file will look like the below.

CsprojAfterSel

  • Create and Run Selenium Tests: Now since everything is ready and set, we can create and run our selenium tests. Update the Class1.cs file with the below codes and Run the test using the same command: $dotnet test

Updated Class1.cs file :

class1cs

Running Selenium Tests using Terminal:

finalterminal

Wohoo!! We are all done. Now create your test scripts and enjoy the XUnit. Comment below for any queries. Thanks.

Advertisement