UI Testing with C# using HyperExecute and Smart UI SDK
This documentation will guide you step-by-step to execute the Smart UI SDK tests on the HyperExecute platform using Selenium - C Sharp
Prerequisites
To run the Tests on HyperExecute from your Local System, you are required:
- Your LambdaTest Username and Access key
- HyperExecute CLI in order to initiate a test execution Job .
- Setup the Environmental Variable
- HyperExecute YAML file which contains all the necessary instructions.
- Login to LambdaTest SmartUI with your credentials.
Step 1: Create a SmartUI Project
The first step is to create a project with the application in which we will combine all your builds run on the project. To create a SmartUI Project, follow these steps:
- Go to Projects page
- Click on the
new project
button - Select the platform as CLI or Web for executing your
SDK
tests. - Add name of the project, approvers for the changes found, tags for any filter or easy navigation.
- Click on the Submit.
After creating the project, you will get your PROJECT_TOKEN
. You need to keep this project token safe as it will be used in the further steps below.
Step 2: Setup Your Test Suite
You can use your own project to configure and test it. For demo purposes, we are using the sample repository.
Update the Dependencies
- Add the following dependencies in your
.csproj
file
<ItemGroup>
<PackageReference Include="LambdaTest.Selenium.Driver" Version="1.0.1" />
</ItemGroup>
You can check the latest version of LambdaTest.Selenium.Driver and update the latest version accordingly.
Configuring your Project
You can configure your project meta information from here, like build name, project name, defining the platform, browser, browser version, your credentials etc.
var capabilities = new JObject {
["browserName"] = "Chrome",
["browserVersion"] = "latest",
["platformName"] = "Windows 10",
["name"] = "<YOUR_PROJECT_NAME>",
["build"] = "<YOUR_BUILD_NAME>",
["user"] = username,
["accessKey"] = accessKey,
["network"] = true,
["video"] = true,
["console"] = true
};
Adding SmartUI function to take screenshot
You can incorporate SmartUI into your custom Selenium
automation test (any platform) script by adding the smartuiSnapshot
function in the required segment of selenium script of which we would like to take the screenshot, as shown below:
using System;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using LambdaTest.Selenium.Driver;
namespace LambdaTest.Selenium.TestProject
{
public static class LocalTest{
public static async Task Run(){
using IWebDriver driver = new ChromeDriver();
try {
Console.WriteLine("Driver started");
driver.Navigate().GoToUrl("Required URL");
await SmartUISnapshot.CaptureSnapshot(driver, "Screenshot Name"); //utilize this function to take the dom snapshot of your test
}catch (Exception ex) {
Console.WriteLine(ex);
}finally {
driver.Quit();
}
}
}
}
Step 3: Setup the CLI in your Test Suite
After cloning / downloading the sample repo, you need to setup the CLI and the environment variables.
Download the HyperExecute CLI
The CLI is used for triggering the tests on HyperExecute. It is recommend to download the CLI binary on the host system and keep it in the root directory of the suite to perform the tests on HyperExecute.
You can download the CLI for your desired platform from the below mentioned links:
Setup Environment Variable
Now, you need to export your environment variables LT_USERNAME and LT_ACCESS_KEY that are available in the LambdaTest Profile page.
Run the below mentioned commands in your terminal to setup the CLI and the environment variables.
- Linux / MacOS
- Windows
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"
set LT_USERNAME="undefined"
set LT_ACCESS_KEY="undefined"
Step 4: Configure YAML in your Test Suite
You need to edit the PROJECT_TOKEN: "YOUR_PROJECT_TOKEN"
flag and enter your project token that show in the SmartUI app after, creating your project.
---
version: 0.1
runson: linux
autosplit: true
concurrency: 1
runtime:
language: dotnet
version: "8.0"
env:
# PAT: ${{ .secrets.testKey }}
PROJECT_TOKEN: <YOUR_PROJECT_TOKEN>
cacheKey: '{{ checksum "package-lock.json" }}'
cacheDirectories:
- node_modules
pre:
- dotnet clean
- dotnet build
- npm i @lambdatest/smartui-cli
- dotnet restore
- npx smartui config:create .smartui.json
testDiscovery:
type: raw
mode: dynamic
command: echo cloud
testRunnerCommand: npx smartui --config .smartui.json exec -- dotnet run $test
jobLabel: ["smart-ui-sdk", "hyperexecute", "selenium", "csharp"]
Step 5: Execute your Test Suite
NOTE : In case of macOS, if you get a permission denied warning while executing CLI, simply run
chmod u+x ./hyperexecute
to allow permission. In case you get a security popup, allow it from your System Preferences → Security & Privacy → General tab.
Run the below command in your terminal at the root folder of the project:
./hyperexecute --config RELATIVE_PATH_OF_YOUR_YAML_FILE
OR use this command if you have not exported your username and access key in the step 3.
./hyperexecute --user undefined --key undefined --config RELATIVE_PATH_OF_YOUR_YAML_FILE
Step 6: Monitor the Test Execution
Visit the HyperExecute Dashboard and check your Job status.
📕 Learn more about the other supported arguments, how you can handle the dynamic data and how to capture the screenshot of a specific element