|
| 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 | + |
| 15 | + public static async Task CaptureSnapshot(IPage page, string name, Dictionary<string, object>? options = null) |
| 16 | + { |
| 17 | + if (string.IsNullOrEmpty(name)) |
| 18 | + { |
| 19 | + throw new ArgumentException("The `snapshotName` argument is required.", nameof(name)); |
| 20 | + } |
| 21 | + |
| 22 | + if (!await LambdaTest.Sdk.Utils.SmartUI.IsSmartUIEnabled()) |
| 23 | + { |
| 24 | + throw new Exception("Cannot find SmartUI server."); |
| 25 | + } |
| 26 | + |
| 27 | + try |
| 28 | + { |
| 29 | + var domSerializerResponse = await LambdaTest.Sdk.Utils.SmartUI.FetchDomSerializer(); |
| 30 | + if (domSerializerResponse == null) |
| 31 | + { |
| 32 | + throw new Exception("Failed to fetch DOM serializer script response."); |
| 33 | + } |
| 34 | + var domSerializerScript = JsonSerializer.Deserialize<FetchDomSerializerResponse>(domSerializerResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 35 | + if (domSerializerScript?.Data?.Dom == null) |
| 36 | + { |
| 37 | + throw new Exception("Failed to json serialize the DOM serializer script."); |
| 38 | + } |
| 39 | + |
| 40 | + string script = domSerializerScript.Data.Dom; |
| 41 | + |
| 42 | + // Execute the DOM serializer script in the page context |
| 43 | + await page.EvaluateAsync(script); |
| 44 | + var optionsJSON = JsonSerializer.Serialize(options); |
| 45 | + var snapshotScript = @" |
| 46 | + () => { |
| 47 | + var options = " + optionsJSON + @"; |
| 48 | + return JSON.stringify({ |
| 49 | + dom: SmartUIDOM.serialize(options), |
| 50 | + url: document.URL |
| 51 | + }); |
| 52 | + }"; |
| 53 | + |
| 54 | + var domJSON = await page.EvaluateAsync<string>(snapshotScript); |
| 55 | + if (domJSON == null) |
| 56 | + { |
| 57 | + throw new Exception("Failed to capture DOM object."); |
| 58 | + } |
| 59 | + |
| 60 | + var domContent = JsonSerializer.Deserialize<DomDeserializerResponse>(domJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 61 | + |
| 62 | + if (domContent == null) |
| 63 | + { |
| 64 | + throw new Exception("Failed to convert DOM object into JSON"); |
| 65 | + } |
| 66 | + |
| 67 | + var dom = new LambdaTest.Sdk.Utils.SmartUI.DomObject |
| 68 | + { |
| 69 | + Dom = new LambdaTest.Sdk.Utils.SmartUI.DomContent |
| 70 | + { |
| 71 | + html = domContent.Dom.html, |
| 72 | + warnings = domContent.Dom.warnings, |
| 73 | + resources = domContent.Dom.resources, |
| 74 | + hints = domContent.Dom.hints |
| 75 | + }, |
| 76 | + Name = name, |
| 77 | + Url = domContent.Url |
| 78 | + }; |
| 79 | + |
| 80 | + var apiResponseJSON = await LambdaTest.Sdk.Utils.SmartUI.PostSnapshot(dom, "Lambdatest.Playwright.Driver", options); |
| 81 | + var apiResponse = JsonSerializer.Deserialize<ApiResponse>(apiResponseJSON, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
| 82 | + |
| 83 | + if (apiResponse?.Data?.Warnings != null && apiResponse.Data.Warnings.Count > 0) |
| 84 | + { |
| 85 | + foreach (var warning in apiResponse.Data.Warnings) |
| 86 | + { |
| 87 | + SmartUILogger.LogWarning(warning); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + SmartUILogger.LogInformation($"Snapshot captured: {name}"); |
| 92 | + } |
| 93 | + catch (Exception e) |
| 94 | + { |
| 95 | + SmartUILogger.LogError($"Playwright snapshot failed: {name}"); |
| 96 | + SmartUILogger.LogError(e.ToString()); |
| 97 | + throw; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static async Task CaptureSnapshot(IBrowserContext context, string name, Dictionary<string, object>? options = null) |
| 102 | + { |
| 103 | + // Capture snapshot from the first page in the context |
| 104 | + var pages = context.Pages; |
| 105 | + if (pages.Count > 0) |
| 106 | + { |
| 107 | + await CaptureSnapshot(pages[0], name, options); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw new Exception("No pages available in the browser context for snapshot capture."); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static async Task CaptureSnapshot(IBrowser browser, string name, Dictionary<string, object>? options = null) |
| 116 | + { |
| 117 | + // Capture snapshot from the first context in the browser |
| 118 | + var contexts = browser.Contexts; |
| 119 | + if (contexts.Count > 0) |
| 120 | + { |
| 121 | + await CaptureSnapshot(contexts[0], name, options); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + throw new Exception("No browser contexts available for snapshot capture."); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private class ApiResponse |
| 130 | + { |
| 131 | + public ApiData Data { get; set; } = new ApiData(); |
| 132 | + } |
| 133 | + |
| 134 | + private class ApiData |
| 135 | + { |
| 136 | + public string Message { get; set; } = string.Empty; |
| 137 | + public List<string> Warnings { get; set; } = new List<string>(); |
| 138 | + } |
| 139 | + |
| 140 | + private class FetchDomSerializerResponse |
| 141 | + { |
| 142 | + public FetchDomSerializerData Data { get; set; } = new FetchDomSerializerData(); |
| 143 | + } |
| 144 | + |
| 145 | + private class FetchDomSerializerData |
| 146 | + { |
| 147 | + public string Dom { get; set; } = string.Empty; |
| 148 | + } |
| 149 | + |
| 150 | + private class DomJSONContent |
| 151 | + { |
| 152 | + public string html { get; set; } = string.Empty; |
| 153 | + public List<string> warnings { get; set; } = new List<string>(); |
| 154 | + public List<LambdaTest.Sdk.Utils.SmartUI.Resource> resources { get; set; } = new List<LambdaTest.Sdk.Utils.SmartUI.Resource>(); |
| 155 | + public List<string> hints { get; set; } = new List<string>(); |
| 156 | + } |
| 157 | + |
| 158 | + private class DomDeserializerResponse |
| 159 | + { |
| 160 | + public DomJSONContent Dom { get; set; } = new DomJSONContent(); |
| 161 | + public string Url { get; set; } = string.Empty; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments