Ejemplo n.º 1
0
        internal async Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format)
        {
            if (bytes == null)
            {
                var pixels = await bmp.GetPixelsAsync().AsTask().ConfigureAwait(false);

                bytes = pixels.ToArray();
            }

            var f = format switch
            {
                ScreenshotFormat.Jpeg => BitmapEncoder.JpegEncoderId,
                _ => BitmapEncoder.PngEncoderId
            };

            var ms = new InMemoryRandomAccessStream();

            var encoder = await BitmapEncoder.CreateAsync(f, ms).AsTask().ConfigureAwait(false);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)Width, (uint)Height, 96, 96, bytes);
            await encoder.FlushAsync().AsTask().ConfigureAwait(false);

            return(ms.AsStreamForRead());
        }
    }
Ejemplo n.º 2
0
        public void WithSeleniumReporting_WithConfiguration_ShouldReturnCorrectValue(
            [Modest] Actor actor,
            IActor iactor,
            SeleniumReportingConfiguration configuration,
            IObserver <string>[] observers,
            ICanNotify canNotify,
            ITakeScreenshotStrategy takeScreenshotStrategy,
            ScreenshotFormat format)
        {
            var actual = ActorExtensions.WithSeleniumReporting(
                actor,
                configuration,
                out var actualSeleniumReporter
                );

            TestWithSeleniumReporting(actualSeleniumReporter,
                                      actual,
                                      actor,
                                      iactor,
                                      configuration.ScreenshotDirectory,
                                      configuration.ScreenshotNameOrFormat,
                                      observers,
                                      canNotify,
                                      takeScreenshotStrategy,
                                      format);
        }
Ejemplo n.º 3
0
        public static ImageCodecInfo getImageCodec(ScreenshotFormat format)
        {
            ImageCodecInfo[] formats =
                ImageCodecInfo.GetImageDecoders();

            String expectedMime;
            switch (format)
            {
                case ScreenshotFormat.BMP:
                    expectedMime = "image/bmp";
                    break;
                case ScreenshotFormat.JPEG:
                    expectedMime = "image/jpeg";
                    break;
                case ScreenshotFormat.TIFF:
                    expectedMime = "image/tiff";
                    break;
                default:
                    expectedMime = "image/png";
                    break;
            }

            ImageCodecInfo result = null;
            for (int i = 0; i < formats.Length; i++)
            {
                if (formats[i].MimeType.ToLower() == expectedMime)
                {
                    result = formats[i];
                    break;
                }
            }

            return result;
        }
Ejemplo n.º 4
0
 public static EncoderParameters getParameters(int jpegQuality, ScreenshotFormat format)
 {
     EncoderParameters result;
     switch(format)
     {
         case ScreenshotFormat.BMP:
             result = new EncoderParameters(1);
             result.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24);
             break;
         case ScreenshotFormat.JPEG:
             result = new EncoderParameters(1);
             result.Param[0] = new EncoderParameter(Encoder.Quality, jpegQuality);
             break;
         case ScreenshotFormat.TIFF:
             result = new EncoderParameters(2);
             EncoderParameter parameter = 
                 new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
             result.Param[0] = parameter;
             parameter = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.LastFrame);
             result.Param[1] = parameter;
             break;
         default:
             result = new EncoderParameters(2);
             result.Param[0] = new EncoderParameter(Encoder.Compression,(long)EncoderValue.CompressionCCITT4);
             result.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)24);
             break;
     }
     return result;
 }
Ejemplo n.º 5
0
        public static EncoderParameters getParameters(int jpegQuality, ScreenshotFormat format)
        {
            EncoderParameters result;

            switch (format)
            {
            case ScreenshotFormat.BMP:
                result          = new EncoderParameters(1);
                result.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24);
                break;

            case ScreenshotFormat.JPEG:
                result          = new EncoderParameters(1);
                result.Param[0] = new EncoderParameter(Encoder.Quality, jpegQuality);
                break;

            case ScreenshotFormat.TIFF:
                result = new EncoderParameters(2);
                EncoderParameter parameter =
                    new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
                result.Param[0] = parameter;
                parameter       = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.LastFrame);
                result.Param[1] = parameter;
                break;

            default:
                result          = new EncoderParameters(2);
                result.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                result.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)24);
                break;
            }
            return(result);
        }
