Skip to content

Commit

Permalink
Merge pull request #247 from Lombiq/issue/OSOE-415
Browse files Browse the repository at this point in the history
OSOE-415: Visual verification update and code refactoring
  • Loading branch information
Piedone authored Dec 20, 2022
2 parents 2438aa1 + 2fc2acb commit f4700a2
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 81 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Atata;
using Codeuctivity.ImageSharpCompare;
using Lombiq.Tests.UI.Attributes;
using Lombiq.Tests.UI.Constants;
Expand All @@ -18,7 +17,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;

namespace Lombiq.Tests.UI.Extensions;

Expand Down Expand Up @@ -291,18 +289,13 @@ private static void AssertVisualVerificationApproved(
throw new VisualVerificationCallerMethodNotFoundException();
}

var approvedContext = new VisualVerificationMatchApprovedContext
{
ModuleName = testFrame.GetModuleName(),
MethodName = testFrame.GetMethodName(),
BrowserName = context.Driver.As<IHasCapabilities>().Capabilities.GetCapability("browserName") as string,
};

approvedContext.BaselineFileName = configuration.BaselineFileNameFormatter(configuration, approvedContext);
var approvedContext = new VisualVerificationMatchApprovedContext(context, configuration, testFrame);

// Try loading baseline image from embedded resources first.
approvedContext.BaselineResourceName = $"{testFrame.MethodInfo.DeclaringType!.Namespace}.{approvedContext.BaselineFileName}.png";
var baselineImage = testFrame.MethodInfo.DeclaringType.Assembly
var baselineImage = testFrame
.MethodInfo
.DeclaringType?
.Assembly
.GetResourceImageSharpImage(approvedContext.BaselineResourceName);

if (baselineImage == null)
Expand All @@ -323,29 +316,13 @@ private static void AssertVisualVerificationApproved(

throw new VisualVerificationSourceInformationNotAvailableException(
$"Source information not available, make sure you are compiling with full debug information."
+ $" Frame: {testFrame.MethodInfo.DeclaringType.Name}.{testFrame.MethodInfo.Name}."
+ $" Frame: {testFrame.MethodInfo.DeclaringType?.Name}.{testFrame.MethodInfo.Name}."
+ $" The suggested baseline image was added to the failure dump as {suggestedImageFileName}");
}

approvedContext.ModuleDirectory = Path.GetDirectoryName(testFrame.GetFileName());
approvedContext.BaselineImagePath = Path.Combine(
approvedContext.ModuleDirectory,
$"{approvedContext.BaselineFileName}.png");

if (!File.Exists(approvedContext.BaselineImagePath))
{
using var suggestedImage = context.TakeElementScreenshot(element);

suggestedImage.Save(approvedContext.BaselineImagePath, new PngEncoder());

// Appending suggested baseline image to failure dump too.
context.AppendFailureDump(
Path.Combine(
VisualVerificationMatchNames.DumpFolderName,
$"{approvedContext.BaselineFileName}.png"),
suggestedImage.Clone(),
messageIfExists: HintFailureDumpItemAlreadyExists);

context.SaveSuggestedImage(element, approvedContext.BaselineImagePath, approvedContext.BaselineFileName);
throw new VisualVerificationBaselineImageNotFoundException(approvedContext.BaselineImagePath);
}

Expand All @@ -360,14 +337,34 @@ private static void AssertVisualVerificationApproved(
diff => comparator(approvedContext, diff),
regionOfInterest,
cfg => cfg.WithFileNamePrefix(approvedContext.BaselineFileName)
.WithFileNameSuffix(string.Empty));
.WithFileNameSuffix(string.Empty),
approvedContext);
}
finally
{
baselineImage?.Dispose();
}
}

private static void SaveSuggestedImage(
this UITestContext context,
IWebElement element,
string baselineImagePath,
string baselineFileName)
{
using var suggestedImage = context.TakeElementScreenshot(element);

suggestedImage.Save(baselineImagePath, new PngEncoder());

// Appending suggested baseline image to failure dump too.
context.AppendFailureDump(
Path.Combine(
VisualVerificationMatchNames.DumpFolderName,
$"{baselineFileName}.png"),
suggestedImage.Clone(),
messageIfExists: HintFailureDumpItemAlreadyExists);
}

