Skip to content

Commit

Permalink
Hide Create/Finish build. Implements IDisposable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 14, 2024
1 parent 05df645 commit f164434
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
15 changes: 8 additions & 7 deletions visual-dotnet/SauceLabs.Visual/VisualApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
Expand All @@ -12,7 +13,7 @@

namespace SauceLabs.Visual
{
internal class VisualApi<T> where T : IHasCapabilities, IHasSessionId
internal class VisualApi<T> : IDisposable where T : IHasCapabilities, IHasSessionId
{
private readonly string _username;
private readonly string _accessKey;
Expand Down Expand Up @@ -45,11 +46,6 @@ public VisualApi(T webdriver, Region region, string username, string accessKey,
httpClient);
}

~VisualApi()
{
_graphQlClient.Dispose();
}

public async Task<GraphQLResponse<ServerResponse<CreateBuild>>> CreateBuild(CreateBuildIn input)
{
var request = CreateAuthenticatedRequest(CreateBuildMutation.OperationDocument, CreateBuildMutation.OperationName, new { input });
Expand Down Expand Up @@ -98,5 +94,10 @@ private AuthenticatedGraphQLRequest CreateAuthenticatedRequest(string query, str
Variables = variables
};
}

public void Dispose()
{
_graphQlClient.Dispose();
}
}
}
47 changes: 42 additions & 5 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ namespace SauceLabs.Visual
/// <summary>
/// <c>VisualClient</c> provides an access to Sauce Labs Visual services.
/// </summary>
public class VisualClient
public class VisualClient : IDisposable
{
private readonly VisualApi<WebDriver> _api;
private readonly string _sessionId;
private readonly string _jobId;
private readonly string _sessionMetadataBlob;
private readonly List<string> _screenshotIds = new List<string>();
public VisualBuild Build { get; }
private readonly bool _externalBuild;
public bool CaptureDom { get; set; } = false;

/// <summary>
Expand All @@ -38,14 +40,40 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
var response = _api.WebDriverSessionInfo(_jobId, _sessionId).Result;
var metadata = response.EnsureValidResponse();
_sessionMetadataBlob = metadata.Result.Blob;

var createBuildResponse = CreateBuild(new CreateBuildOptions()).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
_externalBuild = false;
}

/// <summary>
/// Creates a new instance of <c>VisualClient</c>
/// </summary>
/// <param name="wd">the instance of the WebDriver session</param>
/// <param name="region">the Sauce Labs region to connect to</param>
/// <param name="username">the Sauce Labs username</param>
/// <param name="accessKey">the Sauce Labs access key</param>
/// <param name="buildOptions">the options of the build creation</param>
public VisualClient(WebDriver wd, Region region, string username, string accessKey, CreateBuildOptions buildOptions)
{
_api = new VisualApi<WebDriver>(wd, region, username, accessKey);
_sessionId = wd.SessionId.ToString();
_jobId = wd.Capabilities.HasCapability("jobUuid") ? wd.Capabilities.GetCapability("jobUuid").ToString() : _sessionId;
var response = _api.WebDriverSessionInfo(_jobId, _sessionId).Result;
var metadata = response.EnsureValidResponse();
_sessionMetadataBlob = metadata.Result.Blob;

var createBuildResponse = CreateBuild(buildOptions).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
_externalBuild = false;
}

/// <summary>
/// <c>CreateBuild</c> creates a new Visual build.
/// </summary>
/// <param name="options">the options for the build creation</param>
/// <returns>a <c>VisualBuild</c> instance</returns>
public async Task<VisualBuild> CreateBuild(CreateBuildOptions? options = null)
private async Task<VisualBuild> CreateBuild(CreateBuildOptions? options = null)
{
var result = (await _api.CreateBuild(new CreateBuildIn
{
Expand All @@ -61,7 +89,7 @@ public async Task<VisualBuild> CreateBuild(CreateBuildOptions? options = null)
/// <c>FinishBuild</c> finishes a build
/// </summary>
/// <param name="build">the build to finish</param>
public async Task FinishBuild(VisualBuild build)
private async Task FinishBuild(VisualBuild build)
{
(await _api.FinishBuild(build.Id)).EnsureValidResponse();
}
Expand All @@ -73,14 +101,14 @@ public async Task FinishBuild(VisualBuild build)
/// <param name="name">the name of the screenshot</param>
/// <param name="options">the configuration for the screenshot capture and comparison</param>
/// <returns></returns>
public async Task<string> VisualCheck(VisualBuild build, string name, VisualCheckOptions? options = null)
public async Task<string> VisualCheck(string name, VisualCheckOptions? options = null)
{
var ignored = new List<RegionIn>();
ignored.AddRange(options?.IgnoreRegions?.Select(r => new RegionIn(r)) ?? new List<RegionIn>());
ignored.AddRange(options?.IgnoreElements?.Select(r => new RegionIn(r)) ?? new List<RegionIn>());

var result = (await _api.CreateSnapshotFromWebDriver(new CreateSnapshotFromWebDriverIn(
buildUuid: build.Id,
buildUuid: Build.Id,
name: name,
jobId: _jobId,
diffingMethod: options?.DiffingMethod ?? DiffingMethod.Simple,
Expand All @@ -92,6 +120,15 @@ public async Task<string> VisualCheck(VisualBuild build, string name, VisualChec
return result.Result.Id;
}

public void Dispose()
{
_api.Dispose();
if (!_externalBuild)
{
FinishBuild(Build).Wait();
}
}

/// <summary>
/// <c>VisualResults</c> returns the results of screenshot comparison.
/// </summary>
Expand Down

0 comments on commit f164434

Please sign in to comment.