コード例 #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 UIElement.</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)
        {
            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;
            }
        }
コード例 #2
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, AutomationElement element, Action <Exception, Bitmap> onFail)
        {
            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))
                        {
                            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;
            }
        }
コード例 #3
0
        public static void AreEqual(Bitmap expected, Bitmap actual, Action <Bitmap> onFail)
        {
            if (expected.Size != actual.Size)
            {
                if (Debugger.IsAttached)
                {
                    ImageDiffWindow.Show(expected, actual);
                }

                onFail(actual);
                throw AssertException.Create(
                          "Sizes did not match\r\n" +
                          $"Expected: {expected.Size}\r\n" +
                          $"Actual:   {actual.Size}");
            }

            for (var x = 0; x < expected.Size.Width; x++)
            {
                for (var y = 0; y < expected.Size.Height; y++)
                {
                    if (expected.GetPixel(x, y).Name == actual.GetPixel(x, y).Name)
                    {
                        continue;
                    }

                    if (Debugger.IsAttached)
                    {
                        ImageDiffWindow.Show(expected, actual);
                    }

                    onFail(actual);
                    throw AssertException.Create("Images do not match.");
                }
            }
        }
コード例 #4
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>
        public static void AreEqual(string fileName, UiElement element)
        {
            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))
                        {
                            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 InvalidOperationException($"Not handling OnFail {OnFail}");
                            }
                        }
                    }
                }
            }
            else
            {
                Capture.ElementToFile(element, TempFileName(fileName));
                throw AssertException.Create($"Did not find a file nor resource named {fileName}");
            }
        }
コード例 #5
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 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}");
            }
        }