|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.Json; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Playwright; |
| 7 | +using LambdaTest.Sdk.Utils; |
| 8 | + |
| 9 | +namespace LambdaTest.Playwright.Driver |
| 10 | +{ |
| 11 | + public static class SmartUISnapshot |
| 12 | + { |
| 13 | + private static readonly ILogger SmartUILogger = Logger.CreateLogger("Lambdatest.Playwright.Driver"); |
| 14 | + public static async Task CaptureSnapshot(IPage page, string name, Dictionary<string, object>? options = null) |
| 15 | + { |
| 16 | + if (string.IsNullOrEmpty(name)) |
| 17 | + { |
| 18 | + throw new ArgumentException("The `snapshotName` argument is required.", nameof(name)); |
| 19 | + } |
| 20 | + |
| 21 | + if (!await LambdaTest.Sdk.Utils.SmartUI.IsSmartUIEnabled()) |
| 22 | + { |
| 23 | + throw new Exception("Cannot find SmartUI server."); |
| 24 | + } |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + var domSerializerResponse = await LambdaTest.Sdk.Utils.SmartUI.FetchDomSerializer(); |
| 29 | + if (domSerializerResponse == null) |
| 30 | + { |
| 31 | + throw new Exception("Failed to fetch DOM serializer script response."); |
| 32 | + } |
| 33 | + var domSerializerScript = JsonSerializer.Deserialize<FetchDomSerializerResponse>(domSerializerResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 34 | + if (domSerializerScript?.Data?.Dom == null) |
| 35 | + { |
| 36 | + throw new Exception("Failed to json serialize the DOM serializer script."); |
| 37 | + } |
| 38 | + |
| 39 | + string script = domSerializerScript.Data.Dom; |
| 40 | + |
| 41 | + if (options == null) |
| 42 | + { |
| 43 | + options = new Dictionary<string, object>(); |
| 44 | + } |
| 45 | + |
| 46 | + // Get test details from LambdaTestHook to extract the test ID |
| 47 | + string sessionId = ""; |
| 48 | + try |
| 49 | + { |
| 50 | + var testDetailsResponse = await page.EvaluateAsync<string>("_ => {}", "lambdatest_action: {\"action\": \"getTestDetails\"}"); |
| 51 | + if (!string.IsNullOrEmpty(testDetailsResponse)) |
| 52 | + { |
| 53 | + var testDetails = JsonSerializer.Deserialize<TestDetailsResponse>(testDetailsResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 54 | + sessionId = testDetails?.Data?.SessionId ?? $"playwright_{Guid.NewGuid():N}"; |
| 55 | + } |
| 56 | + } |
| 57 | + catch (Exception) |
| 58 | + { |
| 59 | + SmartUILogger.LogError("Failed to get test details from LambdaTestHook."); |
| 60 | + } |
| 61 | + if (!string.IsNullOrEmpty(sessionId)) |
| 62 | + { |
| 63 | + // Append sessionId to options |
| 64 | + options["sessionId"] = sessionId; |
| 65 | + } |
| 66 | + // Execute the DOM serializer script in the page context |
| 67 | + await page.EvaluateAsync(script); |
| 68 | + var optionsJSON = JsonSerializer.Serialize(options); |
| 69 | + var snapshotScript = @" |
| 70 | + () => { |
| 71 | + var options = " + optionsJSON + @"; |
| 72 | + return JSON.stringify({ |
| 73 | + dom: SmartUIDOM.serialize(options), |
| 74 | + url: document.URL |
| 75 | + }); |
| 76 | + }"; |
| 77 | + |
| 78 | + var domJSON = await page.EvaluateAsync<string>(snapshotScript); |
| 79 | + if (domJSON == null) |
| 80 | + { |
| 81 | + throw new Exception("Failed to capture DOM object."); |
| 82 | + } |
| 83 | + |
| 84 | + var domContent = JsonSerializer.Deserialize<DomDeserializerResponse>(domJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 85 | + |
| 86 | + if (domContent == null) |
| 87 | + { |
| 88 | + throw new Exception("Failed to convert DOM object into JSON"); |
| 89 | + } |
| 90 | + |
| 91 | + var dom = new LambdaTest.Sdk.Utils.SmartUI.DomObject |
| 92 | + { |
| 93 | + Dom = new LambdaTest.Sdk.Utils.SmartUI.DomContent |
| 94 | + { |
| 95 | + html = domContent.Dom.html, |
| 96 | + warnings = domContent.Dom.warnings, |
| 97 | + resources = domContent.Dom.resources, |
| 98 | + hints = domContent.Dom.hints |
| 99 | + }, |
| 100 | + Name = name, |
| 101 | + Url = domContent.Url |
| 102 | + }; |
| 103 | + |
| 104 | + var apiResponseJSON = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "lambdatest-csharp-playwright-driver", options); |
| 105 | + var apiResponse = JsonSerializer.Deserialize<ApiResponse>(apiResponseJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 106 | + |
| 107 | + if (apiResponse?.Data?.Warnings != null && apiResponse.Data.Warnings.Count > 0) |
| 108 | + { |
| 109 | + foreach (var warning in apiResponse.Data.Warnings) |
| 110 | + { |
| 111 | + SmartUILogger.LogWarning(warning); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + SmartUILogger.LogInformation($"Snapshot captured: {name}"); |
| 116 | + } |
| 117 | + catch (Exception e) |
| 118 | + { |
| 119 | + SmartUILogger.LogError($"Playwright snapshot failed: {name}"); |
| 120 | + SmartUILogger.LogError(e.ToString()); |
| 121 | + throw; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public static async Task CaptureSnapshot(IBrowserContext context, string name, Dictionary<string, object>? options = null) |
| 126 | + { |
| 127 | + // Capture snapshot from the first page in the context |
| 128 | + var pages = context.Pages; |
| 129 | + if (pages.Count > 0) |
| 130 | + { |
| 131 | + await CaptureSnapshot(pages[0], name, options); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + throw new Exception("No pages available in the browser context for snapshot capture."); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public static async Task CaptureSnapshot(IBrowser browser, string name, Dictionary<string, object>? options = null) |
| 140 | + { |
| 141 | + // Capture snapshot from the first context in the browser |
| 142 | + var contexts = browser.Contexts; |
| 143 | + if (contexts.Count > 0) |
| 144 | + { |
| 145 | + await CaptureSnapshot(contexts[0], name, options); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + throw new Exception("No browser contexts available for snapshot capture."); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private class ApiResponse |
| 154 | + { |
| 155 | + public ApiData Data { get; set; } = new ApiData(); |
| 156 | + } |
| 157 | + |
| 158 | + private class ApiData |
| 159 | + { |
| 160 | + public string Message { get; set; } = string.Empty; |
| 161 | + public List<string> Warnings { get; set; } = new List<string>(); |
| 162 | + } |
| 163 | + |
| 164 | + private class FetchDomSerializerResponse |
| 165 | + { |
| 166 | + public FetchDomSerializerData Data { get; set; } = new FetchDomSerializerData(); |
| 167 | + } |
| 168 | + |
| 169 | + private class FetchDomSerializerData |
| 170 | + { |
| 171 | + public string Dom { get; set; } = string.Empty; |
| 172 | + } |
| 173 | + |
| 174 | + private class DomJSONContent |
| 175 | + { |
| 176 | + public string html { get; set; } = string.Empty; |
| 177 | + public List<string> warnings { get; set; } = new List<string>(); |
| 178 | + public List<LambdaTest.Sdk.Utils.SmartUI.Resource> resources { get; set; } = new List<LambdaTest.Sdk.Utils.SmartUI.Resource>(); |
| 179 | + public List<string> hints { get; set; } = new List<string>(); |
| 180 | + } |
| 181 | + |
| 182 | + private class DomDeserializerResponse |
| 183 | + { |
| 184 | + public DomJSONContent Dom { get; set; } = new DomJSONContent(); |
| 185 | + public string Url { get; set; } = string.Empty; |
| 186 | + } |
| 187 | + |
| 188 | + private class TestDetailsResponse |
| 189 | + { |
| 190 | + [System.Text.Json.Serialization.JsonPropertyName("data")] |
| 191 | + public TestDetailsData Data { get; set; } = new TestDetailsData(); |
| 192 | + } |
| 193 | + |
| 194 | + private class TestDetailsData |
| 195 | + { |
| 196 | + [System.Text.Json.Serialization.JsonPropertyName("test_id")] |
| 197 | + public string TestId { get; set; } = string.Empty; |
| 198 | + |
| 199 | + [System.Text.Json.Serialization.JsonPropertyName("session_id")] |
| 200 | + public string SessionId { get; set; } = string.Empty; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments