1

On VS Code, I would like to set up UI tests on the app's visual by taking the app's screenshot elements. However, when the element for example "button" is not visible on the page you need to scroll down to see it, when creating a screenshot, a "system out of memory" error is displayed.

This is an code extract in c#.

 public void TakeScreenshotOfElementCheckbox()
        {
            string fileName = @"C:\Users\...\testing\Screenshots\checkbox.png";

            var screenshotDriver = driver as ITakesScreenshot;
            Screenshot screenshot = screenshotDriver.GetScreenshot();
            var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

            IWebElement element = driver.FindElement(By.XPath(".//*[@class='footer-convenant']"));
            var cropArea = new Rectangle(element.Location, element.Size);
            var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
            bitmap.Save(fileName);
       }

More details about Dispose :

I'm trying but I think I'm using the wrong way. using should be used how? Can I have an example?

string fileName = @"C:\Users\...\testing\Screenshots\Checkbox\checkbox.png";

            using (var image = new Bitmap(fileName))
            {
                var screenshotDriver = driver as ITakesScreenshot;
                Screenshot screenshot = screenshotDriver.GetScreenshot();
                //Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

                // 2. Get screenshot of specific element
                element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
                Rectangle rect = new Rectangle(element.Location, element.Size);
                var bitmap = image.Clone(rect, image.PixelFormat);
                bitmap.Save(fileName);
            }

I'm trying but I think I'm not far away. Can you show me a structure of what it could be?

 string fileName = @"C:\Users\...\testing\Screenshots\Checkbox\checkbox.png";

            using (var image = new Bitmap(fileName))
            {
                // 1. Make screenshot of all screen
                var screenshotDriver = driver as ITakesScreenshot;
                Screenshot screenshot = screenshotDriver.GetScreenshot();
                Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

                // 2. Get screenshot of specific element
                element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
                Rectangle rect = new Rectangle(element.Location, element.Size);
                using (var bitmap = image.Clone(rect, image.PixelFormat))
                {
                    bitmap.Save(fileName);
                }
            }

Or

string fileName = @"C:\Users\..\testing\Screenshots\Checkbox\checkbox.png";

            using (var image = new Bitmap(fileName))

            {
                // 1. Make screenshot of all screen
                var screenshotDriver = driver as ITakesScreenshot;
                Screenshot screenshot = screenshotDriver.GetScreenshot();
                Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

                // 2. Get screenshot of specific element
                element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
                Rectangle rect = new Rectangle(element.Location, element.Size);
                var bitmap = bmpScreen.Clone(rect, bmpScreen.PixelFormat);
                bitmap.Save(fileName);
                bitmap.Dispose();
            }
GioOps
  • 23
  • 5
  • Be sure to `Dispose` of **both** of your `Bitmap`s. – mjwills Apr 08 '19 at 21:48
  • Also confirm that `cropArea` is valid, according to the rules at https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.clone?view=netframework-4.7.2 . (i.e. check the values of `element.Location` and `element.Size`) – mjwills Apr 08 '19 at 21:51
  • Hello, so on your second attempt you have 2 bitmaps you are disposing the first one but not the second which should be disposed just after the save method. If that doesnt do the trick i would want to see the Screenshot Object. – Nick Polyderopoulos Apr 08 '19 at 22:48
  • @GioOps See my first comment. Did you `Dispose` of **both** of your `Bitmap`s? – mjwills Apr 08 '19 at 23:28
  • Actually, but sincerly i don't see how to setup the dispose with using – GioOps Apr 09 '19 at 09:35

1 Answers1

3

The Bitmap class hold unmanaged resources with the operating system that need to be released. You need to dispose all your bitmaps after you create them.

Any bitmap you create, or clone need to be disposed (after you finish with them). I suggest looking at the using statement

Provides a convenient syntax that ensures the correct use of IDisposable objects.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141