@@ -13,7 +13,7 @@ public static class SmartUISnapshot
1313 {
1414 private static readonly ILogger SmartUILogger = Logger . CreateLogger ( "Lambdatest.Selenium.Driver" ) ;
1515
16- public static async Task CaptureSnapshot ( IWebDriver driver , string name , Dictionary < string , object > ? options = null )
16+ public static async Task < String > CaptureSnapshot ( IWebDriver driver , string name , Dictionary < string , object > ? options = null )
1717 {
1818 if ( string . IsNullOrEmpty ( name ) )
1919 {
@@ -31,7 +31,7 @@ public static async Task CaptureSnapshot(IWebDriver driver, string name, Diction
3131
3232 if ( domSerializerResponse == null )
3333 {
34- throw new Exception ( "Failed to fetch DOM serializer script repsonse ." ) ;
34+ throw new Exception ( "Failed to fetch DOM serializer script response ." ) ;
3535 }
3636
3737 var domSerializerScript = JsonSerializer . Deserialize < FetchDomSerializerResponse > ( domSerializerResponse , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
@@ -46,22 +46,20 @@ public static async Task CaptureSnapshot(IWebDriver driver, string name, Diction
4646 ( ( IJavaScriptExecutor ) driver ) . ExecuteScript ( script ) ;
4747
4848 // Extract sessionId from driver
49- string sessionId = null ;
49+ string sessionId = "" ;
5050 if ( driver is RemoteWebDriver remoteDriver )
5151 {
5252 sessionId = remoteDriver . SessionId . ToString ( ) ;
5353 }
54-
54+ if ( options == null )
55+ {
56+ options = new Dictionary < string , object > ( ) ;
57+ }
5558 if ( ! string . IsNullOrEmpty ( sessionId ) )
5659 {
57- // Append sessionId to options
58- if ( options == null )
59- {
60- options = new Dictionary < string , object > ( ) ;
61- }
6260 options [ "sessionId" ] = sessionId ;
6361 }
64-
62+
6563 var optionsJSON = JsonSerializer . Serialize ( options ) ;
6664 var snapshotScript = @"
6765 var options = " + optionsJSON + @";
@@ -97,26 +95,62 @@ public static async Task CaptureSnapshot(IWebDriver driver, string name, Diction
9795 Url = domContent . Url
9896 } ;
9997
100- var apiResponseJSON = await LambdaTest . Sdk . Utils . SmartUI . PostSnapshot ( dom , "Lambdatest.Selenium.Driver" , options ) ;
101- var apiResponse = JsonSerializer . Deserialize < ApiResponse > ( apiResponseJSON , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
102-
103- if ( apiResponse ? . Data ? . Warnings != null && apiResponse . Data . Warnings . Count > 0 )
98+ // Handle sync parameter if present
99+ if ( options ? . ContainsKey ( "sync" ) == true && ( bool ) options [ "sync" ] )
104100 {
105- foreach ( var warning in apiResponse . Data . Warnings )
101+ var contextId = Guid . NewGuid ( ) . ToString ( ) ;
102+ options [ "contextId" ] = contextId ;
103+
104+ // Post Snapshot
105+ var apiResponseJSON = await LambdaTest . Sdk . Utils . SmartUI . PostSnapshot ( dom , "Lambdatest.Selenium.Driver" , options ) ;
106+ var apiResponse = JsonSerializer . Deserialize < ApiResponse > ( apiResponseJSON , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
107+
108+ if ( apiResponse ? . Data ? . Warnings != null && apiResponse . Data . Warnings . Count > 0 )
106109 {
107- SmartUILogger . LogWarning ( warning ) ;
110+ foreach ( var warning in apiResponse . Data . Warnings )
111+ {
112+ SmartUILogger . LogWarning ( warning ) ;
113+ }
108114 }
115+
116+ SmartUILogger . LogInformation ( $ "Snapshot captured: { name } ") ;
117+
118+ // Get Snapshot Status
119+ var timeout = 600 ;
120+ if ( options . ContainsKey ( "timeout" ) ) {
121+ timeout = ( int ) options [ "timeout" ] ;
122+ }
123+ var snapshotStatusJSON = await LambdaTest . Sdk . Utils . SmartUI . GetSnapshotStatus ( contextId , timeout , name ) ;
124+ var snapshotStatus = JsonSerializer . Deserialize < ApiResponse > ( snapshotStatusJSON , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
125+ return snapshotStatusJSON ;
109126 }
127+ else
128+ {
129+ // If sync is not true, simply post the snapshot
130+ var apiResponseJSON = await LambdaTest . Sdk . Utils . SmartUI . PostSnapshot ( dom , "Lambdatest.Selenium.Driver" , options ) ;
131+ var apiResponse = JsonSerializer . Deserialize < ApiResponse > ( apiResponseJSON , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
110132
111- SmartUILogger . LogInformation ( $ "Snapshot captured: { name } ") ;
133+ if ( apiResponse ? . Data ? . Warnings != null && apiResponse . Data . Warnings . Count > 0 )
134+ {
135+ foreach ( var warning in apiResponse . Data . Warnings )
136+ {
137+ SmartUILogger . LogWarning ( warning ) ;
138+ }
139+ }
140+
141+ SmartUILogger . LogInformation ( $ "Snapshot captured: { name } ") ;
142+ }
143+ return "" ;
112144 }
113145 catch ( Exception e )
114146 {
115147 SmartUILogger . LogError ( $ "SmartUI snapshot failed: { name } ") ;
116148 SmartUILogger . LogError ( e . ToString ( ) ) ;
149+ return "" ;
117150 }
118151 }
119152
153+
120154 private class ApiResponse
121155 {
122156 public ApiData Data { get ; set ; } = new ApiData ( ) ;
0 commit comments