private static void AssertVisualVerification(
this UITestContext context,
By elementSelector,
Expand All @@ -388,7 +385,8 @@ private static void AssertVisualVerification(
Image baseline,
Action<ICompareResult> comparator,
Rectangle? regionOfInterest = null,
Action<VisualMatchConfiguration> configurator = null)
Action<VisualMatchConfiguration> configurator = null,
VisualVerificationMatchApprovedContext approvedContext = null)
{
var configuration = new VisualMatchConfiguration();
configurator?.Invoke(configuration);
Expand All @@ -403,10 +401,22 @@ private static void AssertVisualVerification(
using var elementImageOriginal = context.TakeElementScreenshot(element).ShouldNotBeNull();

// Checking the size of captured image.
elementImageOriginal.Width
.ShouldBeGreaterThanOrEqualTo(cropRegion.Left + cropRegion.Width);
elementImageOriginal.Height
.ShouldBeGreaterThanOrEqualTo(cropRegion.Top + cropRegion.Height);
try
{
elementImageOriginal.Width
.ShouldBeGreaterThanOrEqualTo(cropRegion.Left + cropRegion.Width);
elementImageOriginal.Height
.ShouldBeGreaterThanOrEqualTo(cropRegion.Top + cropRegion.Height);
}
catch
{
if (approvedContext != null)
{
context.SaveSuggestedImage(element, approvedContext.BaselineImagePath, approvedContext.BaselineFileName);
}

throw;
}

using var baselineImageOriginal = baseline.Clone();

Expand Down Expand Up @@ -645,41 +655,4 @@ private static bool IsCompilerGenerated(EnhancedStackFrame frame) =>

private static MethodBase GetMethodBase(this EnhancedStackFrame frame) =>
frame.MethodInfo.MethodBase ?? frame.MethodInfo.SubMethodBase;

private static string GetModuleName(this EnhancedStackFrame frame)
{
var currentMethod = frame.MethodInfo.DeclaringType;
string moduleName;

// This is required to retrieve the class from the inheritance chain where the method was declared but not
// overridden.
do
{
moduleName = currentMethod.Name;
currentMethod = currentMethod.DeclaringType;
}
while (currentMethod is not null);

var depthMark = new Regex("^(?<module>.*)`[0-9]+$", RegexOptions.ExplicitCapture);
if (depthMark.IsMatch(moduleName))
{
moduleName = depthMark.Match(moduleName).Groups["module"].Value;
}

return moduleName;
}

// Retrieves the method name. Removes the decoration in case of if it inherited from a base class but not overridden.
// Because of the inheritance, the method name gets some decoration in stack trace.
private static string GetMethodName(this EnhancedStackFrame frame)
{
var methodName = frame.MethodInfo.Name;
var inheritedMethod = new Regex("^<(?<method>.*)>.*$", RegexOptions.ExplicitCapture);
if (inheritedMethod.IsMatch(methodName))
{
methodName = inheritedMethod.Match(methodName).Groups["method"].Value;
}

return methodName;
}
}
76 changes: 68 additions & 8 deletions Lombiq.Tests.UI/Models/VisualVerificationMatchApprovedContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,72 @@
using Atata;
using Lombiq.Tests.UI.Services;
using OpenQA.Selenium;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace Lombiq.Tests.UI.Models;

public record VisualVerificationMatchApprovedContext
public class VisualVerificationMatchApprovedContext
{
public string ModuleName { get; set; }
public string MethodName { get; set; }
public string BrowserName { get; set; }
public string BaselineFileName { get; set; }
public string BaselineResourceName { get; set; }
public string ModuleDirectory { get; set; }
public string BaselineImagePath { get; set; }
public string ModuleName { get; }
public string MethodName { get; }
public string BrowserName { get; }
public string BaselineFileName { get; }
public string BaselineResourceName { get; }
public string ModuleDirectory { get; }
public string BaselineImagePath { get; }

public VisualVerificationMatchApprovedContext(
UITestContext context,
VisualVerificationMatchApprovedConfiguration configuration,
EnhancedStackFrame testFrame)
{
ModuleName = GetModuleName(testFrame);
MethodName = GetMethodName(testFrame);
BrowserName = context.Driver.As<IHasCapabilities>().Capabilities.GetCapability("browserName") as string;

BaselineFileName = configuration.BaselineFileNameFormatter(configuration, this);
BaselineResourceName = $"{testFrame.MethodInfo.DeclaringType!.Namespace}.{BaselineFileName}.png";

ModuleDirectory = Path.GetDirectoryName(testFrame.GetFileName());
BaselineImagePath = Path.Combine(ModuleDirectory!, $"{BaselineFileName}.png");
}

private static string GetModuleName(EnhancedStackFrame frame)
{
var currentMethod = frame.MethodInfo.DeclaringType!;
string moduleName;

// This is required to retrieve the class from the inheritance chain where the method was declared but not
// overridden.
do
{
moduleName = currentMethod.Name;
currentMethod = currentMethod.DeclaringType;
}
while (currentMethod is not null);

var depthMark = new Regex("^(?<module>.*)`[0-9]+$", RegexOptions.ExplicitCapture);
if (depthMark.IsMatch(moduleName))
{
moduleName = depthMark.Match(moduleName).Groups["module"].Value;
}

return moduleName;
}

// Retrieves the method name. Removes the decoration in case of if it inherited from a base class but not overridden.
// Because of the inheritance, the method name gets some decoration in stack trace.
private static string GetMethodName(EnhancedStackFrame frame)
{
var methodName = frame.MethodInfo.Name!;
var inheritedMethod = new Regex("^<(?<method>.*)>.*$", RegexOptions.ExplicitCapture);
if (inheritedMethod.IsMatch(methodName))
{
methodName = inheritedMethod.Match(methodName).Groups["method"].Value;
}

return methodName;
}
}

0 comments on commit f4700a2

Please sign in to comment.