Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions LambdaTest.Sdk.Utils/LambdaTest.Sdk.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<PackageId>LambdaTest.Sdk.Utils</PackageId>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<Authors>Lambdatest-SmartUI</Authors>
<Company>LambdaTest</Company>
<Description>LambdaTest SDK Utilities for SmartUI CLI in C#</Description>
Expand All @@ -11,5 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>
</Project>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>
38 changes: 29 additions & 9 deletions LambdaTest.Sdk.Utils/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ private static LogLevel GetLogLevel()
return LogLevel.Debug;
}

var logLevelStr = Environment.GetEnvironmentVariable("LT_SDK_LOG_LEVEL")?.ToLower() ?? "info";
return logLevelStr switch
var logLevelStr = Environment.GetEnvironmentVariable("LT_SDK_LOG_LEVEL");
if (string.IsNullOrEmpty(logLevelStr))
{
"debug" => LogLevel.Debug,
"warning" => LogLevel.Warning,
"error" => LogLevel.Error,
"critical" => LogLevel.Critical,
_ => LogLevel.Information,
};
logLevelStr = "info";
}
else
{
logLevelStr = logLevelStr.ToLower();
}

switch (logLevelStr)
{
case "debug":
return LogLevel.Debug;
case "warning":
return LogLevel.Warning;
case "error":
return LogLevel.Error;
case "critical":
return LogLevel.Critical;
default:
return LogLevel.Information;
}
}

public static ILogger CreateLogger(string packageName)
{
Initialize();

if (loggerFactory == null)
{
throw new InvalidOperationException("Logger factory failed to initialize");
}

return loggerFactory.CreateLogger(packageName);
}
}
}
}
17 changes: 8 additions & 9 deletions LambdaTest.Sdk.Utils/SmartUI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Net.Http;
using System.Text.Json;
Expand All @@ -11,7 +9,7 @@ namespace LambdaTest.Sdk.Utils
{
public static class SmartUI
{
private static readonly HttpClient HttpClient = new();
private static readonly HttpClient HttpClient = new HttpClient();
private static readonly ILogger SdkUtilsLogger = Logger.CreateLogger("LambdaTest.Sdk.Utils");


Expand Down Expand Up @@ -49,7 +47,7 @@ public static async Task<string> FetchDomSerializer()
}
}

public static async Task<string> PostSnapshot(DomObject snapshot, string pkg, Dictionary<string, object>? options =null)
public static async Task<string> PostSnapshot(DomObject snapshot, string pkg, Dictionary<string, object> options = null)
{
try
{
Expand Down Expand Up @@ -125,6 +123,7 @@ public class Resource
public string mimetype { get; set; } = string.Empty;
public string url { get; set; } = string.Empty;
}

public class DomContent
{
public string html { get; set; } = string.Empty;
Expand All @@ -142,10 +141,10 @@ public class DomObject

public class SnapshotData
{
public DomContent? dom { get; set; }
public string? name { get; set; }
public string? url { get; set; }
public Dictionary<string, object>? options { get; set; } // Nullable for handling cases with or without options
public DomContent dom { get; set; }
public string name { get; set; }
public string url { get; set; }
public Dictionary<string, object> options { get; set; }
}
}
}
}
20 changes: 13 additions & 7 deletions LambdaTest.Selenium.Driver/LambdaTest.Selenium.Driver.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<PackageId>LambdaTest.Selenium.Driver</PackageId>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<Authors>Lambdatest-SmartUI</Authors>
<Company>LambdaTest</Company>
<Description>LambdaTest C# Selenium SDK </Description>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>LambdaTest C# Selenium SDK</Description>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../LambdaTest.Sdk.Utils/LambdaTest.Sdk.Utils.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="LambdaTest.Sdk.Utils" Version="1.0.4" />
<PackageReference Include="Selenium.Support" Version="4.21.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.21.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>
</Project>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>
20 changes: 12 additions & 8 deletions LambdaTest.Selenium.Driver/SmartUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class SmartUISnapshot
{
private static readonly ILogger SmartUILogger = Logger.CreateLogger("Lambdatest.Selenium.Driver");

public static async Task<String> CaptureSnapshot(IWebDriver driver, string name, Dictionary<string, object>? options = null)
public static async Task<String> CaptureSnapshot(IWebDriver driver, string name, Dictionary<string, object> options = null)
{
if (string.IsNullOrEmpty(name))
{
Expand All @@ -36,7 +36,7 @@ public static async Task<String> CaptureSnapshot(IWebDriver driver, string name,

var domSerializerScript = JsonSerializer.Deserialize<FetchDomSerializerResponse>(domSerializerResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (domSerializerScript?.Data?.Dom == null)
if (domSerializerScript == null || domSerializerScript.Data == null || domSerializerScript.Data.Dom == null)
{
throw new Exception("Failed to json serialize the DOM serializer script.");
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public static async Task<String> CaptureSnapshot(IWebDriver driver, string name,
};

// Handle sync parameter if present
if (options?.ContainsKey("sync") == true && (bool)options["sync"])
if (options != null && options.ContainsKey("sync") && (bool)options["sync"])
{
var contextId = Guid.NewGuid().ToString();
options["contextId"] = contextId;
Expand All @@ -105,7 +105,7 @@ public static async Task<String> CaptureSnapshot(IWebDriver driver, string name,
var apiResponseJSON = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "Lambdatest.Selenium.Driver", options);
var apiResponse = JsonSerializer.Deserialize<ApiResponse>(apiResponseJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (apiResponse?.Data?.Warnings != null && apiResponse.Data.Warnings.Count > 0)
if (apiResponse != null && apiResponse.Data != null && apiResponse.Data.Warnings != null && apiResponse.Data.Warnings.Count > 0)
{
foreach (var warning in apiResponse.Data.Warnings)
{
Expand All @@ -117,11 +117,15 @@ public static async Task<String> CaptureSnapshot(IWebDriver driver, string name,

// Get Snapshot Status
var timeout=600;
if (options.ContainsKey("timeout")){
if (options.ContainsKey("timeout"))
{
var tempTimeout= (int)options["timeout"];
if (tempTimeout<30||tempTimeout>900){
if (tempTimeout<30||tempTimeout>900)
{
SmartUILogger.LogWarning("Timeout value is out of range(30-900). Defaulting to 600 seconds.");
}else{
}
else
{
timeout=tempTimeout;
}
}
Expand All @@ -135,7 +139,7 @@ public static async Task<String> CaptureSnapshot(IWebDriver driver, string name,
var apiResponseJSON = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "Lambdatest.Selenium.Driver", options);
var apiResponse = JsonSerializer.Deserialize<ApiResponse>(apiResponseJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (apiResponse?.Data?.Warnings != null && apiResponse.Data.Warnings.Count > 0)
if (apiResponse != null && apiResponse.Data != null && apiResponse.Data.Warnings != null && apiResponse.Data.Warnings.Count > 0)
{
foreach (var warning in apiResponse.Data.Warnings)
{
Expand Down