Skip to content

Commit

Permalink
Allow viewport screenshot for cases where element selection won't work.
Browse files Browse the repository at this point in the history
e.g. capturing the built-in PDF viewer
  • Loading branch information
sarahelsaig committed Oct 21, 2024
1 parent cce32a0 commit c7cd645
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ public static Image TakeElementScreenshot(this UITestContext context, IWebElemen
{
using var screenshot = context.TakeFullPageScreenshot();

var elementLocation = element.Location;
var elementSize = element.Size;
var elementLocation = element?.Location ?? default;
var elementWidth = element?.Size.Width ?? screenshot.Size.Width;
var elementHeight = element?.Size.Height ?? screenshot.Size.Height;

var expectedSize = new Size(elementLocation.X + elementSize.Width, elementLocation.Y + elementSize.Height);
var expectedSize = new Size(elementLocation.X + elementWidth, elementLocation.Y + elementHeight);

var widthDifference = expectedSize.Width - screenshot.Width;
var heightDifference = expectedSize.Height - screenshot.Height;
Expand All @@ -134,7 +135,7 @@ public static Image TakeElementScreenshot(this UITestContext context, IWebElemen
}
}

var cropRectangle = new Rectangle(elementLocation.X, elementLocation.Y, elementSize.Width, elementSize.Height);
var cropRectangle = new Rectangle(elementLocation.X, elementLocation.Y, elementWidth, elementHeight);
return screenshot.Clone(image => image.Crop(cropRectangle));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ public static void AssertVisualVerificationApproved(
/// <see cref="AssertVisualVerificationApproved(UITestContext, IWebElement, double, Rectangle?, Action{VisualVerificationMatchApprovedConfiguration})"/>.
/// </summary>
/// <param name="context">The <see cref="UITestContext"/> in which the extension is executed on.</param>
/// <param name="elementSelector">Selector for the target element.</param>
/// <param name="elementSelector">
/// Selector for the target element. If <see langword="null" />, the current viewport (visible screen) will be
/// captured and the <c>viewport</c> text will be used instead of a selector description.
/// </param>
/// <param name="pixelErrorPercentageThreshold">Maximum acceptable pixel error in percentage.</param>
/// <param name="regionOfInterest">Region of interest. Can be <see langword="null"/>.</param>
/// <param name="configurator">Action callback to configure the behavior. Can be <see langword="null"/>.</param>
Expand Down Expand Up @@ -341,7 +344,7 @@ private static void AssertVisualVerificationApproved(
Rectangle? regionOfInterest = null,
Action<VisualVerificationMatchApprovedConfiguration> configurator = null) =>
context.AssertVisualVerificationApproved(
context.Get(elementSelector),
elementSelector is null ? null : context.Get(elementSelector),
comparator,
regionOfInterest,
configuration =>
Expand All @@ -350,7 +353,7 @@ private static void AssertVisualVerificationApproved(
configuration.WithFileNameSuffix(
new[]
{
elementSelector.ToString().MakeFileSystemFriendly(),
elementSelector?.ToString().MakeFileSystemFriendly() ?? "viewport",
configuration.FileNameSuffix,
}
.JoinNotNullOrEmpty("-")
Expand Down

0 comments on commit c7cd645

Please sign in to comment.