Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OFFI-85: Allow viewport screenshot for cases where element selection won't work. #417

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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