Beispiel #1
0
        /// <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 automation element.</param>
        /// <param name="onFail">Useful for saving the actual image on error for example.</param>
        public static void AreEqual(string fileName, UiElement element, Action <Exception, Bitmap> onFail)
        {
            WaitForStartAnimation(element);
            if (TryGetStream(fileName, Assembly.GetCallingAssembly(), out var stream))
            {
                using (stream)
                {
                    using (var expected = (Bitmap)Image.FromStream(stream))
                    {
                        if (Equal(expected, element, RetryTime))
                        {
                            return;
                        }

                        using (var actual = Capture.Rectangle(element.Bounds))
                        {
                            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 = Capture.Rectangle(element.Bounds))
                {
                    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, 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 ArgumentOutOfRangeException();
                        }
                    }
                }
            }
            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}");
            }
        }
        /// <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 automation element.</param>
        public static void AreEqual(string fileName, AutomationElement element)
        {
            WaitForStartAnimation(element);
            if (TryGetStream(fileName, Assembly.GetCallingAssembly(), out var stream))
            {
                using (stream)
                {
                    using (var expected = (Bitmap)Image.FromStream(stream))
                    {
                        using (var actual = Capture.Rectangle(element.Bounds))
                        {
                            switch (OnFail)
                            {
                            case OnFail.DoNothing:
                                AreEqual(expected, actual);
                                break;

                            case OnFail.SaveImageToTemp:
                                AreEqual(
                                    expected,
                                    actual,
                                    (bitmap) => bitmap.Save(TempFileName(fileName), GetImageFormat(fileName)));
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                    }
                }
            }
            else
            {
                Capture.ElementToFile(element, TempFileName(fileName));
                throw AssertException.Create($"Did not find a file nor resource named {fileName}");
            }
        }