/// <summary> /// Compare Capture.Rectangle(element.Bounds) to the expected image. /// </summary> /// <param name="fileName">Can be a full file name relative filename or the name of a resource.</param> /// <param name="element">The UIElement.</param> /// <param name="onFail">Useful for saving the actual image on error for example.</param> public static void AreEqual(string fileName, System.Windows.UIElement element, Action <Exception, Bitmap> onFail) { if (TryGetStream(fileName, Assembly.GetCallingAssembly(), out var stream)) { using (stream) { using (var expected = (Bitmap)Image.FromStream(stream)) { using (var actual = element.ToBitmap(expected.Size(), expected.PixelFormat())) { try { AreEqual(expected, actual); } catch (Exception e) { onFail(e, actual); throw; } } } } } else { var exception = AssertException.Create($"Did not find a file nor resource named {fileName}"); using (var actual = element.ToBitmap(element.RenderSize, GetPixelFormat(fileName))) { onFail(exception, actual); } throw exception; } }
/// <summary> /// Compare Capture.Rectangle(element.Bounds) to the expected image. /// </summary> /// <param name="fileName">Can be a full file name relative filename or the name of a resource.</param> /// <param name="element">The UIElement.</param> public static void AreEqual(string fileName, System.Windows.UIElement element) { if (TryGetStream(fileName, Assembly.GetCallingAssembly(), out var stream)) { using (stream) { using (var expected = (Bitmap)Image.FromStream(stream)) { switch (OnFail) { case OnFail.DoNothing: AreEqual(expected, element); break; case OnFail.SaveImageToTemp: using (var actual = element.ToBitmap(expected.Size(), expected.PixelFormat())) { AreEqual(expected, actual, (bitmap) => bitmap.Save(TempFileName(fileName), GetImageFormat(fileName))); } break; default: throw new InvalidOperationException($"Not handling OnFail {OnFail}"); } } } } else { using (var actual = element.ToBitmap(element.RenderSize, GetPixelFormat(fileName))) { actual.Save(TempFileName(fileName), GetImageFormat(fileName)); } throw AssertException.Create($"Did not find a file nor resource named {fileName}"); } }
public static void AreEqual(Bitmap expected, System.Windows.UIElement actual) { using (var actualBmp = actual.ToBitmap(expected.Size(), expected.PixelFormat())) { if (Equal(expected, actualBmp)) { return; } var start = DateTime.Now; while (!Retry.IsTimeouted(start, RetryTime)) { if (Equal(expected, actualBmp)) { return; } Wait.For(Retry.PollInterval); } AreEqual(expected, actualBmp); } }