Ejemplo n.º 6
0
        Task PlatformCopyToAsync(Stream destination, ScreenshotFormat format, int quality)
        {
            var f = ToCompressFormat(format);

            bmp.Compress(f, quality, destination);
            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
        Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format, int quality)
        {
            var result = new MemoryStream();

            PlatformCopyToAsync(result, format, quality);
            result.Position = 0;
            return(Task.FromResult <Stream>(result));
        }
Ejemplo n.º 8
0
        async Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format, int quality)
        {
            var ms = new InMemoryRandomAccessStream();

            await EncodeAsync(format, ms).ConfigureAwait(false);

            return(ms.AsStreamForRead());
        }
 /// <summary>
 /// Save the screeshots in the specified format
 /// </summary>
 /// <param name="screenshotFormat">The format</param>
 /// <returns></returns>
 public SeleniumReportingConfiguration WithScreenshotFormat(ScreenshotFormat screenshotFormat)
 {
     return(new SeleniumReportingConfiguration(ScreenshotDirectory,
                                               ScreenshotNameOrFormat,
                                               _canNotify,
                                               TextOutputObservers,
                                               TakeScreenshotStrategy,
                                               screenshotFormat));
 }
Ejemplo n.º 10
0
        public void Equals_WithDifferentFormat_ReturnsFalse(ScreenshotFormat sut)
        {
            if (sut.Format == ScreenshotImageFormat.Bmp)
            {
                return;
            }

            Assert.False(((object)ScreenshotFormat.Bmp).Equals(sut));
        }
Ejemplo n.º 11
0
        public void GetHashCode_WithDifferentFormat_ReturnDifferentValue(ScreenshotFormat sut)
        {
            if (sut.Format == ScreenshotImageFormat.Bmp)
            {
                return;
            }

            Assert.NotEqual(ScreenshotFormat.Bmp.GetHashCode(), sut.GetHashCode());
        }
Ejemplo n.º 12
0
        async Task EncodeAsync(ScreenshotFormat format, IRandomAccessStream ms)
        {
            var f = ToBitmapEncoder(format);

            var encoder = await BitmapEncoder.CreateAsync(f, ms).AsTask().ConfigureAwait(false);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)Width, (uint)Height, 96, 96, bytes);
            await encoder.FlushAsync().AsTask().ConfigureAwait(false);
        }
Ejemplo n.º 13
0
        public async Task <byte[]> TakeScreenshotAsync(ScreenshotFormat format, ScreenshotOptions options, Viewport viewport)
        {
            var response = await _session.SendAsync(new PageScreenshotRequest
            {
                MimeType = format == ScreenshotFormat.Jpeg ? ScreenshotMimeType.ImageJpeg : ScreenshotMimeType.ImagePng,
                FullPage = options.FullPage,
                Clip     = options.Clip,
            }).ConfigureAwait(false);

            return(Convert.FromBase64String(response.Data));
        }
Ejemplo n.º 14
0
        internal Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format)
        {
            var data = format switch
            {
                ScreenshotFormat.Jpeg => uiImage.AsJPEG(),
                _ => uiImage.AsPNG()
            };

            return(Task.FromResult(data.AsStream()));
        }
    }
Ejemplo n.º 15
0
        public void WithScreenshotMethods_ShouldSetTheCorrectFormat(ScreenshotFormat format, MethodInfo method)
        {
            // arrange
            var fixture       = new Fixture().Customize(new AutoMoqCustomization());
            var configuration = fixture.Create <SeleniumReportingConfiguration>();

            // act
            var actual = (SeleniumReportingConfiguration)method.Invoke(null, new object[] { configuration });

            // assert
            Assert.Equal(format, actual.ScreenshotFormat);
        }
Ejemplo n.º 16
0
        internal Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format)
        {
            var f = format switch
            {
                ScreenshotFormat.Jpeg => Bitmap.CompressFormat.Jpeg,
                _ => Bitmap.CompressFormat.Png,
            };

            var result = new MemoryStream(bmp.AsImageBytes(f, 100)) as Stream;

            return(Task.FromResult(result));
        }
