Skip to content

Commit

Permalink
Resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 22, 2024
1 parent 9ae81fb commit eb5ae53
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
1 change: 0 additions & 1 deletion visual-dotnet/SauceLabs.Visual.Tests/VisualApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using SauceLabs.Visual.GraphQL;
Expand Down
3 changes: 3 additions & 0 deletions visual-dotnet/SauceLabs.Visual/VisualCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public class VisualCheckOptions
public IgnoreRegion[]? IgnoreRegions { get; set; }
public IWebElement[]? IgnoreElements { get; set; }
public bool? CaptureDom { get; set; }

public string? ClassName { get; set; }
public string? TestName { get; set; }
}
}
47 changes: 32 additions & 15 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using OpenQA.Selenium;
using Polly;
Expand All @@ -26,8 +29,9 @@ public class VisualClient : IDisposable
public bool CaptureDom { get; set; } = false;
private readonly ResiliencePipeline _retryPipeline;

public string? CurrentTestName { get; set; }
public string? CurrentTestClass { get; set; }
public string? CurrentTestName { get; set; }
private string? _previousTestClass = null;

#region Create

Expand Down Expand Up @@ -201,10 +205,12 @@ private async Task<VisualBuild> FindBuildById(string buildId)
/// <returns>a <c>VisualBuild</c> instance</returns>
private async Task<VisualBuild> CreateBuild(CreateBuildOptions? options = null)
{
var projectName = options?.Project ?? Assembly.GetExecutingAssembly().FullName;

var result = (await _api.CreateBuild(new CreateBuildIn
{
Name = options?.Name,
Project = options?.Project,
Project = projectName,
Branch = options?.Branch,
CustomId = options?.CustomId,
DefaultBranch = options?.DefaultBranch,
Expand All @@ -230,23 +236,40 @@ private 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(string name, VisualCheckOptions? options = null)
public Task<string> VisualCheck(string name, VisualCheckOptions? options = null,
[CallerMemberName] string callerMemberName = "")
{
options ??= new VisualCheckOptions();
if (!string.IsNullOrEmpty(callerMemberName) && (string.IsNullOrEmpty(options.ClassName) || string.IsNullOrEmpty(options.TestName)))
{
var stack = new StackTrace();
var frame = stack.GetFrames()?.FirstOrDefault(f => f.GetMethod().Name == callerMemberName);
options.ClassName ??= frame?.GetMethod().DeclaringType?.FullName ?? _previousTestClass;
options.TestName ??= callerMemberName;
_previousTestClass = options.ClassName;
}
return VisualCheckAsync(name, options);
}

private async Task<string> VisualCheckAsync(string name, VisualCheckOptions options)
{

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>());
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,
name: name,
jobId: _jobId,
diffingMethod: options?.DiffingMethod ?? DiffingMethod.Simple,
diffingMethod: options.DiffingMethod ?? DiffingMethod.Simple,
regions: ignored.ToArray(),
sessionId: _sessionId,
sessionMetadata: _sessionMetadataBlob ?? "",
captureDom: options?.CaptureDom ?? CaptureDom,
suiteName: CurrentTestClass,
testName: CurrentTestName))).EnsureValidResponse();
captureDom: options.CaptureDom ?? CaptureDom,
suiteName: options.ClassName,
testName: options.TestName
))).EnsureValidResponse();
result.Result.Diffs.Nodes.ToList().ForEach(d => _screenshotIds.Add(d.Id));
return result.Result.Id;
}
Expand All @@ -267,12 +290,6 @@ public void Dispose()
_api.Dispose();
}

public void ResetCurrentTest()
{
CurrentTestClass = null;
CurrentTestName = null;
}

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

0 comments on commit eb5ae53

Please sign in to comment.