/// <summary> /// Takes a screenshot and highlights the specified target element /// </summary> /// <param name="driver"></param> /// <param name="targetElement">The element to highlight</param> /// <param name="fullSavePath">Destination save path</param> /// <param name="highlightInRed">Should the brush colour be in red or the default yellow</param> public static void TakeScreenshotAndHighlightElement(IWebDriver driver, IWebElement targetElement, string fullSavePath, bool highlightInRed = false) { try { Screenshot screenshot = driver.TakeScreenshot(); var coordinates = targetElement.GetRectangle(); using (var memoryStream = new MemoryStream(screenshot.AsByteArray)) { using (var image = Image.FromStream(memoryStream)) { using (var newImage = ImagingUtilities.HilightZone(image, coordinates, 5, highlightInRed)) { ImagingUtilities.SaveImage(newImage, fullSavePath.SanitisePath()); } } } } catch (Exception e) { LogX.Error.Write("An error occurred while taking screenshot. {0}", e.ToString()); } }
public void HilightZone_OnePxRed() { using (var newBm = new Bitmap(500, 500)) { // define the crop size var targetSize = new Rectangle(125, 125, 125, 125); // fill in the crop to a solid colour using (var newGrapics = Graphics.FromImage(newBm)) { newGrapics.CompositingQuality = CompositingQuality.HighSpeed; newGrapics.SmoothingMode = SmoothingMode.HighSpeed; newGrapics.InterpolationMode = InterpolationMode.HighQualityBicubic; SolidBrush brush = new SolidBrush(Color.Blue); SolidBrush backgroundBrush = new SolidBrush(Color.Green); newGrapics.FillRectangle(backgroundBrush, new Rectangle(0, 0, 500, 500)); newGrapics.FillRectangle(brush, targetSize); } //newBm.Save("D:\\Temp\\hilightedRed_Pre.png", ImageFormat.Png); using (var newImage = new Bitmap(ImagingUtilities.HilightZone(newBm, targetSize, 1, true))) { int incorrectPixels = 0; Color expectedColor = Color.FromArgb(255, Color.Red); //newImage.Save("D:\\Temp\\hilightedRed_post.png", ImageFormat.Png); // check each pixel along the top of the box for (var i = 0; i < 125; i++) { Color pixelColor = newImage.GetPixel(125, 125 + i); if (pixelColor != expectedColor) { incorrectPixels++; } } // check each pixel along the bottom of the box for (var i = 0; i < 125; i++) { Color pixelColor = newImage.GetPixel(250, 125 + i); if (pixelColor != expectedColor) { incorrectPixels++; } } // check each pixel along the left edge for (var i = 0; i < 125; i++) { Color pixelColor = newImage.GetPixel(125, 125 + i); if (pixelColor != expectedColor) { incorrectPixels++; } } // check each pixel along the right edge for (var i = 0; i < 125; i++) { Color pixelColor = newImage.GetPixel(250, 125 + i); if (pixelColor != expectedColor) { incorrectPixels++; } } Assert.AreEqual(0, incorrectPixels, "Number of pixels that are red is incorrect"); } } }