Ejemplo n.º 17
0
        public async Task <byte[]> TakeScreenshotAsync(ScreenshotFormat format, ScreenshotOptions options, Viewport viewport)
        {
            await Client.SendAsync(new PageBringToFrontRequest()).ConfigureAwait(false);

            var clip = options.Clip?.ToViewportProtocol();

            var result = await Client.SendAsync(new PageCaptureScreenshotRequest
            {
                Format  = format.ToStringFormat(),
                Quality = options.Quality,
                Clip    = clip,
            }).ConfigureAwait(false);

            return(result.Data);
        }
Ejemplo n.º 18
0
        public async Task RendersAsImage(ScreenshotFormat type)
        {
            var view = new TStub()
            {
                Height = 100,
                Width  = 100,
            };

            var result = await GetValueAsync(view, handler => handler.VirtualView.CaptureAsync());

            Assert.NotNull(result);

            using var stream = await result.OpenReadAsync(type);

            Assert.True(stream.Length > 0);
        }
Ejemplo n.º 19
0
        internal async Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format)
        {
            var stream = new MemoryStream();

            var f = format switch
            {
                ScreenshotFormat.Jpeg => Bitmap.CompressFormat.Jpeg,
                _ => Bitmap.CompressFormat.Png,
            };

            await bmp.CompressAsync(f, 100, stream).ConfigureAwait(false);

            stream.Position = 0;

            return(stream);
        }
Ejemplo n.º 20
0
        internal Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format)
        {
            var content = format switch
            {
                ScreenshotFormat.Jpeg => ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatDescription == "JPEG") ?? throw new InvalidOperationException("No JPEG encoder found"),
                      ScreenshotFormat.Png => ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatDescription == "PNG") ?? throw new InvalidOperationException("No PNG encoder found"),
                            _ => throw new NotImplementedException()
            };
            var encoderParameters = new EncoderParameters();

            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 50L);
            var stream = new MemoryStream();

            bmp.Save(stream, content, encoderParameters);
            return(Task.FromResult <Stream>(stream));
        }
    }
Ejemplo n.º 21
0
        private async Task <byte[]> ScreenshotAsync(ScreenshotFormat format, ScreenshotOptions options, Viewport viewport)
        {
            bool shouldSetDefaultBackground = options.OmitBackground && format == ScreenshotFormat.Png;

            if (shouldSetDefaultBackground)
            {
                await _page.Delegate.SetBackgroundColorAsync(Color.FromArgb(0, 0, 0, 0)).ConfigureAwait(false);
            }

            byte[] result = await _page.Delegate.TakeScreenshotAsync(format, options, viewport).ConfigureAwait(false);

            if (shouldSetDefaultBackground)
            {
                await _page.Delegate.SetBackgroundColorAsync().ConfigureAwait(false);
            }

            return(result);
        }
 private SeleniumReportingConfiguration(string screenshotDirectory,
                                        string screenshotNameOrFormat,
                                        ICanNotify canNotify,
                                        ImmutableArray <IObserver <string> > textOutputObservers,
                                        ITakeScreenshotStrategy takeScreenshotStrategy,
                                        ScreenshotFormat screenshotFormat)
 {
     if (string.IsNullOrEmpty(screenshotDirectory))
     {
         throw new ArgumentNullException(nameof(screenshotDirectory));
     }
     if (string.IsNullOrEmpty(screenshotNameOrFormat))
     {
         throw new ArgumentNullException(nameof(screenshotNameOrFormat));
     }
     ScreenshotDirectory    = screenshotDirectory;
     ScreenshotNameOrFormat = screenshotNameOrFormat;
     _canNotify             = canNotify;
     TextOutputObservers    = textOutputObservers;
     TakeScreenshotStrategy = takeScreenshotStrategy;
     ScreenshotFormat       = screenshotFormat ?? throw new ArgumentNullException(nameof(screenshotFormat));
 }
