Skip to content

Commit 4d6515f

Browse files
[Testing] Added Left/Right Cropping Support for Screenshot Verification in UI Tests (#31715)
* Added cropLeft and cropRight implementation * Update src/Controls/tests/TestCases.Shared.Tests/UITest.cs Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent e82e914 commit 4d6515f

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed
-20 KB
Loading
-9.11 KB
Loading
-31.2 KB
Loading

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue15154.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public void ShouldFlyoutTextWrapsInLandscape()
2020
App.WaitForElement("OpenFlyoutButton");
2121
App.Tap("OpenFlyoutButton");
2222
App.SetOrientationLandscape();
23+
#if ANDROID
24+
VerifyScreenshot(cropLeft: 125);
25+
#else
2326
VerifyScreenshot();
27+
#endif
2428
}
2529
}
2630
#endif

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue22606.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public void BorderBackgroundSizeUpdatesWhenRotatingScreen()
2929
App.WaitForElement("SetHeightTo200");
3030
App.Tap("SetHeightTo200");
3131
App.SetOrientationLandscape();
32+
#if ANDROID
33+
VerifyScreenshot(cropLeft: 125);
34+
#else
3235
VerifyScreenshot();
36+
#endif
3337
}
3438
#endif
3539
}

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28523.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public void CarouselViewItemShouldScaleProperly()
1919
App.WaitForElement("Baboon");
2020
App.SetOrientationLandscape();
2121
App.WaitForElement("Baboon");
22+
#if ANDROID
23+
VerifyScreenshot(cropLeft: 125);
24+
#else
2225
VerifyScreenshot();
26+
#endif
2327
}
2428

2529
[TearDown]

src/Controls/tests/TestCases.Shared.Tests/UITest.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public void VerifyScreenshotOrSetException(
124124
ref Exception? exception,
125125
string? name = null,
126126
TimeSpan? retryDelay = null,
127+
int cropLeft = 0,
128+
int cropRight = 0,
127129
int cropTop = 0,
128130
int cropBottom = 0,
129131
double tolerance = 0.0
@@ -134,7 +136,7 @@ public void VerifyScreenshotOrSetException(
134136
{
135137
try
136138
{
137-
VerifyScreenshot(name, retryDelay, cropTop, cropBottom, tolerance
139+
VerifyScreenshot(name, retryDelay, cropLeft, cropRight, cropTop, cropBottom, tolerance
138140
#if MACUITEST || WINTEST
139141
, includeTitleBar
140142
#endif
@@ -151,6 +153,8 @@ public void VerifyScreenshotOrSetException(
151153
/// </summary>
152154
/// <param name="name">Optional name for the screenshot. If not provided, a default name will be used.</param>
153155
/// <param name="retryDelay">Optional delay between retry attempts when verification fails.</param>
156+
/// <param name="cropLeft">Number of pixels to crop from the left of the screenshot.</param>
157+
/// <param name="cropRight">Number of pixels to crop from the right of the screenshot.</param>
154158
/// <param name="cropTop">Number of pixels to crop from the top of the screenshot.</param>
155159
/// <param name="cropBottom">Number of pixels to crop from the bottom of the screenshot.</param>
156160
/// <param name="tolerance">Tolerance level for image comparison as a percentage from 0 to 100.</param>
@@ -179,6 +183,8 @@ public void VerifyScreenshotOrSetException(
179183
public void VerifyScreenshot(
180184
string? name = null,
181185
TimeSpan? retryDelay = null,
186+
int cropLeft = 0,
187+
int cropRight = 0,
182188
int cropTop = 0,
183189
int cropBottom = 0,
184190
double tolerance = 0.0 // Add tolerance parameter (0.05 = 5%)
@@ -316,16 +322,24 @@ but both can happen.
316322
TestDevice.iOS => 40,
317323
_ => 0,
318324
};
319-
325+
326+
// Cropping from the left or right can be applied for any platform using the user-specified crop values.
327+
// The default values are set based on the platform, but the final cropping is determined by the parameters passed in.
328+
// This allows cropping of UI elements (such as navigation bars or home indicators) for any platform as needed.
329+
int cropFromLeft = 0;
330+
int cropFromRight = 0;
331+
332+
cropFromLeft = cropLeft > 0 ? cropLeft : cropFromLeft;
333+
cropFromRight = cropRight > 0 ? cropRight : cropFromRight;
320334
cropFromTop = cropTop > 0 ? cropTop : cropFromTop;
321335
cropFromBottom = cropBottom > 0 ? cropBottom : cropFromBottom;
322336

323-
if (cropFromTop > 0 || cropFromBottom > 0)
337+
if (cropFromLeft > 0 || cropFromRight > 0 || cropFromTop > 0 || cropFromBottom > 0)
324338
{
325339
IImageEditor imageEditor = _imageEditorFactory.CreateImageEditor(actualImage);
326340
(int width, int height) = imageEditor.GetSize();
327341

328-
imageEditor.Crop(0, cropFromTop, width, height - cropFromTop - cropFromBottom);
342+
imageEditor.Crop(cropFromLeft, cropFromTop, width - cropFromLeft - cropFromRight, height - cropFromTop - cropFromBottom);
329343

330344
actualImage = imageEditor.GetUpdatedImage();
331345
}

0 commit comments

Comments
 (0)