Skip to main content

Run Selenium Tests With xUnit


Learn how to run your xUnit automation testing scripts on LambdaTest online Selenium Grid of 3000+ real browsers and operating systems.

Prerequisites


Before you start performing C# automation testing with Selenium using xUnit, you need to:

  1. Download and install Selenium WebDriver from its official website.
  2. Ensure you have the latest version of C#.
  3. .NET framework for guidelines while developing a range of applications using C#.
  4. Download Selenium WebDriver Language Binding for C# and extract them to the appropriate folder.
  5. Install a .NET Core SDK of 8.0 or greater version.

Installing Selenium Dependencies


  1. Clone the LambdaTest Selenium xUnit GitHub repository and navigate to the code directory:
git clone https://github.com/LambdaTest/CSharp-xUnit-Selenium
cd CSharp-xUnit-Selenium

Setting up Your Authentication


Ensure you have your LambdaTest credentials to run C# automation scripts. Get these credentials from the LambdaTest Profile by heading to Account Settings > Password & Security tab.

  1. Set your LambdaTest Username and Access Key in environment variables.

Windows:

set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR_ACCESS_KEY"

macOS/Linux:

export LT_USERNAME="YOUR_USERNAME" 
export LT_ACCESS_KEY="YOUR_ACCESS_KEY"

Running Your First xUnit Test


Test Scenario: Check out the sample SingleTest.cs file. This xUnit Selenium script tests a sample to-do list app by marking a couple of items as done, adding a new item to the list, and finally displaying the count of pending items as output.

  1. Navigate to config.json using VS Code. Replace this code in the config.json file in your project.

Configuring Test Capabilities


  1. In the config.json file, update your test capabilities. We are passing browser, browser version, and operating system information, along with LambdaTest Selenium Grid capabilities via the capabilities object.
{
"server": "hub.lambdatest.com",
"user": "LT_USERNAME",
"key": "LT_ACCESS_KEY",

"capabilities": {
"lt:options": {
"buildName": "xunit build",
"sessionName": "lambdatest xunit sample test",
"visual": "true",
"plugin": "xunit:sample"
}
},

"environments": [
{
"browserName": "chrome"
},
{
"browserName": "firefox"
},
{
"browserName": "safari"
}
],

"TunnelOptions": {
"tunnel": false
}
}

Executing the Test


  1. Build the solution in Visual Studio.

  2. Run the tests from the Test Explorer in Visual Studio.

  • If you are using macOS or Linux, to run the tests, clean and rebuild the project using the below command:
dotnet clean
  • Now, run the single test using the below command:
dotnet test --filter "profile=single"

Running Your xUnit Tests in Parallel


For Windows:

To execute parallel tests on Windows, run all tests from the Test Explorer in Visual Studio.

For macOS/Linux:

To execute parallel tests on macOS or Linux, run the below command:

dotnet test --filter "profile=parallel"

Testing Locally Hosted Or Privately Hosted Web Projects


For testing locally hosted or privately hosted projects with LambdaTest Selenium Grid using LambdaTest Tunnel, follow the LambdaTest Tunnel documentation.

Download the LambdaTest Tunnel binary for your OS and run the following command:

LT -user {user’s login email} -key {user’s access key}
Tunnel Capability

Tunnel Capability:

"lt:options": {
"buildName": "xunit build",
"sessionName": "lambdatest xunit sample test",
"visual": "true",
"plugin": "xunit:sample",
"tunnel": "true"
}