Ejemplo n.º 23
0
        public static ImageCodecInfo getImageCodec(ScreenshotFormat format)
        {
            ImageCodecInfo[] formats =
                ImageCodecInfo.GetImageDecoders();

            string expectedMime;

            switch (format)
            {
            case ScreenshotFormat.BMP:
                expectedMime = "image/bmp";
                break;

            case ScreenshotFormat.JPEG:
                expectedMime = "image/jpeg";
                break;

            case ScreenshotFormat.TIFF:
                expectedMime = "image/tiff";
                break;

            default:
                expectedMime = "image/png";
                break;
            }

            ImageCodecInfo result = null;

            for (int i = 0; i < formats.Length; i++)
            {
                if (formats[i].MimeType.ToLower() == expectedMime)
                {
                    result = formats[i];
                    break;
                }
            }

            return(result);
        }
 Task PlatformCopyToAsync(Stream destination, ScreenshotFormat format, int quality) =>
 throw ExceptionUtils.NotSupportedOrImplementedException;
Ejemplo n.º 25
0
 public Screenshot(ScreenshotFormat format, int width, int height, in Intrinsic intrinsic, in Pose cameraPose,
Ejemplo n.º 26
0
 private void ShotGetFormat(out ScreenshotFormat format)
 {
     String ext;
     ShotGetFormat(out format, out ext);
 }
Ejemplo n.º 27
0
 private void ShotGetFormat(out ScreenshotFormat format, out String extension)
 {
     switch (ImgFormat.SelectedIndex)
     {
         case 1:
             format = ScreenshotFormat.BMP;
             extension = ".bmp";
             break;
         case 2:
             format = ScreenshotFormat.JPEG;
             extension = ".jpg";
             SettingsFile.SetValue("Screenshots", "JPEGQuality", JPGQual.Value);
             break;
         case 3:
             format = ScreenshotFormat.TIFF;
             extension = ".tif";
             break;
         default:
             format = ScreenshotFormat.PNG;
             extension = ".png";
             break;
     }
 }
Ejemplo n.º 28
0
 internal Task <Stream> PlatformOpenReadAsync(ScreenshotFormat format) =>
 throw ExceptionUtils.NotSupportedOrImplementedException;
Ejemplo n.º 29
0
 static Bitmap.CompressFormat ToCompressFormat(ScreenshotFormat format) =>
 format switch
 {
Ejemplo n.º 30
0
 public Task <Stream> OpenReadAsync(ScreenshotFormat format = ScreenshotFormat.Png) =>
 PlatformOpenReadAsync(format);
Ejemplo n.º 31
0
 public Task CopyToAsync(Stream destination, ScreenshotFormat format = ScreenshotFormat.Png, int quality = 100)
 => PlatformCopyToAsync(destination, format, quality);
Ejemplo n.º 32
0
 public Task <Stream> OpenReadAsync(ScreenshotFormat format = ScreenshotFormat.Png, int quality = 100)
 => PlatformOpenReadAsync(format, quality);
Ejemplo n.º 33
0
        private async Task <byte[]> PerformScreenshot(ScreenshotFormat format, ScreenshotOptions options)
        {
            Viewport overridenViewport = null;
            var      viewport          = _page.Viewport;
            Viewport viewportSize      = null;

            if (viewport == null)
            {
                viewportSize = await _page.EvaluateAsync <Viewport>(@"() => {
                  if (!document.body || !document.documentElement)
                    return;
                  return {
                    width: Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
                    height: Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
                  };
                }").ConfigureAwait(false);

                if (viewportSize == null)
                {
                    throw new PlaywrightSharpException(ScreenshotDuringNavigationError);
                }
            }

            if (options.FullPage && !_page.Delegate.CanScreenshotOutsideViewport())
            {
                var fullPageRect = await _page.EvaluateAsync <Size>(@"() => {
                  if (!document.body || !document.documentElement)
                    return null;
                  return {
                    width: Math.max(
                        document.body.scrollWidth, document.documentElement.scrollWidth,
                        document.body.offsetWidth, document.documentElement.offsetWidth,
                        document.body.clientWidth, document.documentElement.clientWidth
                    ),
                    height: Math.max(
                        document.body.scrollHeight, document.documentElement.scrollHeight,
                        document.body.offsetHeight, document.documentElement.offsetHeight,
                        document.body.clientHeight, document.documentElement.clientHeight
                    ),
                  };
                }").ConfigureAwait(false);

                if (fullPageRect == null)
                {
                    throw new PlaywrightSharpException(ScreenshotDuringNavigationError);
                }

                overridenViewport = viewport != null?viewport.Clone() : new Viewport();

                overridenViewport.Height = fullPageRect.Height;
                overridenViewport.Width  = fullPageRect.Width;

                await _page.SetViewportAsync(overridenViewport).ConfigureAwait(false);
            }
            else if (options.Clip != null)
            {
                options.Clip = TrimClipToViewport(viewport, options.Clip);
            }

            byte[] result = await ScreenshotAsync(format, options, overridenViewport ?? viewport).ConfigureAwait(false);

            if (overridenViewport != null)
            {
                if (viewport != null)
                {
                    await _page.SetViewportAsync(viewport).ConfigureAwait(false);
                }
                else
                {
                    await _page.Delegate.ResetViewportAsync(viewportSize).ConfigureAwait(false);
                }
            }

            return(result);
        }
Ejemplo n.º 34
0
        private async Task <byte[]> PerformScreenshotElementAsync(ElementHandle handle, ScreenshotFormat format, ScreenshotOptions options)
        {
            Viewport overridenViewport = null;
            Viewport viewportSize      = null;

            var maybeBoundingBox = await _page.Delegate.GetBoundingBoxForScreenshotAsync(handle).ConfigureAwait(false);

            if (maybeBoundingBox == null)
            {
                throw new PlaywrightSharpException("Node is either not visible or not an HTMLElement");
            }

            var boundingBox = maybeBoundingBox;

            if (boundingBox.Width == 0)
            {
                throw new PlaywrightSharpException("Node has 0 width.");
            }

            if (boundingBox.Height == 0)
            {
                throw new PlaywrightSharpException("Node has 0 height.");
            }

            boundingBox = EnclosingIntRect(boundingBox);

            var viewport = _page.Viewport;

            if (!_page.Delegate.CanScreenshotOutsideViewport())
            {
                if (viewport == null)
                {
                    var maybeViewportSize = await _page.EvaluateAsync <Viewport>(@"() => {
                        if (!document.body || !document.documentElement)
                            return;
                        return {
                            width: Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
                            height: Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
                        };
                    }").ConfigureAwait(false);

                    if (maybeViewportSize == null)
                    {
                        throw new PlaywrightSharpException(ScreenshotDuringNavigationError);
                    }

                    viewportSize = maybeViewportSize;
                }
                else
                {
                    viewportSize = viewport;
                }

                if (boundingBox.Width > viewportSize.Width || boundingBox.Height > viewportSize.Height)
                {
                    overridenViewport        = (viewport ?? viewportSize).Clone();
                    overridenViewport.Width  = Convert.ToInt32(Math.Max(viewportSize.Width, boundingBox.Width));
                    overridenViewport.Height = Convert.ToInt32(Math.Max(viewportSize.Height, boundingBox.Height));
                    await _page.SetViewportAsync(overridenViewport).ConfigureAwait(false);
                }

                await handle.ScrollIntoViewIfNeededAsync().ConfigureAwait(false);

                maybeBoundingBox = await _page.Delegate.GetBoundingBoxForScreenshotAsync(handle).ConfigureAwait(false);

                if (maybeBoundingBox == null)
                {
                    throw new PlaywrightSharpException("Node is either not visible or not an HTMLElement");
                }

                boundingBox = EnclosingIntRect(maybeBoundingBox !);
            }

            if (overridenViewport == null)
            {
                options.Clip = boundingBox;
            }

            byte[] result = await ScreenshotAsync(format, options, overridenViewport ?? viewport).ConfigureAwait(false);

            if (overridenViewport != null)
            {
                if (viewport != null)
                {
                    await _page.SetViewportAsync(viewport).ConfigureAwait(false);
                }
                else
                {
                    await _page.Delegate.ResetViewportAsync(viewportSize).ConfigureAwait(false);
                }
            }

            return(result);
        }