private static PointF GetPixelPositionWithColor(ScreenshotInfo screenshotInfo, IAppRect boundingRect, Color expectedColor) { var bitmap = screenshotInfo.GetBitmap(); for (var x = boundingRect.X; x < boundingRect.Right; x++) { for (var y = boundingRect.Y; y < boundingRect.Bottom; y++) { var pixel = bitmap.GetPixel((int)x, (int)y); if (pixel.ToArgb() == expectedColor.ToArgb()) { return(new PointF(x, y)); } } } throw new InvalidOperationException($"Color {expectedColor} was not found."); }
private static (bool areEqual, string context) EqualityCheck( ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Bitmap actualBitmap, Rectangle actualRect, double expectedToActualScale, PixelTolerance tolerance, [CallerLineNumber] int line = 0) { using var expectedBitmap = new Bitmap(expected.File.FullName); if (expectedRect != FirstQuadrant && actualRect != FirstQuadrant) { Assert.AreEqual(expectedRect.Size, actualRect.Size, WithContext("Compare rects don't have the same size")); } if (expectedRect == FirstQuadrant && actualRect == FirstQuadrant) { var effectiveExpectedBitmapSize = new Size( (int)(expectedBitmap.Size.Width * expectedToActualScale), (int)(expectedBitmap.Size.Height * expectedToActualScale)); Assert.AreEqual(effectiveExpectedBitmapSize, actualBitmap.Size, WithContext("Screenshots don't have the same size")); } expectedRect = Normalize(expectedRect, expectedBitmap.Size); actualRect = Normalize(actualRect, actualBitmap.Size); var expectedPixels = ExpectedPixels .At(actualRect.Location) .Pixels(expectedBitmap, expectedRect) .Named(expected.StepName) .WithTolerance(tolerance); var report = GetContext(); var result = Validate(expectedPixels, actualBitmap, expectedToActualScale, report); return(result, report.ToString()); StringBuilder GetContext() => new StringBuilder() .AppendLine($"ImageAssert.AreEqual @ line {line}") .AppendLine("pixelTolerance: " + tolerance) .AppendLine($"expected: {expected?.StepName} ({expected?.File.Name} {expectedBitmap.Size}){(expectedRect == FirstQuadrant ? null : $" in {expectedRect}")}")
public void ExecuteTakeScreenshot_WhenAbilityIsWebBrowser_ShouldNotCallObserver( TakeScreenshotOnErrorStrategy sut, IActor actor, string expectedName, IObserver <ScreenshotInfo> observer) { // arrange var screenshot = new Screenshot("abcdefgskdjf"); var screenshotInfo = new ScreenshotInfo(screenshot, expectedName); var webDriver = new Mock <IWebDriver>(); webDriver.As <ITakesScreenshot>().Setup(t => t.GetScreenshot()).Returns(screenshot); var webBrowser = WebBrowser.With(webDriver.Object); // act sut.ExecuteTakeScreenshot(webBrowser, actor, () => new object(), () => expectedName, observer); // assert Mock.Get(observer).Verify(o => o.OnNext(It.IsAny <ScreenshotInfo>()), Times.Never()); }
public void Take(ScreenshotInfo screenshotInfo) { string title = $"{screenshotInfo.Number:D2} - {screenshotInfo.PageObjectFullName}{screenshotInfo.Title?.Prepend(" - ")}"; string relativeFilePath = Path.Combine(AtataContext.Current.TestNameSanitized, $"{title.SanitizeForFileName()}.png"); string absoluteFilePath = Path.Combine(ExtentContext.WorkingFolder, relativeFilePath); string targetDirectory = Path.GetDirectoryName(absoluteFilePath); if (!Directory.Exists(targetDirectory)) { Directory.CreateDirectory(targetDirectory); } screenshotInfo.Screenshot.SaveAsFile(absoluteFilePath, ScreenshotImageFormat.Png); ExtentContext.CurrentTest.Log( Status.Info, MediaEntityBuilder.CreateScreenCaptureFromPath(relativeFilePath, title).Build()); }
/// <summary> /// スクリーンショットの情報を初期化する /// </summary> private void InitializeScreenshotInfos() { // インスタンスを作成する mScreenshotInfos = new ScreenshotInfo[5]; for (int i = 0; i < mScreenshotInfos.Length; ++i) { mScreenshotInfos[i] = new ScreenshotInfo(); } int index = -1; // BMP ++index; mScreenshotInfos[index].extensionRadioButton = this.radioButtonScreenshotExtensionBmp; mScreenshotInfos[index].extensionName = "bmp"; mScreenshotInfos[index].imageFormat = System.Drawing.Imaging.ImageFormat.Bmp; // JPG ++index; mScreenshotInfos[index].extensionRadioButton = this.radioButtonScreenshotExtensionJpg; mScreenshotInfos[index].extensionName = "jpg"; mScreenshotInfos[index].imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; // PNG ++index; mScreenshotInfos[index].extensionRadioButton = this.radioButtonScreenshotExtensionPng; mScreenshotInfos[index].extensionName = "png"; mScreenshotInfos[index].imageFormat = System.Drawing.Imaging.ImageFormat.Png; // GIF ++index; mScreenshotInfos[index].extensionRadioButton = this.radioButtonScreenshotExtensionGif; mScreenshotInfos[index].extensionName = "gif"; mScreenshotInfos[index].imageFormat = System.Drawing.Imaging.ImageFormat.Gif; // TIFF ++index; mScreenshotInfos[index].extensionRadioButton = this.radioButtonScreenshotExtensionTiff; mScreenshotInfos[index].extensionName = "tiff"; mScreenshotInfos[index].imageFormat = System.Drawing.Imaging.ImageFormat.Tiff; }
private static void AreEqualImpl( ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Bitmap actualBitmap, Rectangle actualRect, double expectedToActualScale, PixelTolerance tolerance, [CallerLineNumber] int line = 0) { var(areEqual, context) = EqualityCheck(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line); if (areEqual) { Console.WriteLine(context.ToString()); } else { Assert.Fail(context.ToString()); } }
public void ExecuteTakeScreenshot_WhenAbilityIsWebBrowser_AndExecuteThrows_ShouldCallObserver( TakeScreenshotOnErrorStrategy sut, IActor actor, string expectedName, IObserver <ScreenshotInfo> observer) { // arrange var screenshot = new Screenshot("abcdefgskdjf"); var expected = new ScreenshotInfo(screenshot, expectedName); var webDriver = new Mock <IWebDriver>(); webDriver.As <ITakesScreenshot>().Setup(t => t.GetScreenshot()).Returns(screenshot); var webBrowser = WebBrowser.With(webDriver.Object); // act Assert.Throws <Exception>(() => sut.ExecuteTakeScreenshot(webBrowser, actor, Throws, () => expectedName, observer)); // assert Mock.Get(observer).Verify(CallOnNext(expected), Times.Once()); object Throws() => throw new Exception(); }
private void lbScreenshots_SelectedIndexChanged(object sender, EventArgs e) { int sel = lbScreenshots.SelectedIndex; if (sel > -1) { ScreenshotInfo ss = lbScreenshots.Items[sel] as ScreenshotInfo; pbScreenshot.Tag = ss; if (ss != null && File.Exists(ss.LocalPath)) { pbScreenshot.LoadImageFromFileAsync(ss.LocalPath); pgScreenshot.SelectedObject = ss; } else if (!string.IsNullOrEmpty(ss.FullImageLink)) { pbScreenshot.LoadImageFromURLAsync(ss.FullImageLink); } else { pbScreenshot.LoadImage(new Bitmap(300, 300)); pgScreenshot.SelectedObject = null; } } }
public void ExecuteTakeScreenshot_WhenAbilityIsNotWebBrowser_ShouldNotCallObserver( bool[] useWebBrowsers, int sampleSize, int[] expectedCalls, IActor actor, string expectedName, IObserver <ScreenshotInfo> observer) { // arrange var sut = new TakeSampleScreenshotsStrategy(sampleSize); var screenshot = new Screenshot("abcdefgskdjf"); var webDriver = new Mock <IWebDriver>(); webDriver.As <ITakesScreenshot>().Setup(t => t.GetScreenshot()).Returns(screenshot); var webBrowser = WebBrowser.With(webDriver.Object); // act foreach (var(useWebBrowser, i) in useWebBrowsers.Select((useWebBrowser, i) => (useWebBrowser, i + 1))) { var ability = useWebBrowser ? webBrowser : new object(); sut.ExecuteTakeScreenshot(ability, actor, () => new object(), () => expectedName + i.ToString(), observer); } // assert if (expectedCalls.Length == 0) { Mock.Get(observer).Verify(o => o.OnNext(It.IsAny <ScreenshotInfo>()), Times.Never()); } else { foreach (var i in expectedCalls) { var expected = new ScreenshotInfo(screenshot, expectedName + i.ToString()); Mock.Get(observer).Verify(CallOnNext(expected), Times.Once()); } } }
public static void AreEqual(ScreenshotInfo expected, ScreenshotInfo actual, IAppRect rect, double expectedToActualScale = 1, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, rect.ToRectangle(), actual, rect.ToRectangle(), expectedToActualScale, PixelTolerance.None, line);
public static void AreEqual(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle?rect = null, double expectedToActualScale = 1, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, PixelTolerance.None, line);
private void Task_ScreenshotUploaded(ScreenshotInfo si) { lbScreenshots.Items.Add(si); lbScreenshots.SelectedIndex = lbScreenshots.Items.Count - 1; }
public void Take(ScreenshotInfo screenshotInfo) { throw new NotSupportedException(); }
public void TearDown() { _result?.Dispose(); _result = null; }
private Expression <System.Action <IObserver <ScreenshotInfo> > > CallOnNext(ScreenshotInfo expected) { return(o => o.OnNext(It.Is <ScreenshotInfo>(s => s.FileName == expected.FileName && s.Screenshot.AsBase64EncodedString == expected.Screenshot.AsBase64EncodedString))); }
/// <summary> /// Asserts that two screenshots are equal to each other inside the area of the nominated rect to within the given pixel error /// (ie, the A, R, G, or B values cumulatively differ by more than the permitted error for any pixel). /// </summary> public static void AreAlmostEqual(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, int permittedPixelError, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), 1, PixelTolerance.Cummulative(permittedPixelError), line);
public void Take(ScreenshotInfo screenshotInfo) { Items.Add(screenshotInfo); }
private void buttonScreenShot_Click(object sender, EventArgs e) { // WebBrowserのサイズ(WEBページのサイズ)に合わせてBitmap生成 Bitmap bmp = new Bitmap(webBrowser1.Width, webBrowser1.Height); // BitmapのGraphicsを取得 Graphics gra = Graphics.FromImage(bmp); // BitmapのGraphicsのHdcを取得 IntPtr hdc = gra.GetHdc(); // WebBrowser(WEBページ)のオブジェクト取得 IntPtr web = System.Runtime.InteropServices.Marshal.GetIUnknownForObject( webBrowser1.ActiveXInstance); // WebBrowser(WEBページ)のイメージをBitmapにコピー Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); OleDraw(web, DVASPECT_CONTENT, hdc, ref rect); // WebBrowser(WEBページ)のオブジェクト使用終了 System.Runtime.InteropServices.Marshal.Release(web); // BitmapのGraphicsの使用終了 gra.Dispose(); // Bitmapを保存する if (textBoxScreenshotPath.Text == String.Empty) { // パスが空欄の場合、exeと同じディレクトリを自動で代入する textBoxScreenshotPath.Text = Application.StartupPath; } String savePath = textBoxScreenshotPath.Text; if (savePath.Last() != '\\') { savePath += '\\'; } if (System.IO.Directory.Exists(savePath)) { // ファイル名を追加する savePath += "Monline_" + DateTime.Now.ToString("yyyyMMddHHmmss"); // 拡張子を追加する String extName = "jpg"; System.Drawing.Imaging.ImageFormat imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; for (Int32 i = 0; i < mScreenshotInfos.Length; ++i) { ScreenshotInfo ssInfo = mScreenshotInfos[i]; if (ssInfo.extensionRadioButton.Checked) { extName = ssInfo.extensionName; imgFormat = ssInfo.imageFormat; break; } } savePath += "." + extName; // 保存する bmp.Save(savePath, imgFormat); } }
public static void AreAlmostEqual(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, int permittedPixelError, double expectedToActualScale = 1, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, PixelTolerance.Cummulative(permittedPixelError), line);
public static void AreEqual(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, double expectedToActualScale = 1, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, PixelTolerance.None, line);
public static void AreEqual(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), expectedToActualScale, tolerance, line);
public static void AreAlmostEqual(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle?rect = null, int permittedPixelError = 5, double expectedToActualScale = 1, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, PixelTolerance.Cummulative(permittedPixelError), line);
public ScreenshotFileSavedEvent(ScreenshotInfo screenshotInfo, string filePath) { ScreenshotInfo = screenshotInfo; FilePath = filePath; }
public static void AreAlmostEqual(ScreenshotInfo expected, Rectangle expectedRect, Bitmap actual, Rectangle actualRect, double expectedToActualScale, PixelTolerance tolerance, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect, null, actual, actualRect, expectedToActualScale, tolerance, line);
public static void AreEqual(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, [CallerLineNumber] int line = 0) => AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), 1, PixelTolerance.None, line);