Skip to content

Commit

Permalink
Merge pull request #262 from Lombiq/issue/LMBQ-114
Browse files Browse the repository at this point in the history
LMBQ-114: AssertVisualVerificationOnAllResolutionsAsync and bugfixes
  • Loading branch information
DemeSzabolcs authored Feb 23, 2023
2 parents 3f43cd4 + 409b726 commit 589cd39
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
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
Expand Up @@ -71,7 +71,7 @@ public static Image TakeFullPageScreenshot(this UITestContext context)
position =>
position.Y % viewportSize.Height == 0
? viewportSize.Height
: viewportSize.Height - (viewportSize.Height % position.Y));
: (position.Y + viewportSize.Height) % viewportSize.Height);

var screenshot = new SixLabors.ImageSharp.Image<Argb32>(viewportSize.Width, height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Lombiq.Tests.UI.Extensions;

Expand All @@ -27,6 +29,61 @@ public static class VisualVerificationUITestContextExtensions
Hint: You can use the configurator callback of {nameof(AssertVisualVerificationApproved)} and {nameof(AssertVisualVerification)}
to customize the name of the dump item.";

/// <summary>
/// Compares the baseline image and screenshot of the selected element on multiple different resolutions.
/// </summary>
/// <param name="sizes">The comparison is performed on each of these resolutions.</param>
/// <param name="beforeAssertAsync">
/// Task to be performed after the viewport has been resized but before the screenshot is taken. This can be used
/// for example if the page must be loaded in the given size.
/// </param>
/// <param name="getSelector">
/// Returns the selector for the given screen size. This may return the same selector all the time, or a different
/// selector, e.g. if mobile and desktop views have different DOMs.
/// </param>
[VisualVerificationApprovedMethod]
public static async Task AssertVisualVerificationOnAllResolutionsAsync(
this UITestContext context,
IEnumerable<Size> sizes,
Func<Task> beforeAssertAsync,
Func<Size, By> getSelector)
{
context.HideScrollbar();

var exceptions = new List<Exception>();
foreach (var size in sizes)
{
context.SetViewportSize(size);
await beforeAssertAsync();

try
{
context.AssertVisualVerificationApproved(
getSelector(size),
pixelErrorPercentageThreshold: 0,
configurator: configuration => configuration
.WithFileNameSuffix(FormattableString.Invariant($"{size.Width}x{size.Height}")));
}
catch (Exception exception)
{
// We don't throw yet, this way if there are missing images they are generated all in one run.
exceptions.Add(exception);
}
}

if (exceptions.Count == 1) throw exceptions.Single();

if (exceptions.Any())
{
// The UITestExecutionSession doesn't support AggregateException with multiple inner exceptions, so we just
// concatenate the exceptions if there are multiple.
throw new InvalidOperationException(
"Several exceptions have occurred:\n" + string.Join("\n\n\n", exceptions.Select(ex => ex.ToString())));
}

context.RestoreHiddenScrollbar();
}

/// <summary>
/// Compares the baseline image and screenshot of the whole page.
/// <see cref="AssertVisualVerificationApproved(UITestContext, By, double, Rectangle?, Action{VisualVerificationMatchApprovedConfiguration})"/>.
Expand Down Expand Up @@ -271,8 +328,9 @@ private static void AssertVisualVerificationApproved(
var stackTrace = new EnhancedStackTrace(new StackTrace(fNeedFileInfo: true))
.Where(frame => frame.GetMethodBase() != null && !IsCompilerGenerated(frame))
.ToList();
var testFrame = stackTrace
.FirstOrDefault(frame => !IsVisualVerificationMethod(frame));
var testFrame = stackTrace.FirstOrDefault(frame =>
!IsVisualVerificationMethod(frame) &&
!string.IsNullOrEmpty(frame.StackFrame.GetFileName()));

if (testFrame != null && configuration.StackOffset > 0)
{
Expand Down

0 comments on commit 589cd39

Please sign in to comment.