1
+ using System ;
1
2
using System . Runtime . CompilerServices ;
2
3
using System . Threading . Tasks ;
3
4
using OpenQA . Selenium ;
@@ -10,9 +11,9 @@ namespace SauceLabs.Visual
10
11
/// </summary>
11
12
public class VisualClient : VisualClientBase
12
13
{
13
- private readonly string _sessionId ;
14
- private readonly string _jobId ;
15
- private string ? _sessionMetadataBlob ;
14
+ private static readonly WebDriverMetadataCache _wdCache = new WebDriverMetadataCache ( ) ;
15
+
16
+ private WebDriverMetadata ? _metadata ;
16
17
17
18
/// <summary>
18
19
/// Creates a new instance of <c>VisualClient</c>
@@ -76,17 +77,75 @@ public static async Task<VisualClient> Create(WebDriver wd, Region region, strin
76
77
/// <param name="buildOptions">the options of the build creation</param>
77
78
public static async Task < VisualClient > Create ( WebDriver wd , Region region , string username , string accessKey , CreateBuildOptions buildOptions )
78
79
{
79
- var client = new VisualClient ( wd , region , username , accessKey ) ;
80
+ var client = new VisualClient ( region , username , accessKey ) ;
81
+ client . _metadata = await client . GetMetadata ( wd ) ;
80
82
await client . SetupBuild ( buildOptions ) ;
81
83
return client ;
82
84
}
83
85
84
- private async Task SetupBuild ( CreateBuildOptions buildOptions )
86
+ /// <summary>
87
+ /// Creates a new instance of <c>VisualClient</c>
88
+ /// </summary>
89
+ public static async Task < VisualClient > Create ( )
90
+ {
91
+ return await Create ( Region . FromEnvironment ( ) , EnvVars . Username , EnvVars . AccessKey , new CreateBuildOptions ( ) ) ;
92
+ }
93
+
94
+ /// <summary>
95
+ /// Creates a new instance of <c>VisualClient</c>
96
+ /// </summary>
97
+ /// <param name="buildOptions">the options of the build creation</param>
98
+ public static async Task < VisualClient > Create ( CreateBuildOptions buildOptions )
99
+ {
100
+ return await Create ( Region . FromEnvironment ( ) , EnvVars . Username , EnvVars . AccessKey , buildOptions ) ;
101
+ }
102
+
103
+ /// <summary>
104
+ /// Creates a new instance of <c>VisualClient</c>
105
+ /// </summary>
106
+ /// <param name="region">the Sauce Labs region to connect to</param>
107
+ public static async Task < VisualClient > Create ( Region region )
85
108
{
86
- var response = await Api . WebDriverSessionInfo ( _jobId , _sessionId ) ;
87
- var metadata = response . EnsureValidResponse ( ) ;
88
- _sessionMetadataBlob = metadata . Result . Blob ;
109
+ return await Create ( region , EnvVars . Username , EnvVars . AccessKey , new CreateBuildOptions ( ) ) ;
110
+ }
111
+
112
+ /// <summary>
113
+ /// Creates a new instance of <c>VisualClient</c>
114
+ /// </summary>
115
+ /// <param name="region">the Sauce Labs region to connect to</param>
116
+ /// <param name="buildOptions">the options of the build creation</param>
117
+ public static async Task < VisualClient > Create ( Region region , CreateBuildOptions buildOptions )
118
+ {
119
+ return await Create ( region , EnvVars . Username , EnvVars . AccessKey , buildOptions ) ;
120
+ }
121
+
122
+ /// <summary>
123
+ /// Creates a new instance of <c>VisualClient</c>
124
+ /// </summary>
125
+ /// <param name="region">the Sauce Labs region to connect to</param>
126
+ /// <param name="username">the Sauce Labs username</param>
127
+ /// <param name="accessKey">the Sauce Labs access key</param>
128
+ public static async Task < VisualClient > Create ( Region region , string username , string accessKey )
129
+ {
130
+ return await Create ( region , username , accessKey , new CreateBuildOptions ( ) ) ;
131
+ }
89
132
133
+ /// <summary>
134
+ /// Creates a new instance of <c>VisualClient</c>
135
+ /// </summary>
136
+ /// <param name="region">the Sauce Labs region to connect to</param>
137
+ /// <param name="username">the Sauce Labs username</param>
138
+ /// <param name="accessKey">the Sauce Labs access key</param>
139
+ /// <param name="buildOptions">the options of the build creation</param>
140
+ public static async Task < VisualClient > Create ( Region region , string username , string accessKey , CreateBuildOptions buildOptions )
141
+ {
142
+ var client = new VisualClient ( region , username , accessKey ) ;
143
+ await client . SetupBuild ( buildOptions ) ;
144
+ return client ;
145
+ }
146
+
147
+ private async Task SetupBuild ( CreateBuildOptions buildOptions )
148
+ {
90
149
Build = await BuildFactory . Get ( Api , buildOptions ) ;
91
150
}
92
151
@@ -97,11 +156,8 @@ private async Task SetupBuild(CreateBuildOptions buildOptions)
97
156
/// <param name="region">the Sauce Labs region to connect to</param>
98
157
/// <param name="username">the Sauce Labs username</param>
99
158
/// <param name="accessKey">the Sauce Labs access key</param>
100
- private VisualClient ( WebDriver wd , Region region , string username , string accessKey ) : base ( region , username , accessKey )
159
+ private VisualClient ( Region region , string username , string accessKey ) : base ( region , username , accessKey )
101
160
{
102
- _sessionId = wd . SessionId . ToString ( ) ;
103
- _jobId = wd . Capabilities . HasCapability ( "jobUuid" ) ? wd . Capabilities . GetCapability ( "jobUuid" ) . ToString ( ) : _sessionId ;
104
-
105
161
}
106
162
107
163
/// <summary>
@@ -122,16 +178,48 @@ private async Task FinishBuild(VisualBuild build)
122
178
/// <returns></returns>
123
179
public Task < string > VisualCheck ( string name , VisualCheckOptions ? options = null ,
124
180
[ CallerMemberName ] string callerMemberName = "" )
181
+ {
182
+ if ( _metadata == null )
183
+ {
184
+ throw new InvalidOperationException ( "VisualClient has not been initialized with a WebDriver instance. Please use the `VisualCheck` method accepting a WebDriver instance." ) ;
185
+ }
186
+
187
+ options ??= new VisualCheckOptions ( ) ;
188
+ options . EnsureTestContextIsPopulated ( callerMemberName , PreviousSuiteName ) ;
189
+ PreviousSuiteName = options . SuiteName ;
190
+
191
+ return VisualCheckAsync ( name , options , _metadata ) ;
192
+ }
193
+
194
+ /// <summary>
195
+ /// <c>VisualCheck</c> captures a screenshot and queue it for processing.
196
+ /// </summary>
197
+ /// <param name="wd">the instance of the WebDriver session</param>
198
+ /// <param name="name">the name of the screenshot</param>
199
+ /// <param name="options">the configuration for the screenshot capture and comparison</param>
200
+ /// <param name="callerMemberName">the member name of the caller (automated) </param>
201
+ /// <returns></returns>
202
+ public async Task < string > VisualCheck ( WebDriver wd , string name , VisualCheckOptions ? options = null ,
203
+ [ CallerMemberName ] string callerMemberName = "" )
125
204
{
126
205
options ??= new VisualCheckOptions ( ) ;
127
206
options . EnsureTestContextIsPopulated ( callerMemberName , PreviousSuiteName ) ;
128
207
PreviousSuiteName = options . SuiteName ;
129
- return VisualCheckAsync ( name , options ) ;
208
+
209
+ var metadata = await GetMetadata ( wd ) ;
210
+ return await VisualCheckAsync ( name , options , metadata ) ;
211
+ }
212
+
213
+ private async Task < string > VisualCheckAsync ( string name , VisualCheckOptions options , WebDriverMetadata webDriverMetadata )
214
+ {
215
+ return await VisualCheckBaseAsync ( name , options , webDriverMetadata ) ;
130
216
}
131
217
132
- private async Task < string > VisualCheckAsync ( string name , VisualCheckOptions options )
218
+ private async Task < WebDriverMetadata > GetMetadata ( WebDriver wd )
133
219
{
134
- return await VisualCheckBaseAsync ( name , options , _jobId , _sessionId , _sessionMetadataBlob ) ;
220
+ var sessionId = wd . SessionId . ToString ( ) ;
221
+ var jobId = wd . Capabilities . HasCapability ( "jobUuid" ) ? wd . Capabilities . GetCapability ( "jobUuid" ) . ToString ( ) : sessionId ;
222
+ return await _wdCache . GetMetadata ( Api , sessionId , jobId ) ;
135
223
}
136
224
137
225
/// <summary>
0 commit comments