Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
4DvAnCeBoY authored Feb 1, 2024
0 parents commit ebf026a
Show file tree
Hide file tree
Showing 7 changed files with 572 additions and 0 deletions.
223 changes: 223 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
# Run Selenium Tests With xUnit On LambdaTest

![LambdaTest Logo](https://user-images.githubusercontent.com/70570645/171429042-610e8f3d-d2a4-4896-8bdb-8aeed87e0ce7.png)

*Learn how to run C# scripts using the xUnit framework.*

<p align="center">
<a href="https://www.lambdatest.com/blog/" target="_bank">Blog</a>
&nbsp; &#8901; &nbsp;
<a href="https://www.lambdatest.com/support/docs/" target="_bank">Docs</a>
&nbsp; &#8901; &nbsp;
<a href="https://www.lambdatest.com/learning-hub/" target="_bank">Learning Hub</a>
&nbsp; &#8901; &nbsp;
<a href="https://www.lambdatest.com/newsletter/" target="_bank">Newsletter</a>
&nbsp; &#8901; &nbsp;
<a href="https://www.lambdatest.com/certifications/" target="_bank">Certifications</a>
&nbsp; &#8901; &nbsp;
<a href="https://www.youtube.com/c/LambdaTest" target="_bank">YouTube</a>
</p>
&emsp;
&emsp;
&emsp;

[<img height="58" width="200" src="https://user-images.githubusercontent.com/70570645/171866795-52c11b49-0728-4229-b073-4b704209ddde.png">](https://accounts.lambdatest.com/register)

## Table of Contents:

* [Prerequisites](#prerequisites)
* [Run Your First Test](#run-your-first-test)
* [Parallel Testing With xUnit](#running-your-parallel-tests-using-xunit-testing-framework)
* [Local Testing With xUnit](#testing-locally-hosted-or-privately-hosted-projects)

## Prerequisites

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

* Download and install **Selenium WebDriver** from its [official website](https://www.selenium.dev/downloads/).
* Ensure you have the latest version of C#.
* **.NET** framework for guidelines while developing a range of applications using C#.
* Download [Selenium WebDriver Language Binding](https://www.selenium.dev/downloads/) for C# and extract them to the appropriate folder. Require a [.NET Core SDK](https://dotnet.microsoft.com/en-us/download) of 8.0 or greater version.

### Installing Selenium Dependencies And Tutorial Repo

**Step 1:** Clone the LambdaTest CSharp-xUnit-Selenium 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. Obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/login) or your LambdaTest Profile.

**Step 2:** Set your LambdaTest Username and Access Key in environment variables.

**For Linux/macOS:**

``` sh
export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR_ACCESS_KEY"
```

**For Windows:**

```sh
set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR_ACCESS_KEY"
```

## Run Your First 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.
**Step 3:** Navigate to [config.json](https://github.com/LambdaTest/CSharp-xUnit-Selenium/blob/master/xUnitSelenium/config.json) using VSCode. Replace this code in the config.json file in your project.

### Configuration of Your Test Capabilities

**Step 4:** In the config, update your test capabilities. We are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via the capabilities object.

Example capabilities object:


```json
{
"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
}
}

```

**Note:** Generate capabilities for your test requirements with the help of the **[Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/)**.

### Executing the Test

**Step 5:** Build the solution in Visual Studio.

**Step 6:** Run the

tests from the Test Explorer in Visual Studio.

### Executing in Linux/macOS

* Clean and rebuild the project.

```sh
dotnet clean
```
* Execute Single Test

```sh
dotnet test --filter "profile=single"
```

## Running Your Parallel Tests Using xUnit Testing Framework

**Executing Parallel tests in Windows**

Run all tests from the Test Explorer in Visual Studio for parallel execution.

**Executing parallel tests in Linux/MacOS**

```sh
dotnet test --filter "profile=parallel"
```

## Testing Locally Hosted Or Privately Hosted Projects

For testing locally hosted or privately hosted projects with LambdaTest Selenium grid using LambdaTest Tunnel, follow the [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/).

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

```bash
LT -user {user’s login email} -key {user’s access key}
```

**Tunnel Capability**

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

## Tutorials 📙

*coming soon*

Subscribe To Our [LambdaTest YouTube Channel 🔔](https://www.youtube.com/c/LambdaTest) for the latest video tutorials.

## Documentation & Resources :books:

* [LambdaTest Documentation](https://www.lambdatest.com/support/docs/)
* [LambdaTest Blog](https://www.lambdatest.com/blog/)
* [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/)

## LambdaTest Community :busts_in_silhouette:

Join the [LambdaTest Community](https://community.lambdatest.com/) to interact with tech enthusiasts. Connect, ask questions, and learn from professionals worldwide.

## What's New At LambdaTest ❓

Stay updated with the latest features and product add-ons at [Changelog](https://changelog.lambdatest.com/).

## About LambdaTest

[LambdaTest](https://www.lambdatest.com/) is a leading test execution and orchestration platform. It allows users to run both manual and automated testing of web and mobile apps across various browsers, operating systems, and real device combinations. Over 500 enterprises and 1 million+ users across 130+ countries rely on LambdaTest for their testing needs.

### Features

* Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
* Real-time Cross-browser testing on 3000+ environments.
* Test on Real device cloud.
* Blazing fast test automation with HyperExecute.
* Accelerate testing, shorten job times, and get faster feedback on code changes with Test At Scale.
* Smart Visual Regression Testing on the cloud.
* 120+ third-party integration with your favorite tools for CI/CD, Project Management, Codeless Automation, and more.
* Automated Screenshot testing across multiple browsers in a single click.
* Local testing of web and mobile apps.
* Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
* Geolocation testing of web and mobile apps across 53+ countries.
* LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports.

[<img height="58" width="200" src="https://user-images.githubusercontent.com/70570645/171866795-52c11b49-0728-4229-b073-4b704209ddde.png

">](https://accounts.lambdatest.com/register)

## We Are Here to Help You :headphones:

* Have a query? We are available 24x7 to help. [Contact Us](mailto:support@lambdatest.com)
* For more info, visit [LambdaTest](https://www.lambdatest.com/)


25 changes: 25 additions & 0 deletions XUnit-LambdaTest.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnit-LambdaTest", "XUnit-LambdaTest\XUnit-LambdaTest.csproj", "{E4CB1B19-9253-4E1C-876C-1A13922E2404}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E4CB1B19-9253-4E1C-876C-1A13922E2404}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4CB1B19-9253-4E1C-876C-1A13922E2404}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4CB1B19-9253-4E1C-876C-1A13922E2404}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4CB1B19-9253-4E1C-876C-1A13922E2404}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {896D0BC3-8440-4DF8-9415-5FDDE5C36200}
EndGlobalSection
EndGlobal
104 changes: 104 additions & 0 deletions XUnit-LambdaTest/BaseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using OpenQA.Selenium.Remote;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium;

namespace XUnit_LambdaTest
{
public class BaseFixture : IDisposable
{

private RemoteWebDriver? WebDriver;


public RemoteWebDriver GetDriver(string platform, string profile)
{
// Get Configuration for correct profile
string currentDirectory = Directory.GetCurrentDirectory();
string path = Path.Combine(currentDirectory, "config.json");
JObject config = JObject.Parse(File.ReadAllText(path));
if (config is null)
throw new Exception("Configuration not found!");

// Get Platform specific capabilities
JObject capabilities = config.GetValue("environments").Where(x => x is JObject y && x["browserName"].ToString().Equals(platform)).ToList()[0] as JObject;

// Get Common Capabilities
JObject commonCapabilities = config.GetValue("capabilities") as JObject;

// Merge Capabilities
capabilities.Merge(commonCapabilities);

JObject ltOptions = capabilities["lt:options"] as JObject;

// Get username and accesskey
string? username = Environment.GetEnvironmentVariable("LT_USERNAME");
if (username is null)
username = config.GetValue("user").ToString();

string? accessKey = Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
if (accessKey is null)
accessKey = config.GetValue("key").ToString();

ltOptions["userName"] = username;
ltOptions["accessKey"] = accessKey;



capabilities["lt:options"] = ltOptions;

// Create Desired Cappabilities for WebDriver
DriverOptions desiredCapabilities = getBrowserOption(capabilities.GetValue("browserName").ToString());
capabilities.Remove("browserName");
foreach (var x in capabilities)
{
if (x.Key.Equals("lt:options"))
desiredCapabilities.AddAdditionalOption(x.Key, x.Value.ToObject<Dictionary<string, object>>());
else
desiredCapabilities.AddAdditionalOption(x.Key, x.Value);
}

// Create RemoteWebDriver instance
WebDriver = new RemoteWebDriver(new Uri($"https://{config["server"]}/wd/hub"), desiredCapabilities);

return WebDriver;
}

public void SetStatus(bool passed)
{
if(WebDriver is not null)
{
if (passed)
((IJavaScriptExecutor)WebDriver).ExecuteScript("lambda-status=passed");
else
((IJavaScriptExecutor)WebDriver).ExecuteScript("lambda-status=failed");
}
}

static DriverOptions getBrowserOption(String browser)
{
switch (browser)
{
case "chrome":
return new OpenQA.Selenium.Chrome.ChromeOptions();
case "firefox":
return new OpenQA.Selenium.Firefox.FirefoxOptions();
case "safari":
return new OpenQA.Selenium.Safari.SafariOptions();
case "edge":
return new OpenQA.Selenium.Edge.EdgeOptions();
default:
return new OpenQA.Selenium.Chrome.ChromeOptions();
}
}

public void Dispose()
{
if (WebDriver is not null)
{
WebDriver.Quit();
WebDriver.Dispose();
}
}
}
}
Loading

0 comments on commit ebf026a

Please sign in to comment.