Exemplo n.º 1
0
        /// <summary>
        /// Takes a screenshot and crops it to the specified target element
        /// </summary>
        /// <param name="driver"></param>
        /// <param name="targetElement">The element to crop to</param>
        /// <param name="fullSavePath">Destination save path</param>
        public static void TakeScreenshotAndCropToElement(IWebDriver driver, IWebElement targetElement, string fullSavePath)
        {
            try
            {
                Screenshot screenshot = driver.TakeScreenshot();

                using (var image = Image.FromStream(new MemoryStream(screenshot.AsByteArray)))
                {
                    var coords = new Rectangle(targetElement.Location, targetElement.Size);
                    using (var newImage = ImagingUtilities.CropToSelection(image, coords))
                    {
                        ImagingUtilities.SaveImage(newImage, fullSavePath.SanitisePath());
                    }
                }
            }
            catch (Exception e)
            {
                LogX.Error.Write("An error occurred while taking screenshot. {0}", e.ToString());
            }
        }
Exemplo n.º 2
0
 /// <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());
     }
 }