Esempio n. 1
0
        /// <summary>
        /// Compares images pixel by pixel with a specified tolerance defined by PixelToleranceCount and PixelColorToleranceCount
        /// </summary>
        public bool Compare(BrowserPattern browserPattern, byte[] screenshot, out string resultMessage)
        {
            if (browserPattern == null)
            {
                throw new ArgumentNullException(nameof(browserPattern));
            }

            if (screenshot == null)
            {
                throw new ArgumentNullException(nameof(screenshot));
            }

            var pattern          = browserPattern.PatternScreenshot.Image;
            var blindRegions     = browserPattern.GetAllBlindRegions();
            var patternBitmap    = ImageHelpers.ApplyBlindRegions(pattern, blindRegions);
            var screenshotBitmap = ImageHelpers.ApplyBlindRegions(screenshot, blindRegions);

            if (patternBitmap == null)
            {
                resultMessage = "\r\n+---patternBitmap is null";
                return(false);
            }

            if (screenshotBitmap == null)
            {
                resultMessage = "\r\n+---screenshotBitmap is null";
                return(false);
            }

            if (!AreBitmapsOfEqualSize(patternBitmap, screenshotBitmap))
            {
                resultMessage = "\r\n+---Different sizes of bitmaps" +
                                $"\r\n+---Pattern size: {patternBitmap.Size}" +
                                $"\r\n+---Screenshot size: {screenshotBitmap.Size}";

                return(false);
            }

            var numberOfUnmatchedPixels = CountUnmatchedPixels(patternBitmap, screenshotBitmap);

            var percentOfUnmatchedPixels = (numberOfUnmatchedPixels * 100.0) / (patternBitmap.Width * patternBitmap.Height);
            var areMatchedByPixelCount   = numberOfUnmatchedPixels <= ComparisonParameters.PixelToleranceCount;
            var areMatchedByAllowedMaximumDifferenceInPercent = percentOfUnmatchedPixels <= ComparisonParameters.MaxPercentOfUnmatchedPixels;

            resultMessage = "\r\n+---" + (areMatchedByPixelCount ? "[PASS] Images match" : "[FAIL] Images do not match") +
                            $" within specified pixel tolerance: {numberOfUnmatchedPixels}" +
                            $" <= {ComparisonParameters.PixelToleranceCount}";

            resultMessage += $"\r\n+---" + (areMatchedByAllowedMaximumDifferenceInPercent ? "[PASS] Images match" : "[FAIL] Images do not match") +
                             " within specified percent of tolerance: " +
                             $"{percentOfUnmatchedPixels.ToString("0.######", CultureInfo.InvariantCulture)}%" +
                             " <= " +
                             $"{ComparisonParameters.MaxPercentOfUnmatchedPixels.ToString("0.######", CultureInfo.InvariantCulture)}%";

            return(areMatchedByPixelCount && areMatchedByAllowedMaximumDifferenceInPercent);
        }
        public bool Compare(BrowserPattern browserPattern, byte[] screenshot, out string resultMessage)
        {
            if (browserPattern == null)
            {
                throw new ArgumentNullException(nameof(browserPattern));
            }

            if (screenshot == null)
            {
                throw new ArgumentNullException(nameof(screenshot));
            }

            var blindRegions = browserPattern.GetAllBlindRegions();

            var screenshotHash = ImageHelpers.ComputeHash(screenshot, blindRegions);
            var areMatched     = screenshotHash == browserPattern.PatternScreenshot.Hash;

            resultMessage = areMatched ? "Hashes match" : "Hashes do not match";

            return(areMatched);
        }