Inheritance: IPersist
Exemple #1
0
        /// <summary>
        /// Compares the specified pages and returns an object detailing the differences.
        /// </summary>
        /// <param name="pageA">The first page.</param>
        /// <param name="pageB">The second page.</param>
        /// <returns>An object of type Comparison.</returns>
        private Comparison ComputeDiff(Scrape pageA, Scrape pageB)
        {
            Comparison comparison;

            using (Image pageScreenshotA = ImageUtil.Base64ToImage(pageA.Screenshot), pageScreenshotB = ImageUtil.Base64ToImage(pageB.Screenshot))
            {
                // Html diff
                var htmlDiff = GetHtmlDiffReport(pageA.Html, pageB.Html);

                // Pixel diff
                var pixelDiff = PixelDiff(pageScreenshotA, pageScreenshotB);

                // Match up elements
                var largestDiagonal = (decimal)Math.Max(
                        Hypotenuse(pageScreenshotA.Width, pageScreenshotA.Height),
                        Hypotenuse(pageScreenshotB.Width, pageScreenshotB.Height));
                CorrespondingElements(pageA, pageB, largestDiagonal);

                var matches = pageA.Elements.Where(e => e.CorrespondingScrapedElement != null).ToArray();

                // Get changes
                var changedItems = GetChanges(matches, pageScreenshotA, pageScreenshotB, pageA, pageB).ToArray();

                var unchanged = matches.Count(m => !m.HasChanges);
                var changed = matches.Where(m => m.HasChanges).ToArray();
                var deleted = pageA.Elements.Where(e => e.CorrespondingScrapedElement == null).ToArray();
                var added = pageB.Elements.Where(e => e.CorrespondingScrapedElement == null).ToArray();

                var removedItems = deleted.Select(e => GetElementData(pageScreenshotA, e, pageA)).ToArray();
                var addedItems = added.Select(e => GetElementData(pageScreenshotB, e, pageB)).ToArray();

                // Compute overall percentages
                var total = (added.Length + deleted.Length + unchanged + changed.Length) * 100;
                var addedDeleted = (added.Length + deleted.Length) * 100;

                var cssChangePercentage = TotalChangePercentage(total, changedItems.Sum(i => i.CssPercentageChange), addedDeleted);
                var textChangePercentage = TotalChangePercentage(total, changedItems.Sum(i => i.TextPercentageChange), addedDeleted);
                var overallElementPositionChangePercentage = TotalChangePercentage(total, changedItems.Sum(i => i.LocationPercentageChange), addedDeleted);
                var htmlPercentChanged = TotalChangePercentage(total, changedItems.Sum(i => i.HtmlPercentageChange), addedDeleted);

                // Create Added/Changed elements and Deleted elements images
                var outlinedImages = OutlinedImagesAsBase64(pageScreenshotA, pageScreenshotB, added, deleted, changedItems).ToArray();

                var scrapeHybridA = AssembleScrapeHybrid(pageA, true);
                var scrapeHybridB = AssembleScrapeHybrid(pageB, false);

                comparison = new Comparison
                {
                    Scrapes = new[] { scrapeHybridA, scrapeHybridB },
                    Result = new PageResult
                        {
                            HtmlDiff       = htmlDiff,
                            Pixels         = pixelDiff,
                            ChangedItems   = changedItems,
                            AddedItems     = addedItems,
                            RemovedItems   = removedItems,
                            UnchangedItems = unchanged,
                            CssChangePercentage  = cssChangePercentage,
                            TextChangePercentage = textChangePercentage,
                            OverallElementPositionChangePercentage = overallElementPositionChangePercentage,
                            Html = new HtmlResult { PercentChanged = htmlPercentChanged, Images = outlinedImages },
                        }
                };
            }

            return comparison;
        }
Exemple #2
0
        public Comparison FakeComparison(FakeComparisonParams fakeComparisonParams)
        {
            var hybrid1 = new ScrapeHybrid();
            var hybrid2 = new ScrapeHybrid();
            var result = new PageResult();
            if (fakeComparisonParams.ScrapeHybrids == null)
            {
                var idString = new StringAsReference {Value = "a1b2c3d4e5f6a7b8c9d1e"};
                var htmlString = new StringAsReference {Value = ""};

                hybrid1 = new ScrapeHybrid
                {
                    ExcludeJquerySelector = @"''",
                    IncludeJquerySelector = @"'body*'",
                    Script                = "",
                    BoundingRectangle     = new Rectangle(0, 0, 0, 0),
                    Description           = "",
                    IdString              = idString,
                    Path                  = null,
                    Resources             = null,
                    Html                  = htmlString,
                    Url                   = "http://www.google.com",
                    Screenshot            = null,
                    ViewportSize          = new Size(800, 600),
                    Browser               = "Chrome",
                    BrowserVersion        = "10",
                    TimeStamp             = DateTime.Now,
                    Platform              = "Windows",
                    Cookies               = null
                };
                hybrid2 = new ScrapeHybrid
                {
                    ExcludeJquerySelector = @"''",
                    IncludeJquerySelector = @"'body*'",
                    Script                = "",
                    BoundingRectangle     = new Rectangle(10, 10, 10, 10),
                    Description           = "",
                    IdString              = idString,
                    Path                  = null,
                    Resources             = null,
                    Html                  = htmlString,
                    Url                   = "http://www.ancestry.com",
                    Screenshot            = null,
                    ViewportSize          = new Size(800, 600),
                    Browser               = "FireFox",
                    BrowserVersion        = "10",
                    TimeStamp             = DateTime.Now.AddDays(10),
                    Platform              = "Windows",
                    Cookies               = null
                };
            }

            if (fakeComparisonParams.Result == null)
            {
                result = FakePageResult(new FakeComparisonResult());
            }
            var fakedComparison = new Comparison
            {
                Scrapes = fakeComparisonParams.ScrapeHybrids ?? new[] { hybrid1, hybrid2 },
                Id      = fakeComparisonParams.ObjectId      ?? new ObjectId("555b77b99b111d2aa4b33333"),
                Path    = fakeComparisonParams.Path          ?? new StringAsReference(),
                Result  = fakeComparisonParams.Result        ?? result
            };

            return fakedComparison;
        }