Skip to content

Commit fad6c29

Browse files
committed
Add options and code refactoring
1 parent a444f28 commit fad6c29

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

LambdaTest.Sdk.Utils/SmartUI.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Net.Http;
3-
using System.Text;
43
using System.Text.Json;
54
using System.Threading.Tasks;
65
using System.Collections.Generic;
@@ -10,7 +9,7 @@ namespace LambdaTest.Sdk.Utils
109
{
1110
public static class SmartUI
1211
{
13-
private static readonly HttpClient HttpClient = new HttpClient();
12+
private static readonly HttpClient HttpClient = new();
1413
private static readonly ILogger SdkUtilsLogger = Logger.CreateLogger("LambdaTest.Sdk.Utils");
1514

1615
public static async Task<bool> IsSmartUIEnabled()
@@ -47,22 +46,24 @@ public static async Task<string> FetchDomSerializer()
4746
}
4847
}
4948

50-
public static async Task<string> PostSnapshot(DomObject snapshot, string pkg, object additionalParams = null)
49+
public static async Task<string> PostSnapshot(DomObject snapshot, string pkg, object options = null)
5150
{
5251
try
5352
{
54-
var json = JsonSerializer.Serialize(new
53+
object snapshotObject = new
5554
{
56-
snapshot = new
57-
{
58-
dom = snapshot.Dom,
59-
name = snapshot.Name,
60-
url = snapshot.Url,
61-
},
62-
testType = pkg
63-
});
55+
dom = snapshot.Dom,
56+
name = snapshot.Name,
57+
url = snapshot.Url
58+
};
6459

60+
var jsonObject = new
61+
{
62+
snapshot = options != null ? new { dom = snapshot.Dom, name = snapshot.Name, url = snapshot.Url, options } : snapshotObject,
63+
testType = pkg
64+
};
6565

66+
var json = JsonSerializer.Serialize(jsonObject);
6667
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
6768
var response = await HttpClient.PostAsync($"{Constants.GetSmartUIServerAddress()}/snapshot", content);
6869
response.EnsureSuccessStatusCode();
@@ -80,21 +81,6 @@ public static async Task<string> PostSnapshot(DomObject snapshot, string pkg, o
8081
throw new Exception("post snapshot failed", e);
8182
}
8283
}
83-
static Dictionary<string, object> CreateDictionary(string name, Dictionary<string, object> additionalParams)
84-
{
85-
var result = new Dictionary<string, object>
86-
{
87-
{ "name", name }
88-
};
89-
90-
// Add the additional parameters to the result dictionary
91-
foreach (var kvp in additionalParams)
92-
{
93-
result[kvp.Key] = kvp.Value;
94-
}
95-
96-
return result;
97-
}
9884
public class DomContent
9985
{
10086
public string html { get; set; } = string.Empty;

LambdaTest.Selenium.Driver/SmartUI.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,40 @@ public static async Task CaptureSnapshot(IWebDriver driver, string name, object?
2626

2727
try
2828
{
29-
var resp = await LambdaTest.Sdk.Utils.SmartUI.FetchDomSerializer();
29+
var domSerializerResponse = await LambdaTest.Sdk.Utils.SmartUI.FetchDomSerializer();
3030

31-
var optionsSer = new JsonSerializerOptions
31+
if (domSerializerResponse == null)
3232
{
33-
PropertyNameCaseInsensitive = true
34-
};
35-
var responseObject = JsonSerializer.Deserialize<FetchDomSerializerResponse>(resp, optionsSer);
33+
throw new Exception("Failed to fetch DOM serializer script repsonse.");
34+
}
35+
36+
var domSerializerScript = JsonSerializer.Deserialize<FetchDomSerializerResponse>(domSerializerResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
3637

37-
if (responseObject?.Data?.Dom == null)
38+
if (domSerializerScript?.Data?.Dom == null)
3839
{
39-
throw new Exception("Failed to fetch DOM serializer script.");
40+
throw new Exception("Failed to json serialize the DOM serializer script.");
4041
}
4142

42-
string script = responseObject.Data.Dom;
43+
string script = domSerializerScript.Data.Dom;
4344

4445
((IJavaScriptExecutor)driver).ExecuteScript(script);
4546

46-
var optionsJson = JsonSerializer.Serialize(options);
47+
var optionsJSON = JsonSerializer.Serialize(options);
4748
var snapshotScript = @"
48-
var options = " + optionsJson + @";
49+
var options = " + optionsJSON + @";
4950
return JSON.stringify({
5051
dom: SmartUIDOM.serialize(options),
5152
url: document.URL
5253
});";
5354

54-
var domJson = (string)((IJavaScriptExecutor)driver).ExecuteScript(snapshotScript);
55+
var domJSON = (string)((IJavaScriptExecutor)driver).ExecuteScript(snapshotScript);
56+
57+
if (domJSON == null)
58+
{
59+
throw new Exception("Failed to capture DOM object.");
60+
}
5561

56-
var domContent = JsonSerializer.Deserialize<DomDeserializerResponse>(domJson, optionsSer);
62+
var domContent = JsonSerializer.Deserialize<DomDeserializerResponse>(domJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
5763

5864
if (domContent == null)
5965
{
@@ -73,13 +79,12 @@ public static async Task CaptureSnapshot(IWebDriver driver, string name, object?
7379
Url = domContent.Url
7480
};
7581

76-
var resJson = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "lambdatest-selenium-driver", options);
77-
SmartUILogger.LogInformation($"HERE IS RES_JSON: {resJson}");
78-
var res = JsonSerializer.Deserialize<ApiResponse>(resJson);
82+
var apiResponseJSON = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "Lambdatest.Selenium.Driver", options);
83+
var apiResponse = JsonSerializer.Deserialize<ApiResponse>(apiResponseJSON);
7984

80-
if (res?.Data?.Warnings != null && res.Data.Warnings.Count > 0)
85+
if (apiResponse?.Data?.Warnings != null && apiResponse.Data.Warnings.Count > 0)
8186
{
82-
foreach (var warning in res.Data.Warnings)
87+
foreach (var warning in apiResponse.Data.Warnings)
8388
{
8489
SmartUILogger.LogWarning(warning);
8590
}

0 commit comments

Comments
 (0)